Skip to content

Commit

Permalink
Merge branch 'master' of github.com:coldbox-modules/cbwire into devel…
Browse files Browse the repository at this point in the history
…opment
  • Loading branch information
grantcopley committed Sep 29, 2024
2 parents 67d8636 + a5e06c6 commit 4eff5ce
Show file tree
Hide file tree
Showing 37 changed files with 912 additions and 113 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:

- name: Upload Test Results Artifacts
if: always()
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: test-results-${{ matrix.cfengine }}
path: |
Expand All @@ -116,7 +116,7 @@ jobs:
- name: Upload Debugging Info To Artifacts
if: ${{ failure() }}
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: Failure Debugging Info - ${{ matrix.cfengine }}
path: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
- name: Upload Debugging Info To Artifacts
if: ${{ failure() }}
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: Failure Debugging Info - ${{ matrix.cfengine }}
path: |
Expand Down
11 changes: 1 addition & 10 deletions ModuleConfig.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,7 @@ component {
* The default folder name where your cbwire components are stored.
* Defaults to 'wires' folder.
*/
"componentLocation" : "wires",
/**
* Determines if Turbo should be enabled
*/
"enableTurbo" : false,
/**
* Caching for single-file components to speed up response time.
* Should be false for local development.
*/
"cacheSingleFileComponents": false,
"wiresLocation" : "wires",
/**
* Trims string properties if set to true
*/
Expand Down
12 changes: 11 additions & 1 deletion handlers/Main.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ component {
* URI: /cbwire/update
*/
function index( event, rc, prc ){
return cbwireController.handleRequest( getHTTPRequestData(), arguments.event );
try {
return cbwireController.handleRequest( getHTTPRequestData(), arguments.event );
} catch ( any e ) {
if ( e.message contains "Page expired" ) {
event.noLayout();
event.setView( view="errors/pageExpired", module="cbwire" );
event.setHTTPHeader( statusCode="419", statusText="Page Expired" );
} else {
rethrow;
}
}
}

/**
Expand Down
37 changes: 23 additions & 14 deletions interceptors/CBWIRE.cfc
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
component {

/**
* Ensures that no other custom interceptors run for the cbwire module.
* Returns true to break the interceptor chain.
*
* @return boolean
* Returns the module settings.
*
* @return struct
*/
function afterAspectsLoad() {
variables.CBWIREController = getInstance( dsl="CBWIREController@cbwire");
variables.settings = getInstance( dsl="coldbox:modulesettings:cbwire" );
function getSettings() {
return getInstance( "coldbox:modulesettings:cbwire" );
}

/**
* Returns the CBWIRE controller.
*
* @return CBWIREController
*/
function getCBWIREController() {
return getInstance( "CBWIREController@cbwire" );
}

/**
Expand All @@ -17,7 +24,8 @@ component {
* @return void
*/
function preReinit() {
local.tmpDirectory = variables.settings.moduleRootPath & "/models/tmp";
local.settings = getSettings();
local.tmpDirectory = local.settings.moduleRootPath & "/models/tmp";

if ( directoryExists( local.tmpDirectory ) ) {
directoryDelete( local.tmpDirectory, true );
Expand Down Expand Up @@ -69,9 +77,8 @@ component {
data = "",
statusCode = 400
).noExecution();
// Returning true breaks further interceptors execution.
return true;
}
return true;
}

function preEvent() eventPattern="^cbwire.*" {
Expand Down Expand Up @@ -123,9 +130,10 @@ component {
}

function postLayoutRender() {
if ( shouldInject( arguments.event ) ) {
if ( shouldInject( arguments.event ) && !request.keyExists( "_cbwire_injected_assets" ) ) {
arguments.data.renderedLayout = replaceNoCase( arguments.data.renderedLayout, "</head>", getStyles() & chr( 10 ) & "</head>", "one" );
arguments.data.renderedLayout = replaceNoCase( arguments.data.renderedLayout, "</body>", getScripts() & chr( 10 ) & "</body>", "one" );
request._cbwire_injected_assets = true;
}
}

Expand Down Expand Up @@ -172,7 +180,7 @@ component {
* @return string
*/
private function getStyles() {
return variables.CBWIREController.getStyles();
return getCBWIREController().getStyles();
}

/**
Expand All @@ -181,7 +189,7 @@ component {
* @return string
*/
private function getScripts() {
return variables.CBWIREController.getScripts();
return getCBWIREController().getScripts();
}

/**
Expand All @@ -192,6 +200,7 @@ component {
* @return boolean
*/
private function shouldInject( event ) {
return arguments.event.getCurrentModule() != "cbwire" && variables.settings.autoInjectAssets == true;
local.settings = getSettings();
return arguments.event.getCurrentModule() != "cbwire" && local.settings.autoInjectAssets == true;
}
}
115 changes: 85 additions & 30 deletions models/CBWIREController.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ component singleton {
// Inject module settings
property name="moduleSettings" inject="coldbox:modulesettings:cbwire";

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

// Inject SingleFileComponentBuilder
property name="singleFileComponentBuilder" inject="SingleFileComponentBuilder@cbwire";

function init() {
// Initialize the array to store single file components
variables._singleFileComponents = [];
return this;
}
/**
* Instantiates a CBWIRE component, mounts it,
* and then calls its internal renderIt() method.
Expand All @@ -32,6 +37,7 @@ component singleton {
*/
function wire(required name, params = {}, key = "", lazy = false, lazyIsolated = true ) {
local.instance = createInstance(argumentCollection=arguments)
._withPath( arguments.name )
._withEvent( getEvent() )
._withParams( arguments.params, arguments.lazy )
._withKey( arguments.key );
Expand Down Expand Up @@ -59,8 +65,7 @@ component singleton {
local.csrfTokenVerified = variables.wirebox.getInstance( dsl="@cbcsrf" ).verify( local.csrfToken );
// Check the CSRF token, throw 403 if invalid
if( !local.csrfTokenVerified ){
cfheader( statusCode="403", statusText="Forbidden" );
throw( type="CBWIREException", message="Invalid CSRF token." );
throw( type="CBWIREException", message="Page expired." );
}
// Perform additional deserialization of the component snapshots
local.payload.components = local.payload.components.map( function( _comp ) {
Expand All @@ -74,6 +79,7 @@ component singleton {
local.componentInstance = createInstance( _componentPayload.snapshot.memo.name );
// Return the response for this component
return local.componentInstance
._withPath( _componentPayload.snapshot.memo.name )
._withEvent( event )
._withIncomingPayload( _componentPayload )
._getHTTPResponse( _componentPayload );
Expand Down Expand Up @@ -173,19 +179,26 @@ 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 & "'." );
}
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 {
// Check if we've already flagged this component as a single file component
// This is to improve performance by not attempting to create the component again
if ( variables._singleFileComponents.contains( arguments.name ) ) {
throw( type="Injector.InstanceNotFoundException", message="Component '#arguments.name#' is a single file component." );
}
// Attempt to create an instance of the component
return variables.wirebox.getInstance(local.fullComponentPath);
local.componentInstance = variables.wirebox.getInstance(local.fullComponentPath)
._withPath( arguments.name );
return local.componentInstance;
} catch( Injector.InstanceNotFoundException e ) {
local.singleFileComponent = variables.singleFileComponentBuilder
.setInitialRender( true )
Expand All @@ -196,6 +209,7 @@ component singleton {
abort;
rethrow;
}
variables._singleFileComponents.append( arguments.name );

return local.singleFileComponent;
} catch (Any e) {
Expand All @@ -206,24 +220,56 @@ 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 path to the modules folder.
*
* @module string | The name of the module.
*
* @return string
*/
function getModuleRootPath( module ) {
var moduleRegistry = moduleService.getModuleRegistry();

if ( moduleRegistry.keyExists( module ) ) {
return moduleRegistry[ module ].invocationPath & "." & module;
}

throw( type="ModuleNotFound", message = "CBWIRE cannot locate the module '" & arguments.module & "'.")
}

/**
* 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.
*/
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();

var result = arguments.path contains "." ?
moduleRegistry[ module ].invocationPath & "." & module & "." & arguments.path :
moduleRegistry[ module ].invocationPath & "." & module & "." & getWiresLocation() & "." & arguments.path;

return result;
}

/**
* Returns the path to the wires folder within a module path.
*
* @module string | The name of the module.
*
* @return string
*/
function getModuleWiresPath( module ) {
local.moduleRegistry = moduleService.getModuleRegistry();
return arguments
}

/**
* Returns the ColdBox RequestContext object.
Expand Down Expand Up @@ -442,4 +488,13 @@ component singleton {
return requestService.getController().getRenderer().view( argumentCollection=arguments );
}

/**
* Returns the URI endpoint for updating CBWIRE components.
*
* @return string
*/
function getUpdateEndpoint() {
var settings = variables.moduleSettings;
return settings.keyExists( "updateEndpoint") && settings.updateEndpoint.len() ? settings.updateEndpoint : "/cbwire/update";
}
}
Loading

0 comments on commit 4eff5ce

Please sign in to comment.