/** * json response * you can pass an array of data wich will be converted to json * * @param array $data * @param int $status * @param bool $beautify should the json output be formatted? * @return CCResponse */ public static function json($data = array(), $status = 200, $beautify = true) { if (!is_array($data)) { throw new CCException("CCResponse::json - first argument has to be an array."); } $response = new static(CCJson::encode($data, $beautify), $status); $response->header('Content-Type', 'text/json'); return $response; }
/** * create an new ship object from path * * @param string $path * @return CCOrbit_Ship */ public static function create($path) { if (is_null($path)) { return new static(); } // use the orbit path if we are relative if (substr($path, 0, 1) != '/') { $path = ORBITPATH . $path; } // check the path if (substr($path, -1) != '/') { $path .= '/'; } $blueprint = $path . 'blueprint.json'; if (!file_exists($blueprint)) { throw new CCException("CCOrbit_Ship::create - clould not find a blueprint at {$blueprint}."); } if (!($blueprint_data = CCJson::read($blueprint))) { throw new CCException("CCOrbit_Ship::create - invalid blueprint at {$blueprint}."); } return static::blueprint($blueprint_data, $path); }
/** * 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'); }
/** * Get the object as json * When $modifiers is true, then the data will be passed trough the modifiers * * @param bool $modifiers * @param bool $beautify * @return string */ public function as_json($modifiers = true, $beautify = true) { return CCJson::encode($this->as_array($modifiers), $beautify); }
/** * 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) { \CCJson::write($this->file_path($id), $data, true); }
/** * 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)); }
/** * Write the file down to disk * * @return array */ protected function write_file($file, $data) { CCJson::write($file, $data, true); }