Skip to content

Commit

Permalink
Merge remote branch 'kriswallsmith/assetic/dump-fixes'
Browse files Browse the repository at this point in the history
* kriswallsmith/assetic/dump-fixes:
  [AsseticBundle] fixed command unit test
  [AsseticBundle] added env and debug mode to assetic:dump output
  [AsseticBundle] made --watch sleep period a command option
  [AsseticBundle] added a --force option to assetic:dump --watch
  [AsseticBundle] fixed typo in dump --watch
  • Loading branch information
fabpot committed May 17, 2011
2 parents 8e85a36 + f29aba5 commit fdbdcbb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
26 changes: 16 additions & 10 deletions src/Symfony/Bundle/AsseticBundle/Command/DumpCommand.php
Expand Up @@ -36,6 +36,8 @@ protected function configure()
->setDescription('Dumps all assets to the filesystem')
->addArgument('write_to', InputArgument::OPTIONAL, 'Override the configured asset root')
->addOption('watch', null, InputOption::VALUE_NONE, 'Check for changes every second, debug mode only')
->addOption('force', null, InputOption::VALUE_NONE, 'Force an initial generation of all assets (used with --watch)')
->addOption('period', null, InputOption::VALUE_REQUIRED, 'Set the polling period in seconds (used with --watch)', 1)
;
}

Expand All @@ -49,6 +51,10 @@ protected function initialize(InputInterface $input, OutputInterface $output)

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(sprintf('Dumping all <comment>%s</comment> assets.', $input->getOption('env')));
$output->writeln(sprintf('Debug mode is <comment>%s</comment>.', $input->getOption('no-debug') ? 'off' : 'on'));
$output->writeln('');

if (!$input->getOption('watch')) {
foreach ($this->am->getNames() as $name) {
$this->dumpAsset($name, $output);
Expand All @@ -61,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \RuntimeException('The --watch option is only available in debug mode.');
}

$this->watch($output);
$this->watch($input, $output);
}

/**
Expand All @@ -72,25 +78,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
*
* @param OutputInterface $output The command output
*/
private function watch(OutputInterface $output)
private function watch(InputInterface $input, OutputInterface $output)
{
$refl = new \ReflectionClass('Assetic\\AssetManager');
$prop = $refl->getProperty('assets');
$prop->setAccessible(true);

$cache = sys_get_temp_dir().'/assetic_watch_'.substr(sha1($this->basePath), 0, 7);
if (file_exists($cache)) {
$previously = unserialize(file_get_contents($cache));
} else {
if ($input->getOption('force') || !file_exists($cache)) {
$previously = array();
} else {
$previously = unserialize(file_get_contents($cache));
}

$error = '';
while (true) {
try {
foreach ($this->am->getNames() as $name) {
if ($asset = $this->checkAsset($name, $previously)) {
$this->dumpAsset($asset, $output);
if ($this->checkAsset($name, $previously)) {
$this->dumpAsset($name, $output);
}
}

Expand All @@ -101,7 +107,7 @@ private function watch(OutputInterface $output)
file_put_contents($cache, serialize($previously));
$error = '';

sleep(1);
sleep($input->getOption('period'));
} catch (\Exception $e) {
if ($error != $msg = $e->getMessage()) {
$output->writeln('<error>[error]</error> '.$msg);
Expand All @@ -117,7 +123,7 @@ private function watch(OutputInterface $output)
* @param string $name The asset name
* @param array &$previously An array of previous visits
*
* @return AssetInterface|Boolean The asset if it should be dumped
* @return Boolean Whether the asset should be dumped
*/
private function checkAsset($name, array &$previously)
{
Expand All @@ -133,7 +139,7 @@ private function checkAsset($name, array &$previously)

$previously[$name] = array('mtime' => $mtime, 'formula' => $formula);

return $changed ? $asset : false;
return $changed;
}

/**
Expand Down
Expand Up @@ -13,6 +13,7 @@

use Symfony\Bundle\AsseticBundle\Command\DumpCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\NullOutput;

class DumpCommandTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -52,7 +53,10 @@ protected function setUp()
->will($this->returnValue(array()));
$this->definition->expects($this->any())
->method('getOptions')
->will($this->returnValue(array()));
->will($this->returnValue(array(
new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev'),
new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.'),
)));
$this->application->expects($this->any())
->method('getKernel')
->will($this->returnValue($this->kernel));
Expand Down

0 comments on commit fdbdcbb

Please sign in to comment.