Skip to content

Instantly share code, notes, and snippets.

@samdark
Created April 5, 2013 10:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samdark/5318196 to your computer and use it in GitHub Desktop.
Save samdark/5318196 to your computer and use it in GitHub Desktop.
Yii command to clear assets
<?php
/**
* Clear assets Command
*
* Deletes all published assets.
*
* Usage:
* yiic clearassets
*
* @author Alexander Makarov
*/
class ClearAssetsCommand extends CConsoleCommand {
/**
* Executes the command.
* @param array command line parameters for this command.
*/
public function run($args)
{
$path = Yii::app()->getAssetManager()->getBasePath();
$di = new DirectoryIterator($path);
foreach($di as $d)
{
if(!$d->isDot())
{
echo "Removed ".$d->getPathname()."\n";
$this->removeDirRecursive($d->getPathname());
}
}
echo "Done.\n";
}
function removeDirRecursive($dir)
{
$files = glob($dir.'*', GLOB_MARK);
foreach ($files as $file)
{
if (is_dir($file)){
$this->removeDirRecursive($file);
}
else {
unlink($file);
}
}
if (is_dir($dir)) rmdir($dir);
}
}
@slaFFik
Copy link

slaFFik commented Nov 15, 2013

exception 'CException' with message 'CConsoleApplication and its behaviors do not have a method
or closure named "getAssetManager"

@dimanus
Copy link

dimanus commented Nov 21, 2013

$path = '../assets/'; //Yii::app()->getAssetManager()->getBasePath();
that`s work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment