Created
April 4, 2013 21:31
-
-
Save adibMosharrof/5314551 to your computer and use it in GitHub Desktop.
PHP Laravel fetch.php task by jeffrey way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Asset { | |
//Downloads the content of the file from internet | |
public function fetch( $assetPath ) { | |
return file_get_contents( $assetPath ); | |
} | |
} | |
class Fetch_Task { | |
public static $paths = array( | |
'jquery' =>'http://code.jquery.com/jquery.js', | |
'backbone'=>'http://backbonejs.org/backbone.js', | |
'underscore'=>'http://underscorejs.org/underscore.js', | |
'normalize' => 'https://raw.github.com/necolas/normalize.css/master/normalize.css', | |
'generate' =>'https://raw.github.com/JeffreyWay/Laravel-Generator/master/generate.php' | |
); | |
public static $cssDir = 'public/css/vendors/'; | |
public static $jsDir = 'public/js/vendors/'; | |
public static $tasksDir = 'application/tasks/'; | |
public function __construct( Asset $file=null ) { | |
$this->file = is_null( $file ) ? ( new Asset ) : $file; | |
} | |
public function run( $query = [] ) { | |
if ( !$query ) { | |
throw new InvalidArgumentException( 'Please pass an asset to download' ); | |
} | |
$this->asset = strtolower( $query[0] ); | |
// if recognized, then fetch it and create the file; | |
if ( $this->recognizesAsset( $this->asset ) ) { | |
// then fetch it and create file | |
$content = $this->file->fetch( static::$paths[$this->asset] ); | |
$this->createFile( $this->asset, $content ); | |
echo "Your asset has been genereated" . PHP_EOL; | |
}else { | |
// echo not found to the user | |
echo "The {$query[0]} key was not recognized" . PHP_EOL; | |
} | |
} | |
//Checks to see js or css | |
public function recognizesAsset( $asset ) { | |
return array_key_exists( $asset, static::$paths ); | |
} | |
//Creates the file and saves in directory | |
public function createFile( $asset, $content ) { | |
$file = pathinfo( static::$paths[$asset] ); | |
switch($file['extension']){ | |
case 'js': | |
$path = static::$jsDir . $file['basename']; | |
break; | |
case 'css': | |
$path = static::$cssDir . $file['basename']; | |
break; | |
case 'php': | |
$path = static::$tasksDir . $file['basename']; | |
break; | |
default: | |
} | |
File::mkdir( dirname( $path ) ); | |
File::put( $path, $content ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment