/** * Check if a session with the given key already exists * * @param string $id The session id key. * @param array $data * @return bool */ public function write($id, $data) { if (!\CCFile::write($this->file_path($id), serialize($data))) { \CCError::exception(new Exception('Could not write session file.')); } return true; }
/** * 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); }
/** * 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)); }
/** * Write the file down to disk * * @return array */ protected function write_file($file, $data) { return CCFile::write($file, '<?php return ' . var_export($data, true) . ';'); }
/** * 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)); }