/** * collect controllers list * @param Project $project * @throws \Cti\Core\Exception */ public function init(Project $project) { foreach ($project->getClasses('Controller') as $controller) { $location = '/'; $name = Reflection::getReflectionClass($controller)->getShortName(); $slug = String::camelCaseToUnderScore(substr($name, 0, -1 * strlen('Controller'))); if ($slug != 'default') { $location = '/' . $slug; } $this->add($location, $controller); } // validate base if ($this->base != '/') { if ($this->base[0] != '/') { throw new Exception('Base url not begins with /'); } elseif ($this->base[strlen($this->base) - 1] != '/') { throw new Exception('Base url not ends with /'); } if (strpos($_SERVER['REQUEST_URI'], $this->base) !== 0) { throw new Exception(sprintf('Base url (%s) is incorrect', $this->base)); } } // define method $this->method = strtolower($_SERVER['REQUEST_METHOD']); // define location $this->location = '/' . substr($_SERVER['REQUEST_URI'], strlen($this->base)); if ($this->location[strlen($this->location) - 1] != '/') { $this->location .= '/'; } // process controllers $controllers = $this->controllers; $this->controllers = array(); foreach ($controllers as $path => $controller) { if (is_numeric($path)) { $class = Reflection::getReflectionClass($controller)->getShortName(); if (substr($class, -10) == 'Controller') { $class = substr($class, 0, -10); $slug = String::camelCaseToUnderScore($class); if ($slug == 'default' || !$slug) { $path = '/'; } else { $path = '/' . $slug . '/'; } } } $this->add($path, $controller); } }
/** * Initialize console application * @param Core $core * @param Project $project * @throws \Cti\Di\Exception */ function init(Core $core, Project $project) { $commands = array_merge($project->getClasses('Command'), $core->getClasses('Command')); array_walk($commands, array($this, 'processClass')); }