Skip to content

Commit

Permalink
Fix and enhance cache clear in ConfigBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
cocolabssas committed Feb 9, 2021
1 parent 69bf9f0 commit 7881411
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/Cocorico/ConfigBundle/Admin/ParameterAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Filesystem\Filesystem;

class ParameterAdmin extends AbstractAdmin
{
Expand All @@ -29,7 +28,7 @@ class ParameterAdmin extends AbstractAdmin
// setup the default sort column and order
protected $datagridValues = array(
'_sort_order' => 'ASC',
'_sort_by' => 'name',
'_sort_by' => 'name'
);

/** @inheritdoc */
Expand All @@ -38,29 +37,28 @@ protected function configureFormFields(FormMapper $formMapper)
/** @var Parameter $parameter */
$parameter = $this->getSubject();


$formMapper
->with(
'admin.parameter.title',
array(
'description' => 'admin.parameters.warning',
'translation_domain' => 'SonataAdminBundle',
'translation_domain' => 'SonataAdminBundle'
)
)
->add(
'name',
null,
array(
'label' => 'admin.parameter.name.label',
'disabled' => true,
'disabled' => true
)
)
->add(
'value',
$parameter ? $parameter->getType() : null,
array(
'label' => 'admin.parameter.value.label',
'required' => false,
'required' => false
)
)
->end();
Expand Down Expand Up @@ -99,7 +97,7 @@ protected function configureListFields(ListMapper $listMapper)
null,
array(
'label' => 'admin.parameter.value.label',
'template' => 'CocoricoConfigBundle::list_parameter_value.html.twig',
'template' => 'CocoricoConfigBundle::list_parameter_value.html.twig'
)
);

Expand All @@ -110,7 +108,7 @@ protected function configureListFields(ListMapper $listMapper)
'actions' => array(
'show' => array(),
'edit' => array(),
),
)
)
);
}
Expand Down Expand Up @@ -165,18 +163,29 @@ private function clearCache()
{
$container = $this->getConfigurationPool()->getContainer();
$kernel = $container->get('kernel');
$php = $container->getParameter('cocorico_config_php_cli_path');

//Clear cache
$rootDir = $kernel->getRootDir();
$command = $php.' '.$rootDir.'/console cache:clear --env='.$kernel->getEnvironment();
$cacheDir = $container->getParameter('kernel.cache_dir');

$file = '';
switch ($kernel->getEnvironment()) {
case 'dev':
$file = 'appDevDebugProjectContainer.php';
break;
case 'staging':
$file = 'appStagingDebugProjectContainer.php';
break;
case 'prod':
$file = 'appProdProjectContainer.php';
break;
}

$process = new Process($command);
try {
$process->mustRun();
$content = $process->getOutput();
} catch (ProcessFailedException $e) {
$content = $e->getMessage();
if ($file) {
$fs = new Filesystem();
$fs->remove($cacheDir . DIRECTORY_SEPARATOR . $file);
$fs->remove($cacheDir . DIRECTORY_SEPARATOR . 'translations');
}
} catch (\Exception $e) {
// $content = $e->getMessage();

return false;
}
Expand Down

0 comments on commit 7881411

Please sign in to comment.