/** * @param $route * * @return null */ public function addRoute($route) { $counter = count($this->_routes); $route = Craft::createComponent($route); $route->init(); $this->_routes[$counter] = $route; }
/** * @param string $name command name (case-insensitive) * @return \CConsoleCommand the command object. Null if the name is invalid. */ public function createCommand($name) { $name = strtolower($name); $command = null; if (isset($this->commands[$name])) { $command = $this->commands[$name]; } else { $commands = array_change_key_case($this->commands); if (isset($commands[$name])) { $command = $commands[$name]; } } if ($command !== null) { if (is_string($command)) { if (strpos($command, '/') !== false || strpos($command, '\\') !== false) { $className = IOHelper::getFileName($command, false); // If it's a default framework command, don't namespace it. if (strpos($command, 'framework') === false) { $className = __NAMESPACE__ . '\\' . $className; } if (!class_exists($className, false)) { require_once $command; } } else { $className = Craft::import($command); } return new $className($name, $this); } else { return Craft::createComponent($command, $name, $this); } } else { if ($name === 'help') { return new \CHelpCommand('help', $this); } else { return null; } } }
/** * Creates a controller instance based on a route. * * @param string $route * @param mixed $owner * @return array|null */ public function createController($route, $owner = null) { if (($route = trim($route, '/')) === '') { $route = $this->defaultController; } $routeParts = array_filter(explode('/', $route)); // First check if the controller class is a combination of the first two segments. // That way FooController won't steal all of Foo_BarController's requests. if (isset($routeParts[1])) { $controllerId = ucfirst($routeParts[0]) . '_' . ucfirst($routeParts[1]); $class = __NAMESPACE__ . '\\' . $controllerId . 'Controller'; if (class_exists($class)) { $action = implode('/', array_slice($routeParts, 2)); } } // If that didn't work, now look for that FooController. if (!isset($action)) { $controllerId = ucfirst($routeParts[0]); $class = __NAMESPACE__ . '\\' . $controllerId . 'Controller'; if (class_exists($class)) { $action = implode('/', array_slice($routeParts, 1)); } } // Did we find a valid controller? if (isset($action)) { return array(Craft::createComponent($class, $controllerId), $this->parseActionParams($action)); } }