/** * 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); } }
/** * Set up the basic uri filters in our static init and * also add a default 404 response * * @return void */ public static function _init() { // default string static::filter('any', '[a-zA-Z0-9' . ClanCats::$config->get('router.allowed_special_chars') . ']'); // only numbers static::filter('num', '[0-9]'); // only alpha characters static::filter('alpha', '[a-zA-Z]'); // only alphanumeric characters static::filter('alphanum', '[a-zA-Z0-9]'); // 404 not found error CCRouter::on('#404', function () { return CCResponse::create(CCView::create('Core::CCF/404')->render(), 404); }); }
/** * Execute the Request * * @param array $action * @param array $params * * @return self */ public function perform() { // set the input if (!is_null($this->input)) { CCIn::instance($this->input); } else { CCIn::instance(CCServer::instance()); } // set current request static::$_current =& $this; // route is invalid show 404 if (!$this->route instanceof CCRoute) { $this->route = CCRouter::resolve('#404'); } /* * call wake events * if one event returns an response all other calls will be skipped also events! */ foreach (CCRouter::events_matching('wake', $this->route->uri) as $callback) { if (($return = CCContainer::call($callback)) instanceof CCResponse) { $this->response = $return; return $this; } } /* * a closure */ if (!is_array($this->route->callback) && is_callable($this->route->callback)) { // execute and capture the output ob_start(); // run the closure $return = call_user_func_array($this->route->callback, $this->route->params); // catch the output $output = ob_get_clean(); // do we got a response? if (!$return instanceof CCResponse) { // if not create one with the captured output $return = CCResponse::create($output); } } elseif (is_callable($this->route->callback)) { // execute the callback and get the return $return = call_user_func_array($this->route->callback, array($this->route->action, $this->route->params)); // do we got a response? if (!$return instanceof CCResponse) { // if not create one with the return as string $return = CCResponse::create((string) $return); } } else { $return = CCResponse::error(404); } // set the response $this->response = $return; /* * call sleep events * if one event returns an response all other calls will be skipped also events! */ foreach (CCRouter::events_matching('sleep', $this->route->uri) as $callback) { if ($return = CCContainer::call($callback, $this->response) instanceof CCResponse) { $this->response = $return; return $this; } } return $this; }
/** * Create an URL based on an router alias * * @param string $alias * @param array $params * @param bool $retain Should we keep the get parameters? * @return string */ public static function alias($alias, $params = array(), $retain = false) { $route_params = array(); // to handle the suffix after a slash in an alias define $suffix = ''; if (strpos($alias, '/') !== false && $alias !== '/') { // slashes in aliases get appended as suffix list($alias, $suffix) = explode('/', $alias); $suffix = '/' . $suffix; } // get the parameters with the numeric keys so we can // pass them as route parameters like [any]-[num]-[num]/[any]/ foreach ($params as $key => $value) { if (is_int($key)) { $route_params[] = $value; unset($params[$key]); } } return CCUrl::to(CCRouter::alias($alias, $route_params) . $suffix, $params, $retain); }
/** * start the ccf app lifecycle * * This method registers the App class and runs the wake events. * * @param string $app The used app class = APPATH/<$app>.php * @return void */ public static function wake_app($app) { static::$runtime_class = $app; \CCFinder::bind($app, static::$paths['app'] . $app . EXT); // run the application wake $response = $app::wake(); // when the application wake returns an response we display it if ($response instanceof CCResponse) { if (static::$config->send_app_wake_response) { $response->send(true); die; } } $response = null; // run the environment wake if (method_exists($app, 'wake_' . static::$environment)) { $response = call_user_func($app . '::wake_' . static::$environment); // when the application env wake returns an response we display it if ($response instanceof CCResponse) { if (static::$config->send_app_wake_response) { $response->send(true); die; } } } // add routes from the app CCRouter::on($app::routes()); }
/** * CCUrl::action */ public function test_action() { CCRouter::on('test_action', 'CCUnit::Test'); CCRequest::uri('test_action/detail')->perform(); $this->assertEquals('/test_action/detail/?woo=yay', CCUrl::action('detail/', array('woo' => 'yay'))); $this->assertEquals('/test_action/?woo=yay', CCUrl::action('index', array('woo' => 'yay'))); $this->assertEquals('/test_action/', CCUrl::action()); // another route CCRouter::on('test_action/wurst', 'CCUnit::Test'); CCRequest::uri('test_action/wurst/detail')->perform(); $this->assertEquals('/test_action/wurst/detail/?woo=yay', CCUrl::action('detail/', array('woo' => 'yay'))); $this->assertEquals('/test_action/wurst/?woo=yay', CCUrl::action('index', array('woo' => 'yay'))); $this->assertEquals('/test_action/wurst/', CCUrl::action()); }
/** * Tests the CCRouter filter escaping */ public function test_escaping() { CCRouter::on('this/u$l/conta([num])n+special/[any]', function () { echo "Escaping works"; }); $response = CCRequest::uri('this/u$l/conta(1)n+special/chars')->perform()->response()->body; $this->assertEquals($response, "Escaping works"); }