/** * Delete session that are older than the given time in secounds * * @param int $time * @return void */ public function gc($time) { foreach (\CCFile::ls(\CCStorage::path('sessions/*')) as $file) { if (filemtime($file) - (time() - $time) < 0) { if (!\CCFile::delete($file)) { throw new Exception("Manager_File::gc - cannot delete session file."); } } } }
/** * 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); } }
/** * write the log down to disk * * @return void */ public static function write() { if (empty(static::$_data)) { return; } $buffer = date("H:i:s") . " - " . CCServer::method() . ' ' . CCServer::server('REQUEST_URI') . "\n"; $buffer .= implode("\n", static::$_data); CCFile::append(CCStorage::path('logs/' . date('Y-m') . '/' . date('d') . '.log'), $buffer . "\n"); // clear static::clear(); }
/** * Compile the view * * @param string $builder * @param string $view_file */ public static function compile($builder, $view_file) { if (!isset(static::$_view_builders[$builder])) { throw new CCException("CCView_Builder - view builder '" . $builder . "' is not registerd."); } $builder = static::$_view_builders[$builder]; if (!class_exists($builder)) { throw new CCException("CCView_Builder - invalid view builder '" . $builder . "'."); } $builder = new $builder(CCFile::read($view_file)); return $builder->compile(); }
/** * Builds the phpunit.xml file * * @param array $params */ public function action_build($params) { $test_directories = array(); // check if there is an application tests direcotry if (is_dir(APPPATH . CCDIR_TEST)) { $this->line('found tests in your application.'); $test_directories['App'] = APPPATH . CCDIR_TEST; } // add the core tests if ($params['-include-core']) { $test_directories['Core'] = COREPATH . '../' . CCDIR_TEST; } // check all bundles for tests foreach (\CCFinder::$bundles as $bundle => $path) { if (is_dir($path . CCDIR_TEST)) { $this->info('found tests in the ' . $bundle . ' bundle.'); $test_directories[$bundle] = $path . CCDIR_TEST; } } // we have to remove CCROOT from the paths to get the relative one foreach ($test_directories as $key => $dir) { $test_directories[$key] = './' . str_replace(CCROOT, '', $dir); } // finally generate an phpunit.xml file $xml = new \DOMDocument("1.0", 'UTF-8'); $root = $xml->createElement("phpunit"); $testsuites = $xml->createElement("testsuites"); foreach ($test_directories as $key => $dir) { $testsuite = $xml->createElement("testsuite"); $testsuite->setAttribute('name', $key . ' tests'); $directory = $xml->createElement("directory", $dir); $directory->setAttribute('suffix', EXT); $testsuite->appendChild($directory); $testsuites->appendChild($testsuite); } $root->appendChild($testsuites); $root = $xml->appendChild($root); $root->setAttribute('colors', 'true'); $root->setAttribute('bootstrap', 'boot/phpunit.php'); $xml->formatOutput = true; \CCFile::write(CCROOT . 'phpunit.xml', $xml->saveXML()); }
/** * 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 ships * * exmample: * run shipyard::ship <name> * run shipyard::ship <name> <namespace> * run shipyard::ship <name> --no-namespace * * @param array $params * @return void */ public function action_ship($params) { $options = \CCDataObject::assign($params); $name = $options->get(0, null); $namespace = $options->get(1, null); // get name if we dont have one while (!$name) { $name = CCCli::read('Please enter the ship name: '); } // set namespace if (!$namespace) { $namespace = $name; } // no namespace? if ($params['no-namespace']) { $namespace = false; } // custom target $target = $options->get('target', ORBITPATH); if (substr($target, 0, 1) !== '/') { $target = CCFPATH . $target; } if (substr($target, -1) !== '/') { $target .= '/'; } $target .= $name . '/'; // check if the module is in our orbit path if (is_dir($target)) { if (!CCCli::confirm("there is already a ship with this name. do you want to overwrite?", true)) { return; } } // create the blueprint $defaults = \CCConfig::create('shipyard'); $blueprint = array('name' => $name, 'version' => $defaults->get('defaults.version'), 'description' => $defaults->get('defaults.description'), 'homepage' => $defaults->get('defaults.homepage'), 'keywords' => $defaults->get('defaults.keywords'), 'license' => $defaults->get('defaults.license'), 'authors' => $defaults->get('defaults.authors'), 'namespace' => $namespace); // create file \CCJson::write($target . 'blueprint.json', $blueprint, true); $ship = \CCOrbit_Ship::blueprint($blueprint, $target); // create event files if ($namespace) { // create forge instance $forge = new \CCForge_Php($namespace); // add header $forge->comment($this->make_comment_header($ship->name . ' ship', array('package' => $ship->name, 'authors' => $ship->authors, 'version' => $ship->version, 'copyright' => \CCConfig::create('shipyard')->get('defaults.copyright')))); // add class $forge->closure('class Ship extends \\CCOrbit_Ship', function () { $forge = new \CCForge_Php(); // add init function echo $forge->closure('public function wake()', '// Do stuff', "initialize the ship\n\n" . "@return void"); echo $forge->line(2); // add init function echo $forge->closure('public function install()', '// Do stuff', "install the ship\n\n" . "@return void"); echo $forge->line(2); // add init function echo $forge->closure('public function unsintall()', '// Do stuff', "uninstall the ship\n\n" . "@return void"); }); \CCFile::write($target . CCDIR_CLASS . 'Ship' . EXT, $forge); } else { // create forge instance $forge = new \CCForge_Php(); // add header $forge->comment($this->make_comment_header($ship->name, array('package' => $ship->name, 'authors' => $ship->authors, 'version' => $ship->version, 'copyright' => \CCConfig::create('shipyard')->get('defaults.copyright')))); \CCFile::write($target . 'shipyard/wake' . EXT, $forge); \CCFile::write($target . 'shipyard/install' . EXT, $forge); \CCFile::write($target . 'shipyard/uninstall' . EXT, $forge); } // sucess CCCli::line("'" . $name . "' succesfully created under: " . $target, 'green'); }
/** * Write a file to the storage * * @param string $file * @param string $key * @return string */ public static function write($file, $content, $key = null) { return CCFile::write(static::path($file, $key), $content); }
/** * Send the mail * * @param CCMail $mail The mail object. * @return void * * @throws Mail\Exception */ public function send(CCMail $mail) { $data = $mail->export_data(); $filename = 'mails/' . date('Y-m') . '/' . date('d') . '/' . date("H-i-s") . '-' . \CCStr::clean_url($data['subject']) . '.log'; \CCFile::append(\CCStorage::path($filename), \CCJson::encode($data, true)); }
/** * Save the image * * When you set the file to null the image will be send to the output buffer. * * Examples: * $image = CCImage::create( 'path/to/my/image.jpg' ); * // save to file * $image->save( 'my/new/image.jpg' ); * // quality (80%) and type (gif) * $image->save( 'my/new/image.gif', 'gif', 80 ); * // send to output buffer * $image->save( null ); * * @param string $file * @param int $quality between 1-100 * @param string $type * * @return bool */ public function save($file, $quality = null, $type = null) { $type = $this->set_type($type); // create directory if not exists if (!is_null($file)) { CCFile::mkdir($file); } switch ($type) { // PNG images case 'png': if (is_null($quality)) { $quality = -1; } else { $quality = $quality / 100 * 9; } return imagepng($this->image_context, $file, $quality); break; // GIF images // GIF images case 'gif': return imagegif($this->image_context, $file); break; // JPEG images // JPEG images case 'jpeg': default: if (is_null($quality)) { $quality = 90; } return imagejpeg($this->image_context, $file, $quality); break; } }
/** * write a json file * * @param string $path * @param array $data * @param bool $beautify * @return array|object */ public static function write($path, $array, $beautify = false) { return CCFile::write($path, static::encode($array, $beautify)); }
/** * 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); } }
/** * Write the file down to disk * * @return array */ protected function write_file($file, $data) { return CCFile::write($file, '<?php return ' . var_export($data, true) . ';'); }
/** * uninstall an orbit module * * @param array $params */ public function action_uninstall($params) { $path = $params[0]; if (empty($path)) { CCCli::line('no ship path given.', 'red'); return; } /* * direct install if starting with / */ if (substr($path, 0, 1) == '/') { // fix path if (substr($path, -1) != '/') { $path .= '/'; } // is directory if (!is_dir($path)) { CCCli::line('could not find a ship at path: ' . $path, 'red'); return; } // are ya serius.. if (!CCCli::confirm("are you sure you want to uninstall this ship?", true)) { return; } // run the uninstaller try { \CCOrbit::uninstall($path); } catch (\Exception $e) { CCCli::line($e->getMessage(), 'red'); CCCli::line('ship destroying failure.', 'red'); return; } // also remove the direcoty? if (CCCli::confirm("do you also wish to remove the ship files?", true)) { \CCFile::delete($path); } // we are done CCCli::line('ship destroyed!', 'green'); return; } // check if the module is in our orbit path if (is_dir(ORBITPATH . $path)) { // there is a ship yay CCCli::line('found ship at path: ' . ORBITPATH . $path, 'green'); return static::action_uninstall(array(ORBITPATH . $path)); } // nothing to do here CCCli::line('could not find a ship at this path or name.', 'red'); return; }
/** * Create new migration * * @param array $params * @return void */ public function action_create($params) { $name = array_shift($params); $file = \DB\Migrator::path($name); \CCFile::write($file, "# ---> up\n\n\n\n# ---> down\n\n"); }
/** * Build a view cache file * * @param string $path * @param string $builder * @param string * * @return void */ protected function build_cache($path, $builder, $view_path) { CCFile::write($path, CCView_Builder::compile($builder, $view_path)); }