<?php
namespace AppBundle\Controller\Admin\Master;
use AppBundle\Component\PageBuilder\PageBuilder;
use AppBundle\Controller\Admin\Base\MasterArticleBaseController;
use AppBundle\Entity\Master\Language;
use AppBundle\PageForm\MasterLanguageForm;
use Monolog\Logger;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class MasterLanguageController extends MasterArticleBaseController implements EventSubscriberInterface
{
protected $form_class = MasterLanguageForm::class;
protected $models = ['item' => Language::class];
private $commandAfterResponse = false;
public function before(FilterControllerEvent $event)
{
set_time_limit(60 * 30);
parent::before($event); // TODO: Change the autogenerated stub
}
public function deleteAction(Request $request)
{
$response = parent::deleteAction($request); // TODO: Change the autogenerated stub
$this->editConfigLingual();
return $response;
}
protected function initEntitySettings($name = null)
{
$this->entity_settings
->setTitle('言語設定')
->setBaseRoute('admin_master_master_language')
->assignActions([
'sort' => true
])
->assignPublishOptions([
'public_status' => false,
'public_period' => false
])
->setDefaultPrivilege('admin_master_master_language_edit');
}
protected function pageBuilderOnLoad(PageBuilder $builder)
{
$choices = ['' => 'なし'];
$repo = $this->getDoctrine()->getRepository(Language::class);
$entities = $repo->findBy([], ['sort_num' => 'ASC']);
foreach ($entities as $entity) {
$choices[$entity->getTranslateKey()] = $entity->getTitle();
}
$targetBlock = $builder->findBlockByName('target_key');
$targetBlock->setChoices($choices);
return $builder;
}
protected function getDatatableColumns()
{
$column = [];
$column[] = [
"sTitle" => "タイトル",
"mData" => "title",
"@render" => "function(data, type, row){ return ('' == data)? '<strong>(無題)</strong>':'<strong>' + data + '</strong>'; }"
];
$column[] = [
"sTitle" => "エイリアス",
"mData" => "alias"
];
return $column;
}
protected function save($em, $entity, $formData, $mode = self::SAVE_MODE_NEW, $options = [])
{
$entity = parent::save($em, $entity, $formData, $mode, $options); // TODO: Change the autogenerated stub
$this->editConfigLingual();
return $entity;
}
protected function editConfigLingual() {
try{
$repo = $this->getDoctrine()->getRepository(Language::class);
$languages = $repo->findBy([], ['sort_num' => 'ASC']);
$locales = [];
foreach ($languages as $language) {
$locales[] = $language->getAlias();
}
if (!empty($locales)){
$text = 'parameters:' . "\n";
$text .= ' app.locales: ' . implode("|", $locales);
$configPath = CMS__ADMIN_DIR . 'cms' . DS . 'app' . DS . 'config' . DS . 'config_lingual.yml';
file_put_contents($configPath, $text);
$this->commandAfterResponse = true;
}
}
catch (\Exception $e){
$this->addFlash('error', 'config_lingual.ymlに書き込めませんでした。手動で設定してください。');
}
}
protected function runCommand(ArrayInput $input)
{
$app = new Application($this->get('kernel'));
$app->setAutoExit(false);
$output = new BufferedOutput();
$exitCode = $app->run($input, $output);
return $output->fetch();
}
public function forceCacheClear(PostResponseEvent $event)
{
set_time_limit(60 * 3);
if ($this->commandAfterResponse === false) {
return '';
}
/** @var Logger $logger */
$logger = $this->container->get('monolog.logger.custom');
$logger->info('command cache clear start');
$log = '';
$commands = [];
$commands[] = new ArrayInput([
'command' => 'cache:clear',
'--no-interaction' => true,
'-v' => true
]);
foreach($commands as $command){
/** ArrayInput $command */
$log .= (string)$command . \PHP_EOL;
$log .= $this->runCommand($command);
}
$logger->info($log);
$logger->info('command cache clear finish');
$this->commandAfterResponse = false;
return $log;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [KernelEvents::TERMINATE => 'forceCacheClear'];
}
}