/** * initialize the ship * * @return void */ public function wake() { if (!\ClanCats::in_development()) { return; } // get all controllers in the dev namespace foreach (\CCFile::ls(\CCPath::controllers('Dev::*Controller' . EXT)) as $path) { $name = \CCStr::cut(basename($path), 'Controller' . EXT); \CCRouter::on('dev/' . \CCStr::lower($name), 'Dev::' . $name); } }
/** * generate an controller * * exmample: * run shipyard::controller <controller> * run shipyard::controller <controller> <parent_class> * run shipyard::controller <namespace>::<controller> * * @param array $params * @return void */ public function action_controller($params) { $options = \CCDataObject::assign($params); $name = $options->get(0, null); $parent = $options->get(1, null); // get name if we dont have one while (!$name) { $name = $this->read('Please enter the controller name: '); } // fix controller suffix if (substr($name, strlen('Controller') * -1) != 'Controller') { $name .= 'Controller'; } // try to resolve the path if (!($path = \CCPath::controllers(str_replace('_', '/', $name), EXT))) { $this->error('Could not resolve the path. Check if the namespace is registered.'); return; } // parent if (is_null($parent)) { $parent = '\\CCController'; } // view controller if ($options->get('view', false)) { $parent = '\\CCViewController'; } // create the class $class = \CCShipyard::create('class', $name, $parent); // get the actions $actions = array('index'); if ($options->get('actions', false)) { $actions = array_merge($actions, explode(',', $options->get('actions'))); } foreach ($actions as $action) { $action = trim($action); $class->add('function', 'action_' . $action, 'protected', 'echo "' . $name . ' ' . $action . ' action";', ucfirst($action) . " action\n@return void|CCResponse"); $class->add('line', 2); } // add static init if (!$options->get('no-events', false)) { $class->add('function', 'wake', 'protected', '//', "Controller wake\n@return void|CCResponse"); $class->add('line', 2); $class->add('function', 'sleep', 'protected', '//', "Controller wake\n@return void"); } // check for overwrite if (file_exists($path)) { if (!$this->confirm("The class already exists. Do you wish to overwrite it?", true)) { return; } } // write file \CCFile::write($path, $class->output()); }