Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @Module syntax support for V4 #158

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions models/CBWIREController.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ component singleton {

// Inject module settings
property name="moduleSettings" inject="coldbox:modulesettings:cbwire";

// Inject module service
property name="moduleService" inject="coldbox:moduleService";

// Inject SingleFileComponentBuilder
property name="singleFileComponentBuilder" inject="SingleFileComponentBuilder@cbwire";
Expand Down Expand Up @@ -170,6 +173,16 @@ component singleton {
local.fullComponentPath = "wires." & local.fullComponentPath;
}

if ( find( "@", local.fullComponentPath ) ) {
// This is a module reference, find in our module
var params = listToArray( local.fullComponentPath, "@" );
if ( params.len() != 2 ) {
throw( type="ModuleNotFound", message = "CBWIRE cannot locate the module or component using '" & local.fullComponentPath & "'." );
}
// modify local.fullComponentPath to full path for module
local.fullComponentPath = getModuleComponentPath( params[ 1 ], params[ 2 ] );
}

try {
// Attempt to create an instance of the component
return variables.wirebox.getInstance(local.fullComponentPath);
Expand All @@ -193,6 +206,25 @@ component singleton {
}
}

/**
* Returns the full dot notation path to a modules component.
*
* @path String | Name of the cbwire component.
* @module String | Name of the module to look for wire in.
*/
private function getModuleComponentPath( path, module ) {
var moduleConfig = moduleService.getModuleConfigCache();
if ( !moduleConfig.keyExists( module ) ) {
throw( type="ModuleNotFound", message = "CBWIRE cannot locate the module '" & arguments.module & "'.")
}

// if there is a dot in the path, then we are referencing a folder within a module otherwise use the default wire location.
var moduleRegistry = moduleService.getModuleRegistry();
return arguments.path contains "." ?
moduleRegistry[ module ].invocationPath & "." & module & "." & arguments.path :
moduleRegistry[ module ].invocationPath & "." & module & "." & getWiresLocation() & "." & arguments.path;
}

/**
* Returns the ColdBox RequestContext object.
*
Expand Down
4 changes: 4 additions & 0 deletions test-harness/anotherFolder/OutsideFolderComponent.cfm
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<cfscript>
//@startWire

data = {
"title": "outside component"
}

//@endWire
</cfscript>

<cfoutput>
Expand Down
54 changes: 28 additions & 26 deletions test-harness/box.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
{
"name": "Tester",
"version": "0.0.0",
"slug": "tester",
"private": true,
"description": "",
"dependencies": {
"coldbox": "^6",
"cbvalidation": "^4.3.0+24",
"cbi18n": "^3.2.0+12"
"name":"Tester",
"version":"0.0.0",
"slug":"tester",
"private":true,
"description":"",
"dependencies":{
"coldbox":"^6",
"cbvalidation":"^4.3.0+24",
"cbi18n":"^3.2.0+12",
"cbcsrf":"^3.1.0+16"
},
"devDependencies": {
"testbox": "^5",
"route-visualizer": "^1.4.0+24"
"devDependencies":{
"testbox":"^5",
"route-visualizer":"^1.4.0+24"
},
"installPaths": {
"coldbox": "coldbox/",
"testbox": "testbox/",
"cbvalidation": "modules/cbvalidation/",
"route-visualizer": "modules/route-visualizer/",
"cbi18n": "modules/cbi18n/"
"installPaths":{
"coldbox":"coldbox/",
"testbox":"testbox/",
"cbvalidation":"modules/cbvalidation/",
"route-visualizer":"modules/route-visualizer/",
"cbi18n":"modules/cbi18n/",
"cbcsrf":"modules/cbcsrf/"
},
"testbox": {
"runner": "http://localhost:60299/tests/runner.cfm"
"testbox":{
"runner":"http://localhost:60299/tests/runner.cfm"
},
"scripts": {
"cfpm": "echo '\".engine/adobe2021/WEB-INF/cfusion/bin/cfpm.sh\"' | run",
"cfpm:install2021": "echo '\".engine/adobe2021/WEB-INF/cfusion/bin/cfpm.sh\" install ${1}' | run",
"cfpm:install2023": "echo '\".engine/adobe2023/WEB-INF/cfusion/bin/cfpm.sh\" install ${1}' | run",
"install:2021": "run-script cfpm:install2021 zip,mysql,debugger",
"install:2023": "run-script cfpm:install2023 zip,mysql,debugger"
"scripts":{
"cfpm":"echo '\".engine/adobe2021/WEB-INF/cfusion/bin/cfpm.sh\"' | run",
"cfpm:install2021":"echo '\".engine/adobe2021/WEB-INF/cfusion/bin/cfpm.sh\" install ${1}' | run",
"cfpm:install2023":"echo '\".engine/adobe2023/WEB-INF/cfusion/bin/cfpm.sh\" install ${1}' | run",
"install:2021":"run-script cfpm:install2021 zip,mysql,debugger",
"install:2023":"run-script cfpm:install2023 zip,mysql,debugger"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<cfscript>
//@startWire

data = {
"title": "Nested module component"
}

//@endWire
</cfscript>

<cfoutput>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<cfscript>
//@startWire

data = {
"title": "Nested module component using default wires location"
}

//@endWire
</cfscript>

<cfoutput>
Expand Down
22 changes: 22 additions & 0 deletions test-harness/tests/specs/CBWIRESpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,28 @@ component extends="coldbox.system.testing.BaseTestCase" {
expect( result.trim() ).toBe( "</div>" );
} );

it( "can render component from ./wires folder using wire()", function() {
var result = cbwireController.wire( "TestComponent" );
expect( result ).toContain( "Title: CBWIRE Rocks!" );
} );

it( "can render component from nested folder using wire()", function() {
var result = cbwireController.wire( "wires.nestedComponent.NestedFolderComponent" );
expect( result ).toContain( "Nested folder component" );
} );

it( "throws error if it's unable to find a module component", function() {
expect( function() {
var result = cbwireController.wire( "missing@someModule" );
} ).toThrow( type="ModuleNotFound" );
} );

it( "can render component from nested module using default wires location", function() {
var result = cbwireController.wire( "NestedModuleDefaultComponent@testingmodule" );
expect( result ).toContain( "Nested module component using default wires location" );
} );


});

describe( "Preprocessors", function() {
Expand Down
4 changes: 4 additions & 0 deletions test-harness/wires/nestedComponent/NestedFolderComponent.cfm
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<cfscript>
//@startWire

data = {
"title": "Nested folder component"
}

//@endWire
</cfscript>

<cfoutput>
Expand Down
Loading