/** * Add something to the packer * This method allows you to add stylesheets, javascript files etc. * to one single file. * * exmaple: * CCAsset::pack( 'jquery.js', 'footer', 'core' ); * * @param string |array $item An single or an array of assets * @param string $name The holders name like header / theme / footer * @param string $pack The package like core lib etc. * @return void */ public static function pack($item, $name = null, $pack = 'pack') { if (!is_array($item)) { $items = array($item => $name); } $path = PUBLICPATH . static::holder($name)->path; foreach ($items as $file => $holder) { static::holder($name)->assets['_packtacular'][$pack][CCStr::extension($item)][] = $path . $file; } }
/** * Add an asset to the holder. Basically this method checks the * file extension to sort them and generate the correct code using the macros. * * $holder->add( 'jquery.js' ); * $holder->add( 'style.css' ); * $holder->add( '<script>document.write( "Hello World" );</script>' ); * * @param string $item * @return void */ public function add($item) { // when the first character is a "smaller than" we simply assume // that a custom tag has been passed and not a filepath if (strpos($item, "<") !== false) { $macro = '_'; } else { $macro = CCStr::extension($item); } if (!isset($this->assets[$macro])) { $this->assets[$macro] = array(); } $this->assets[$macro][] = $item; }
/** * handle some files * * @param array $files * @param string $dir * @param string $file */ public static function handle($files, $dir, $file) { if (empty($files)) { return null; } $last_change = static::last_change($files); $file = str_replace("{time}", $last_change, $file); // the main needed paths $file = static::$path . $dir . $file; $dir = static::$path . $dir; // does the file already exist? if (file_exists(PUBLICPATH . $file)) { return $file; } // is there a cache dir? if (!is_dir(PUBLICPATH . $dir)) { if (!mkdir(PUBLICPATH . $dir, 0755, true)) { throw new CCException("CCPacktacular - could not create Packtacular folder at: {$dir}"); } } // get the pack file extention $ext = CCStr::extension($file); // get all the content $content = ""; foreach ($files as $tmp_file) { $content .= "\n\n/*\n * file: " . str_replace(PUBLICPATH, '', $tmp_file) . "\n */\n\n"; $content .= file_get_contents($tmp_file); } // call the modifier if (method_exists(get_class(), "handle_" . $ext)) { $content = call_user_func_array("static::handle_" . $ext, array($content)); } // save the file if (!file_put_contents(PUBLICPATH . $file, $content)) { throw new CCException("CCPacktacular - could not create packed file {$file}!"); } // return the path return $file; }
/** * 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); }
/** * Returns the available migrations * * @param string $path * @return array */ public static function get_migrations($path) { $objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST); $files = array(); foreach ($objects as $name => $object) { if (\CCStr::extension($name) == 'sql') { $files[\CCStr::cut(substr(basename($name), strrpos(basename($name), '_') + 1), '.')] = $name; } } ksort($files); return $files; }
/** * test extentsion */ public function testExtension() { $this->assertEquals(CCStr::extension('test.php'), 'php'); $this->assertEquals(CCStr::extension('test.sdf.sdf.md'), 'md'); $this->assertEquals(CCStr::extension('test.sdf.......pdf'), 'pdf'); }