Cake PHP 运行 Console/command 来自 root 的模块

Cake PHP run Console/command from within a module from root

我正在做一个 Cake PHP 2 项目,并且在 app/modules/MyModule 中设置了一个自定义模块系统,在这个目录中,我有如下文件夹:

我添加了一个名为 Console 的新目录,并在其中创建了一个 Command 目录,我将在其中放置命令。

但是,Console/cake 命令存在于 app/Console 而不是 app/modules/MyModule/Console 目录中。

我怎样才能拥有实际上是插件但从主 Console/cake 目录执行的命令。

我的自定义模块设置是这样的:

<?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;
    }

}

您必须连接 sub-namespace/package,CakePHP 将在其中寻找 shell,即 Console/Command,如果您有任务,也需要连接:

// ...
'Console/Command' => array("$modules_base_dir/$classname/Console/Command/"),
'Console/Command/Task' => array("$modules_base_dir/$classname/Console/Command/Task/"),
// ...