[英]Cake PHP run Console/command from within a module from root
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:我正在做一个 Cake PHP 2 项目,并且在
app/modules/MyModule
中设置了一个自定义模块系统,在这个目录中我有如下文件夹:
I've added a new one called Console , and within this have created a Command
directory where I'll put commands.我添加了一个名为Console的新目录,并在其中创建了一个
Command
目录,我将在其中放置命令。
However, the Console/cake
command exists within app/Console
rather than app/modules/MyModule/Console
directory.但是,
Console/cake
命令存在于app/Console
中,而不是app/modules/MyModule/Console
目录中。
How can I have commands that exist within what is effectively a plugin, but executed from the main Console/cake
directory.我怎样才能拥有有效插件中存在的命令,但从主
Console/cake
目录执行。
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;
}
}
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:您必须连接子命名空间/包,其中 CakePHP 将寻找 shell,即
Console/Command
,如果您有任务,也需要连接:
// ...
'Console/Command' => array("$modules_base_dir/$classname/Console/Command/"),
'Console/Command/Task' => array("$modules_base_dir/$classname/Console/Command/Task/"),
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.