src/AppBundle/Controller/Admin/Master/Maintenance/SystemController.php line 19

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller\Admin\Master\Maintenance;
  3. use AppBundle\Controller\Admin\Base\MasterBaseController;
  4. use Monolog\Logger;
  5. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Console\Input\ArrayInput;
  10. use Symfony\Component\Console\Output\BufferedOutput;
  11. use Symfony\Bundle\FrameworkBundle\Console\Application;
  12. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  13. use Symfony\Component\Finder\Finder;
  14. use Symfony\Component\Filesystem\Filesystem;
  15. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. class SystemController extends MasterBaseController implements EventSubscriberInterface
  18. {
  19.     private $commandAfterResponse false;
  20.     private $params;
  21.     public function before(FilterControllerEvent $event)
  22.     {
  23.         $user $this->getUser();
  24.         if(!$user->isSystemAdmin()){
  25.             throw $this->createAccessDeniedException();
  26.         }
  27.         parent::before($event);
  28.     }
  29.     public function indexAction(Request $request)
  30.     {
  31.         $bundles = [];
  32.         $bundles[] = 'AppBundle';
  33.         $bundles[] = 'CustomBundle';
  34.         $metaList $this->container->getParameter('kernel.bundles_metadata');
  35.         foreach($metaList as $meta) {
  36.             $namespace $meta['namespace'];
  37.             if(preg_match('/^Mod\\\\/'$namespace)){
  38.                 $bundles[] = $namespace;
  39.             }
  40.         }
  41.         if ($request->isMethod('post')) {
  42.             $this->commandAfterResponse true;
  43.             $this->params $request->request->all();
  44.             $this->addFlash('success''コマンド実行しました。');
  45.         }
  46.         return $this->render('::admin/master/maintenance/system/index.html.php', [
  47.             'bundles' => $bundles
  48.         ]);
  49.     }
  50.     public function commandAction(PostResponseEvent $event)
  51.     {
  52.         set_time_limit(60 3);
  53.         if ($this->commandAfterResponse === false) {
  54.             return '';
  55.         }
  56.         $action $this->params['action'];
  57.         $options = !empty($this->params['options'])?$this->params['options'] : [];
  58.         $commands = [];
  59.         $log '';
  60.         /** @var Logger $logger */
  61.         $logger $this->container->get('monolog.logger.custom');
  62.         $logger->info('command ' $action ' start');
  63.         switch($action){
  64.             case 'cache:clear':
  65.                 $commands[] = new ArrayInput([
  66.                     'command' => 'cache:clear',
  67.                     '-v' => true
  68.                 ]);
  69.                 break;
  70.             case 'schema:update':
  71.                 $params = [
  72.                     'command' => 'app:schema:update',
  73.                     '-v' => true
  74.                 ];
  75.                 if(!empty($options['bundle'])){
  76.                     $params['--bundle'] = $options['bundle'];
  77.                 }
  78.                 $commands[] = new ArrayInput($params);
  79.                 $commands[] = new ArrayInput([
  80.                     'command' => 'cache:clear',
  81.                     '-v' => true
  82.                 ]);
  83.                 break;
  84.             case 'migrate':
  85.                 $log .= $this->forceMigrate();
  86.                 break;
  87.             case 'run:upload:queue':
  88.                 $commands[] = new ArrayInput([
  89.                     'command' => 'run:upload:queue'
  90.                 ]);
  91.                 break;
  92.             default:
  93.                 $input null;
  94.         }
  95.         foreach($commands as $command){
  96.             /** ArrayInput $command */
  97.             $log .= (string)$command . \PHP_EOL;
  98.             $log .= $this->runCommand($command);
  99.         }
  100.         $this->commandAfterResponse false;
  101.         $logger->info($log);
  102.         $logger->info('command ' $action ' finish');
  103.         return $log;
  104.     }
  105.     /**
  106.      * アプリケーションコマンドの実行
  107.      *
  108.      * @param ArrayInput $input
  109.      * @return string
  110.      * @throws \Exception
  111.      */
  112.     protected function runCommand(ArrayInput $input)
  113.     {
  114.         $app = new Application($this->get('kernel'));
  115.         $app->setAutoExit(false);
  116.         $output = new BufferedOutput();
  117.         $exitCode $app->run($input$output);
  118.         return $output->fetch();
  119.     }
  120.     /**
  121.      * DoctrineMigrations を初期化し、マイグレーションを実行
  122.      *
  123.      * @return string
  124.      * @throws \Exception
  125.      */
  126.     public function forceMigrate()
  127.     {
  128.         $log '';
  129.         $doctrine $this->container->get('doctrine');
  130.         $em $doctrine->getManager();
  131.         $finder = new Finder();
  132.         $fs = new Filesystem();
  133.         $root $this->container->get('kernel')->getRootDir();
  134.         $dir $root '/DoctrineMigrations';
  135.         $migrations $finder->files()->in($dir);
  136.         foreach($migrations as $file){
  137.             $fs->remove($file);
  138.         }
  139.         $connection $em->getConnection();
  140.         $platform   $connection->getDatabasePlatform();
  141.         $connection->executeUpdate($platform->getTruncateTableSQL('migration_versions'true));
  142.         $log .= $this->runCommand(new ArrayInput([
  143.             'command' => 'doctrine:migration:diff',
  144.             '--no-interaction' => true,
  145.             '-v' => true,
  146.         ]));
  147.         $log .= $this->runCommand(new ArrayInput([
  148.             'command' => 'doctrine:migration:migrate',
  149.             '--no-interaction' => true,
  150.             '-v' => true,
  151.         ]));
  152.         $migrations $finder->files()->in($dir);
  153.         foreach($migrations as $file){
  154.             $fs->remove($file);
  155.         }
  156.         $connection->executeUpdate($platform->getTruncateTableSQL('migration_versions'true));
  157.         return $log;
  158.     }
  159.     /**
  160.      * {@inheritdoc}
  161.      */
  162.     public static function getSubscribedEvents()
  163.     {
  164.         return [KernelEvents::TERMINATE => 'commandAction'];
  165.     }
  166. }