Reputation: 2945
I'm working on a Cake PHP 2 project and have got a custom module system set up that exists within app/modules/MyModule
, within this directory I have folders such as:
I've added a new one called Console, and within this have created a Command
directory where I'll put commands.
However, the Console/cake
command exists within app/Console
rather than app/modules/MyModule/Console
directory.
How can I have commands that exist within what is effectively a plugin, but executed from the main Console/cake
directory.
My custom module set up works like this:
<?php
App::uses('BaseModule', 'Modules');
App::uses('CakeEventManager', 'Event');
/**
* Helper class to load modules of a specific format from /app/modules directory,
* and create instances that can connect to system events, modify system behaviours etc.
*
* Usage:
*
* $_modules = new Modules();
$mods_arr = $_modules->initModules(ROOT.'/app/modules');
*
*
*/
class Modules
{
public function initModules($modules_base_dir)
{
$modules = array();
//loop over all directories in /app/modules/
foreach (new DirectoryIterator($modules_base_dir) as $dir)
{
if($dir->isDot()) continue;
if($dir->getFilename()=='.svn') continue;
if($dir->isFile()) {
continue;
}
//compile a list of all modules, and load each Module class
$classname = $dir->getFilename();
App::import('modules/'.$classname, $classname);
$module = new $classname();
array_push($modules, $module);
//enumerate all events from BaseModule so we know what we need to handle
$base_events_array = array();
$methods = get_class_methods('BaseModule');
foreach($methods as $method)
{
//strip out any method that starts with "handle_"
if(substr($method, 0, 7)=='handle_')
{
$base_events_array[] = substr($method, 7);
}
}
//IF this module is enabled
if($module->_enabled)
{
//register any EXISTING event handlers for this module
foreach($base_events_array as $event_name)
{
if(method_exists($module, 'handle_'.$event_name))
{
CakeEventManager::instance()->attach(array($module, 'handle_'.$event_name), $event_name);
}
}
//connect up any additional controllers,views, models, bootstraps from this module
App::build(array(
'Console' => array($modules_base_dir.'/'.$classname.'/Console/'),
'Controller' => array($modules_base_dir.'/'.$classname.'/Controller/'),
'View' => array($modules_base_dir.'/'.$classname.'/View/'),
'Model' => array($modules_base_dir.'/'.$classname.'/Model/'),
'Vendor' => array($modules_base_dir.'/'.$classname.'/Vendor/')
));
if(file_exists($modules_base_dir.'/'.$classname.'/bootstrap.php'))
{
include_once $modules_base_dir.'/'.$classname.'/bootstrap.php';
}
}
}
//die(var_dump(App::path('Controller')));
return $modules;
}
}
Upvotes: 0
Views: 487
Reputation: 60453
You'd have to connect the sub-namespace/package where CakePHP will look for shells, ie Console/Command
, and if you have tasks, that would need to be connected too:
// ...
'Console/Command' => array("$modules_base_dir/$classname/Console/Command/"),
'Console/Command/Task' => array("$modules_base_dir/$classname/Console/Command/Task/"),
// ...
Upvotes: 2