/** * 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); } }
/** * Get the current builder output * * @return string */ public function output() { $shema = \DB::fetch("DESCRIBE {$this->table};", array(), null, 'assoc'); if (empty($shema)) { throw new CCException('CCShipyard - The given database table is invalid or does not exist.'); } $class = \CCShipyard::create('class', $this->name, "\\DB\\Model"); $class->add('property', '_table', 'protected static', $this->table, 'The database table name'); // timestamps if ($this->timestamps) { $class->add('line', 2); $class->add('property', '_timestamps', 'protected static', true, 'Allow automatic timestamps'); } // shema $class->add('line', 2); // define the internal types $internal_types = array('bool', 'int', 'float', 'double', 'string'); $match_types = array('tinyint' => 'int', 'text' => 'string', 'varchar' => 'string'); foreach ($shema as $item) { $default = $item['Default']; $field = $item['Field']; if (empty($default)) { $type = \CCStr::cut($item['Type'], '('); if (array_key_exists($type, $match_types)) { $type = $match_types[$type]; } elseif (!in_array($type, $internal_types)) { $type = null; } // The primary key should not contain a default value if ($item['Key'] == 'PRI') { $type = null; } elseif ($item['Type'] == 'tinyint(1)') { $type = 'bool'; } if ($type !== null) { settype($default, $type); } } $buffer = "\t'{$field}'"; if (!is_null($type)) { $buffer .= " => array( '" . $type . "'"; if (!is_null($default)) { $buffer .= ", " . var_export($default, true) . " )"; } } $props[] = $buffer; } $class->add('property', '_defaults', 'protected static', "array(\n" . implode(",\n", $props) . "\n)", 'The ' . $class . ' default properties', false); return $class->output(); }
/** * check if a controller implements an action * * @param string $path * @param string $action * @return bool */ public static function has_action($path, $action = null) { $path = CCStr::cut($path, '@'); $path = CCStr::cut($path, '?'); // fix closure given if (!is_string($path)) { return false; } // get controllers default action if (is_null($action)) { $action = static::$_default_action; } // try to load the controller if (!($controller = static::create($path))) { return false; } return method_exists($controller, static::$_action_prefix . $action); }
/** * Creates a new migrator instance * * @param string $path The path of the migration * @return void */ protected function __construct($path) { $this->path = $path; $this->name = \CCStr::cut(substr(basename($path), 0, strrpos(basename($path), '_')), '.'); }
/** * get the requestet uri * * @param bool $full Don't cut the get params * @return string */ public function uri($full = false) { // check if we already set the current uri if (is_null($this->uri)) { $this->uri = $this->server('REQUEST_URI', '/'); // fix doubled slashes $this->uri = preg_replace('/(\\/+)/', '/', $this->uri); // remove get params if (!$full) { $this->uri = CCStr::cut($this->uri, '?'); } // cut the offset from the config $this->uri = substr($this->uri, strlen(ClanCats::$config->get('url.path'))); // if null or false set to default if (!$this->uri) { $this->uri = ''; } } return $this->uri; }
/** * test string cut */ public function testCut() { $this->assertEquals(CCStr::cut('some/of/my/url/?with=param', '?'), 'some/of/my/url/'); $this->assertEquals(CCStr::cut('some/of/my/url/?with=param', '/'), 'some'); }
/** * Check and complete a route * * @param CCRoute $route * @param mixed $raw_route * @return false|CCRoute */ protected static function configure($route, $raw_route) { // deal with emptiness if (is_null($raw_route)) { return false; } // this might be a controller if (is_string($raw_route)) { // are there overwrite parameters? if (strpos($raw_route, '?') !== false) { $route->params = explode(',', CCStr::suffix($raw_route, '?')); $raw_route = CCStr::cut($raw_route, '?'); } // is there a overwrite action? if (strpos($raw_route, '@') !== false) { $route->action = CCStr::suffix($raw_route, '@'); $raw_route = CCStr::cut($raw_route, '@'); } // try creating an controller instance $controller = CCController::create($raw_route); // set the callback on controller execute $route->callback = array($controller, 'execute'); } elseif (is_callable($raw_route)) { $route->callback = $raw_route; } return $route; }