From cab787cb2901a5f3124eaa733287ac43209aea48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81ro=CC=82me=20Nadaud?= Date: Fri, 25 Nov 2016 16:36:36 +0100 Subject: [PATCH] Add cache/logs command to console --- config/dev.php | 5 +++-- config/prod.php | 3 ++- src/console.php | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/config/dev.php b/config/dev.php index a8a9f76..d7da37a 100644 --- a/config/dev.php +++ b/config/dev.php @@ -8,11 +8,12 @@ // enable the debug mode $app['debug'] = true; +$app['logs.path'] = __DIR__.'/../var/logs/'; $app->register(new MonologServiceProvider(), array( - 'monolog.logfile' => __DIR__.'/../var/logs/silex_dev.log', + 'monolog.logfile' => $app['logs.path'].'silex_dev.log', )); $app->register(new WebProfilerServiceProvider(), array( - 'profiler.cache_dir' => __DIR__.'/../var/cache/profiler', + 'profiler.cache_dir' => $app['cache.path'].'profiler', )); diff --git a/config/prod.php b/config/prod.php index 26a5a51..e84db60 100644 --- a/config/prod.php +++ b/config/prod.php @@ -2,5 +2,6 @@ // configure your app for the production environment +$app['cache.path'] = __DIR__.'/../var/cache/'; $app['twig.path'] = array(__DIR__.'/../templates'); -$app['twig.options'] = array('cache' => __DIR__.'/../var/cache/twig'); +$app['twig.options'] = array('cache' => $app['cache.path'].'twig'); diff --git a/src/console.php b/src/console.php index 465e14f..5d2c0c0 100644 --- a/src/console.php +++ b/src/console.php @@ -5,6 +5,8 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; $console = new Application('My Silex Application', 'n/a'); $console->getDefinition()->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev')); @@ -20,4 +22,34 @@ }) ; +if (isset($app['cache.path'])) { + $console + ->register('cache:clear') + ->setDescription('Clears the cache') + ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) { + $cacheDir = $app['cache.path']; + $finder = Finder::create()->in($cacheDir)->notName('.gitkeep'); + $filesystem = new Filesystem(); + $filesystem->remove($finder); + + $output->writeln(sprintf('%s success', 'cache:clear')); + }) + ; +} + +if (isset($app['logs.path'])) { + $console + ->register('logs:clear') + ->setDescription('Clears the logs') + ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) { + $cacheDir = $app['logs.path']; + $finder = Finder::create()->in($cacheDir)->notName('.gitkeep'); + $filesystem = new Filesystem(); + $filesystem->remove($finder); + + $output->writeln(sprintf('%s success', 'logs:clear')); + }) + ; +} + return $console;