public function createCommand() { $usage = 'format is invalid: {Module}:{A,B,C,D,E}'; $arguments = $this->arguments->get(); if (count($arguments) === 0) { $this->console->writeLn($usage); return 1; } $parts = explode(':', $arguments[0]); if (count($parts) !== 2) { $this->console->writeLn($usage); return 1; } $moduleName = Text::camelize($this->crossword->guess($this->application->getModules(), $parts[0])); if (!$moduleName) { return $this->console->error('module name is unknown: `:module`', ['module' => $parts[0]]); } $controllers = explode(',', $parts[1]); $controllerNamespace = $this->alias->resolve('@ns.app' . '\\' . $moduleName . '\\Controllers'); foreach ($controllers as $controller) { $controller = Text::camelize($controller); $controllerName = $controller . 'Controller'; $controllerFile = '@app/' . $moduleName . '/Controllers/' . $controllerName . '.php'; if ($this->filesystem->fileExists($controllerFile)) { $this->console->writeLn('`:controller` controller exists already', ['controller' => $controllerNamespace . '\\' . $controller]); continue; } $controllerContent = <<<EOD <?php namespace {$controllerNamespace}; class {$controllerName} extends ControllerBase{ public function indexAction() { } } EOD; $this->filesystem->filePut($controllerFile, $controllerContent); $this->filesystem->filePut('@app/' . $moduleName . '/Views/' . $controller . '/Index.sword', ''); $this->filesystem->filePut('@app/' . $moduleName . '/Layouts/' . $controller . '.sword', '@content()'); } return 0; }
/** * @param string|array $controllerAction * @param int $duration * @param int $ip_times * @param int $user_times * * @return void * @throws \ManaPHP\Security\RateLimiter\Exception */ public function limit($controllerAction, $duration, $ip_times, $user_times = null) { if ($controllerAction === null) { $resource = $this->dispatcher->getControllerName() . ':' . $this->dispatcher->getActionName(); $this->limitAny($resource, $duration, $ip_times, $user_times); } else { if (is_array($controllerAction)) { $resource = basename($controllerAction[0], 'Controller') . ':' . lcfirst(Text::camelize($controllerAction[1])); } else { $parts = explode(':', $controllerAction); if (count($parts) !== 2) { throw new RateLimiterException('`:controllerAction` controllerAction is invalid: the correct format is `controller:action`', ['controllerAction' => $controllerAction]); } $resource = Text::camelize($parts[0]) . ':' . lcfirst(Text::camelize($parts[1])); } if ($resource === $this->dispatcher->getControllerName() . ':' . $this->dispatcher->getActionName()) { $this->limitAny($controllerAction, $duration, $ip_times, $user_times); } } }
/** * @param string $cmd * * @return bool */ public function route($cmd) { $this->_controllerName = null; $this->_actionName = null; $command = $cmd ?: 'help:list'; if (isset($this->_commandAliases[strtolower($command)])) { $command = $this->_commandAliases[strtolower($command)]; } $parts = explode(':', $command); switch (count($parts)) { case 1: $controllerName = $parts[0]; $actionName = null; break; case 2: $controllerName = $parts[0]; $actionName = $parts[1]; break; default: return false; } if ($this->_guessCommand && strlen($controllerName) <= 3) { $controllers = $this->_getControllers(); $controllerName = $this->crossword->guess($controllers, $controllerName); if (!$controllerName) { return false; } } else { $controllerName = Text::camelize($controllerName); } if ($actionName === null) { $commands = $this->_getCommands($controllerName); if (count($commands) === 1) { $actionName = $commands[0]; } else { return false; } } else { if ($this->_guessCommand && strlen($actionName) <= 2) { $commands = $this->_getCommands($controllerName); $actionName = $this->crossword->guess($commands, $actionName); if (!$actionName) { return false; } } else { $actionName = lcfirst(Text::camelize($actionName)); } } $this->_controllerName = $controllerName; $this->_actionName = $actionName; return true; }
/** * @param array $table * * @return array */ protected function _getByColumns($table) { $keyColumns = []; foreach ($this->db->fetchAll("SHOW INDEX FROM `{$table}`") as $row) { $keyName = $row['Key_name']; if (!isset($keyColumns[$keyName])) { $keyColumns[$keyName] = []; } $keyColumns[$keyName] = $row['Column_name']; } $columns = []; foreach ($keyColumns as $k => $v) { if (count($v) === 1) { $columns[$v] = Text::camelize($v); } } return $columns; }
/** * @description create a new module * @throws \Application\Exception * @throws \ManaPHP\Filesystem\Adapter\Exception */ public function createCommand() { $modules = ['dd']; foreach ($modules as $module) { $module = Text::camelize($module); $moduleDir = $this->alias->resolve('@app/' . $module); if ($this->filesystem->dirExists($moduleDir)) { throw new Exception('`:module` module is exists already.', ['module' => $module]); } $this->filesystem->dirCreate($moduleDir . '/Models'); $this->filesystem->dirCreate($moduleDir . '/Views'); $this->filesystem->dirCreate($moduleDir . '/Views/Shared'); $this->filesystem->dirCreate($moduleDir . '/Views/Layouts'); $this->filesystem->dirCreate($moduleDir . '/Views/Widgets'); $this->filesystem->dirCreate($moduleDir . '/Widgets'); $this->filesystem->dirCreate($moduleDir . '/Controllers'); //------------------------------ $controllerBaseContent = <<<EOD <?php namespace Application\\{$module}\\Controllers; use ManaPHP\\Mvc\\Controller; class ControllerBase extends Controller { } EOD; $this->filesystem->filePut($moduleDir . './Controllers/ControllerBase.php', $controllerBaseContent); //--------------------------------------- $indexControllerContent = <<<EOD <?php namespace Application\\{$module}\\Controllers; class IndexController extends ControllerBase { public function indexAction(){ echo __FILE__; } } EOD; $this->filesystem->filePut($moduleDir . '/Controllers/IndexController.php', $indexControllerContent); //----------------------- $moduleContent = <<<EOD <?php namespace Application\\{$module}; class Module extends \\ManaPHP\\Mvc\\Module { public function registerServices(\$di) { } public function authorize(\$controller, \$action) { return true; } } EOD; $this->filesystem->filePut($moduleDir . '/Module.php', $moduleContent); //------------------------ $routeGroupContent = <<<EOD <?php namespace Application\\{$module}; use ManaPHP\\Mvc\\Router\\Group; class RouteGroup extends Group { public function __construct() { parent::__construct(true); } } EOD; $this->filesystem->filePut($moduleDir . '/RouteGroup.php', $routeGroupContent); $viewLayoutsDefaultContent = <<<EOD <!DOCTYPE html> <html lang="en"> <head> \t<meta charset="UTF-8"> \t<title>{$module}</title> </head> <body> @content()\t </body> </html> EOD; $this->filesystem->filePut($moduleDir . '/Views/Layouts/Default.sword', $viewLayoutsDefaultContent); $this->filesystem->filePut($moduleDir . '/Views/Layouts/Index.sword', $viewLayoutsDefaultContent); //---------- $viewIndexContent = <<<EOD <h1>{$module}</h1> EOD; $this->filesystem->filePut($moduleDir . '/Views/Index/Index.sword', $viewIndexContent); } }
/** * @param string $controllerName * * @return static */ public function setControllerName($controllerName) { $this->_controllerName = Text::camelize($controllerName); return $this; }