/** * modify the headers to force a download * * @param string $filename * @return void */ public function as_download($filename = null) { if (is_null($filename)) { $filename = 'file.' . CCStr::suffix($this->header('Content-Type'), '/'); } $this->header('Content-Description', 'File Transfer'); $this->header('Content-Disposition', 'attachment; filename=' . $filename); $this->header('Content-Transfer-Encoding', 'binary'); $this->header('Expires', '0'); $this->header('Cache-Control', 'must-revalidate'); $this->header('Pragma', 'public'); $this->header('Content-Length', strlen($this->body())); return $this; }
/** * test suffix */ public function testSuffix() { $this->assertEquals(CCStr::suffix('test.php', '.'), 'php'); $this->assertEquals(CCStr::suffix('Main::Sub', '::'), 'Sub'); $this->assertEquals(CCStr::suffix('ControllerMain', 'Controller'), 'Main'); }
/** * 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; }
/** * Create a image from file * * @param string $file * @param string $type jpg|png|gif * * @return CCImage|false */ public static function create($file, $type = null) { // when no type is given use the file extension if (is_null($type)) { $type = CCStr::extension($file); // validate type if (!in_array($type, static::$available_image_types)) { $type = null; } } $image_data = getimagesize($file); if ($image_data === false) { return false; } $image = null; switch ($image_data['mime']) { case 'image/gif': $image = imagecreatefromgif($file); break; case 'image/jpeg': $image = imagecreatefromjpeg($file); break; case 'image/png': $image = imagecreatefrompng($file); break; default: // we dont support other image types return false; break; } // when the image type is still null we are going to use // the mime type of the image if (is_null($type)) { $type = CCStr::suffix($image_data['mime'], '/'); } return new static($image, $type); }
/** * default help formatter * * @param array $params * @return void */ protected function help_formatter($help = null) { if (is_null($help)) { CCCli::line('Invalid data passed to help formatter.', 'red'); return; } $output = array(); // print the name if (isset($help['name'])) { $output[] = '+-' . str_repeat('-', strlen($help['name'])) . '-+'; $output[] = '| ' . CCCli::color($help['name'], 'light_yellow') . ' |'; $output[] = '+-' . str_repeat('-', strlen($help['name'])) . '-+'; } // description if (isset($help['desc'])) { $output[] = wordwrap(str_replace("\n", "\n" . ' * ', $help['desc']), 60); } // list the actions if (isset($help['actions']) && !empty($help['actions'])) { // for every action in this console controller foreach ($help['actions'] as $action => $attributes) { // print the action $output[] = ''; $output[] = '| ' . CCStr::suffix(get_called_class(), '\\') . '::' . CCCli::color($action, 'cyan'); $output[] = '|'; // for every attribute ( arguments, usage etc. ) foreach ($attributes as $attribute => $options) { $output[] = '| ' . CCCli::color(ucfirst($attribute), 'light_yellow'); // if we just got a string if (is_string($options)) { $output[] = '| ' . CCCli::color($options, 'green'); } elseif (is_array($options)) { // print every option and its description foreach ($options as $option => $description) { $buffer = CCCli::color($option, 'green'); $buffer .= str_repeat(' ', 35 - strlen($buffer)); $buffer .= $description; $buffer = '| ' . $buffer; // is the line to long? if (strlen($buffer) > 80) { $overflow = substr($buffer, 80); $buffer = substr($buffer, 0, 80) . "\n"; $overflow = wordwrap($overflow, 45); $overflow = explode("\n", $overflow); foreach ($overflow as $key => $value) { $overflow[$key] = '| ' . str_repeat(' ', 25) . trim($value); } $buffer .= implode("\n", $overflow); } $output[] = $buffer; } } $output[] = '|'; } array_pop($output); } } return $output; }