/** * 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); } }
/** * CCController factory * * @param string $path * @return CCController */ public static function create($path) { $class = static::find($path); // if class already loaded if (!class_exists($class, false)) { // register the controller with the autoloader \CCFinder::bind($class, CCPath::get($path, CCDIR_CONTROLLER, 'Controller' . EXT)); } // create new controller instance and assign the name $controller = new $class(); $controller->name = $path; return $controller; }
/** * Try to generate a security key in the main config file * * @param array $params */ public function action_security_key($params) { $path = \CCPath::config('main.config' . EXT); // Check if the file exists if (!file_exists($path)) { $this->error('Could not find main configuration file.'); return; } // Now try to replace the placeholder with // an new generated key $data = \CCFile::read($path); if (strpos($data, '{{security salt here}}') === false) { $this->error('The key has already been generated or set.'); return; } $data = str_replace('{{security salt here}}', \CCStr::random(32, 'password'), $data); // write the data back down \CCFile::write($path, $data); $this->success('The key has been generated.'); }
/** * generate an controller * * exmample: * run shipyard::controller <controller> * run shipyard::controller <controller> <parent_class> * run shipyard::controller <namespace>::<controller> * * @param array $params * @return void */ public function action_controller($params) { $options = \CCDataObject::assign($params); $name = $options->get(0, null); $parent = $options->get(1, null); // get name if we dont have one while (!$name) { $name = $this->read('Please enter the controller name: '); } // fix controller suffix if (substr($name, strlen('Controller') * -1) != 'Controller') { $name .= 'Controller'; } // try to resolve the path if (!($path = \CCPath::controllers(str_replace('_', '/', $name), EXT))) { $this->error('Could not resolve the path. Check if the namespace is registered.'); return; } // parent if (is_null($parent)) { $parent = '\\CCController'; } // view controller if ($options->get('view', false)) { $parent = '\\CCViewController'; } // create the class $class = \CCShipyard::create('class', $name, $parent); // get the actions $actions = array('index'); if ($options->get('actions', false)) { $actions = array_merge($actions, explode(',', $options->get('actions'))); } foreach ($actions as $action) { $action = trim($action); $class->add('function', 'action_' . $action, 'protected', 'echo "' . $name . ' ' . $action . ' action";', ucfirst($action) . " action\n@return void|CCResponse"); $class->add('line', 2); } // add static init if (!$options->get('no-events', false)) { $class->add('function', 'wake', 'protected', '//', "Controller wake\n@return void|CCResponse"); $class->add('line', 2); $class->add('function', 'sleep', 'protected', '//', "Controller wake\n@return void"); } // check for overwrite if (file_exists($path)) { if (!$this->confirm("The class already exists. Do you wish to overwrite it?", true)) { return; } } // write file \CCFile::write($path, $class->output()); }
/** * Load a language file into the appliaction * * CCLang::load( 'some/path/to/file' ); * CCLang::load( 'controller/example' ); * CCLang::load( 'Blog::controller/dashboard' ); * * @param string $path * @param bool $overwrite * @return void */ public static function load($path, $overwrite = false) { if (array_key_exists($path, static::$data[static::$current_language]) && $overwrite === false) { return; } $file_path = CCPath::get($path, CCDIR_LANGUAGE . static::$current_language . '/', EXT); if (!file_exists($file_path)) { // as fallback try to load the language file of the default language if (static::$current_language !== ($default_lang = ClanCats::$config->get('language.default'))) { $file_path = CCPath::get($path, CCDIR_LANGUAGE . $default_lang . '/', EXT); if (!file_exists($file_path)) { throw new CCException("CCLang::load - could not find language file: " . $file_path); } } else { throw new CCException("CCLang::load - could not find language file: " . $file_path); } } static::$data[static::$current_language][$path] = (require $file_path); }
<?php /* *--------------------------------------------------------------- * Database configuration for phpunit *--------------------------------------------------------------- */ return array('main' => 'app', 'app' => array('db' => 'ccf2_phpunit_application', 'driver' => 'mysql', 'host' => '127.0.0.1', 'user' => 'root', 'pass' => '', 'charset' => 'utf8'), 'phpunit' => array('db' => 'ccf2_phpunit_database', 'driver' => 'mysql', 'host' => '127.0.0.1', 'user' => 'root', 'pass' => '', 'charset' => 'utf8'), 'phpunit_sqlite' => array('driver' => 'sqlite', 'path' => CCPath::get('CCUnit::test.db'), 'charset' => 'utf8'));
/** * Returns the available migrations * * @return array */ public static function available() { $bundles = array_merge(\CCFinder::$bundles, array('app' => \CCPath::get('', null))); $available = array(); foreach ($bundles as $name => $path) { $directory = $path . \ClanCats::directory('migration'); if (is_dir($directory)) { $available[strtolower($name)] = static::get_migrations($directory); } } return $available; }
/** * Write the file down * * @return bool */ public function write() { // resolve the path if (!($path = \CCPath::classes(str_replace('_', '/', $this->name), EXT))) { throw new CCException('Could not resolve the class path. Check if the namespace is registered.'); } }
/** * Run a console script * * @param string $controller * @param string $action * @param array $params */ public static function run($controller, $action = null, $params = array()) { // always enable the file infos // this allows CCFile to print an info when a file gets created or deleted. CCFile::enable_infos(); // execute by default the help action if (empty($action)) { $action = 'default'; } $path = CCPath::get($controller, CCDIR_CONSOLE, EXT); // check if the file exists, if not try with core path if (!file_exists($path)) { if (!CCPath::contains_namespace($controller)) { $path = CCPath::get(CCCORE_NAMESPACE . '::' . $controller, CCDIR_CONSOLE, EXT); } } // still nothing? if (!file_exists($path)) { CCCli::line("Could not find controller {$controller}.", 'red'); return false; } // all console classes should be on the CCConsole namespace // this way you can easly overwrite a console script $class = 'CCConsole\\' . $controller; // add the class to the autoloader \CCFinder::bind($class, $path); // create an instance $class = new $class($action, $params); // run wake function if (method_exists($class, 'wake')) { call_user_func(array($class, 'wake'), $params); } // run the execution call_user_func(array($class, '_execute'), $action, $params); // run sleep if (method_exists($class, 'sleep')) { call_user_func(array($class, 'sleep'), $params); } }
/** * generates a path for a config name * * @param string $name * @param bool $env */ protected function path($name, $env = false) { $conf = CCPath::get($name, \CCDIR_CONFIG, static::EXT); if (!$env) { return $conf; } $env_conf = CCPath::get($name, \CCDIR_CONFIG . ClanCats::environment() . '/', static::EXT); if (file_exists($env_conf)) { return $env_conf; } return $conf; }
/** * Get the path to real view file * * This function will also build the cache view file if needed. * * @param string $path * @return string */ protected function view_path($view) { if (isset(static::$_view_paths[$view])) { return static::$_view_paths[$view]; } // view is empty? if (is_null($view)) { throw new CCException("CCView - cannot render view without a view file."); } // generate the views path $path = CCPath::get($view, CCDIR_VIEW, EXT); if (!file_exists($path)) { throw new CCException("CCView - could not find view: " . $view . " at: {$path}."); } // does the view implement a view builder? if (strpos($view, '.') !== false) { $cache = static::cache_path($view); // does the cache not exits or is out of date if (!file_exists($cache) || filemtime($cache) < filemtime($path)) { list($view_name, $builder) = explode('.', $view); $this->build_cache($cache, $builder, $path); } $path = $cache; } return static::$_view_paths[$view] = $path; }