app/AppKernel.php line 67

Open in your IDE?
  1. <?php
  2. use AppBundle\Common\ExtensionManager;
  3. use Codeages\Biz\Framework\Context\Biz;
  4. use Codeages\Biz\Framework\Provider\DoctrineServiceProvider;
  5. use Codeages\Biz\Framework\Provider\MonologServiceProvider;
  6. use Codeages\PluginBundle\System\PluginableHttpKernelInterface;
  7. use Codeages\PluginBundle\System\PluginConfigurationManager;
  8. use Symfony\Bundle\MakerBundle\MakerBundle;
  9. use Symfony\Component\Config\Loader\LoaderInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Kernel;
  12. use Topxia\Service\Common\ServiceKernel;
  13. class AppKernel extends Kernel implements PluginableHttpKernelInterface
  14. {
  15.     protected $plugins = array();
  16.     /**
  17.      * @var Request
  18.      */
  19.     protected $request;
  20.     protected $extensionManger;
  21.     private $isServiceKernelInit false;
  22.     protected $pluginConfigurationManager;
  23.    public function __construct($environment$debug)
  24.     {
  25.         parent::__construct($environment$debug);
  26.         date_default_timezone_set('Asia/Shanghai');
  27.         $this->extensionManger ExtensionManager::init($this);
  28.         $this->pluginConfigurationManager = new PluginConfigurationManager($this->getRootDir());
  29.     }
  30.     /**
  31.      * @deprecated since Symfony 4.2, use getProjectDir() instead
  32.      */
  33.     public function getRootDir()
  34.     {
  35.         return $this->getProjectDir() . '/app';
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function getLogDir()
  41.     {
  42.         return $this->getProjectDir().'/app/logs';
  43.     }
  44.     public function boot()
  45.     {
  46.         if (true === $this->booted) {
  47.             return;
  48.         }
  49.         // init bundles
  50.         $this->initializeBundles();
  51.         // init container
  52.         $this->initializeContainer();
  53.         $this->initializeBiz($this->getContainer()->get('biz'));
  54.         $this->initializeServiceKernel();
  55.         foreach ($this->getBundles() as $bundle) {
  56.             $bundle->setContainer($this->container);
  57.             $bundle->boot();
  58.         }
  59.         $this->booted true;
  60.     }
  61.     public function registerBundles()
  62.     {
  63.         $bundles = array(
  64.             new Codeages\PluginBundle\FrameworkBundle(),
  65.             new Symfony\Bundle\SecurityBundle\SecurityBundle(),
  66.             new Symfony\Bundle\TwigBundle\TwigBundle(),
  67.             new Symfony\Bundle\MonologBundle\MonologBundle(),
  68.             new Twig\Extra\TwigExtraBundle\TwigExtraBundle(),
  69.             new Topxia\WebBundle\TopxiaWebBundle(),
  70.             new Topxia\AdminBundle\TopxiaAdminBundle(),
  71.             new Topxia\MobileBundleV2\TopxiaMobileBundleV2(),
  72.             new Bazinga\Bundle\JsTranslationBundle\BazingaJsTranslationBundle(),
  73.             new OAuth2\ServerBundle\OAuth2ServerBundle(),
  74.             new Codeages\PluginBundle\CodeagesPluginBundle(),
  75.             new AppBundle\AppBundle(),
  76.             new CorporateTrainingBundle\CorporateTrainingBundle(),
  77.             new CustomBundle\CustomBundle(),
  78.             new ApiBundle\ApiBundle(),
  79.             new ApiV3Bundle\ApiV3Bundle(),
  80.             new MakerBundle,
  81.         );
  82.         if ('test' !== $this->getEnvironment()) {
  83.             $plugins $this->pluginConfigurationManager->getInstalledPlugins();
  84.             foreach ($plugins as $plugin) {
  85.                 if ('plugin' != $plugin['type']) {
  86.                     continue;
  87.                 }
  88.                 if (!= $plugin['protocol']) {
  89.                     continue;
  90.                 }
  91.                 $code ucfirst($plugin['code']);
  92.                 $class "{$code}Plugin\\{$code}Plugin";
  93.                 $bundles[] = new $class();
  94.             }
  95.         }
  96.         if (in_array($this->getEnvironment(), array('dev''test'))) {
  97.             if (class_exists('Symfony\Bundle\WebProfilerBundle\WebProfilerBundle')) {
  98.                 $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
  99.             }
  100.         }
  101.         return $bundles;
  102.     }
  103.     public function registerContainerConfiguration(LoaderInterface $loader)
  104.     {
  105.         $loader->load(__DIR__ '/config/config_' $this->getEnvironment() . '.yml');
  106.     }
  107.     public function getPlugins()
  108.     {
  109.         return $this->pluginConfigurationManager->getInstalledPlugins();
  110.     }
  111.     public function getPluginConfigurationManager()
  112.     {
  113.         return $this->pluginConfigurationManager;
  114.     }
  115.     public function setRequest(Request $request)
  116.     {
  117.         $this->request $request;
  118.         return $this;
  119.     }
  120.     public function initializeBiz(Biz $biz)
  121.     {
  122.         $biz['migration.directories'][] = dirname(__DIR__) . '/migrations';
  123.         $biz['env'] = array(
  124.             'base_url' => $this->request->getSchemeAndHttpHost() . $this->request->getBasePath(),
  125.         );
  126.         $biz->register(new DoctrineServiceProvider());
  127.         $biz->register(new MonologServiceProvider(), array(
  128.             'monolog.logfile' => $this->getContainer()->getParameter('kernel.logs_dir') . '/biz.log',
  129.             'monolog.level' => $this->isDebug() ? \Monolog\Logger::DEBUG : \Monolog\Logger::INFO,
  130.             'monolog.permission' => 0666,
  131.         ));
  132.         $biz->register(new \Codeages\Biz\Framework\Provider\SchedulerServiceProvider());
  133.         $biz->register(new \Codeages\Biz\Framework\Provider\TargetlogServiceProvider());
  134.         $biz->register(new \Biz\DefaultServiceProvider());
  135.         $biz->register(new \Biz\DefaultSdkProvider());
  136.         $collector $this->getContainer()->get('biz.service_provider.collector');
  137.         foreach ($collector->all() as $provider) {
  138.             $biz->register($provider);
  139.         }
  140.         $biz->register(new Codeages\Biz\RateLimiter\RateLimiterServiceProvider());
  141.         $this->registerCacheServiceProvider($biz);
  142.         $biz->register(new \Biz\Accessor\AccessorServiceProvider());
  143.         $this->registerSessionServiceProvider($biz);
  144.         $biz->register(new \Codeages\Biz\Framework\Provider\QueueServiceProvider());
  145.         $biz->boot();
  146.         $activeTheme $this->pluginConfigurationManager->getActiveThemeName();
  147.         if (empty($activeTheme)) {
  148.             $this->pluginConfigurationManager->setActiveThemeName('jianmo')->save();
  149.         }
  150.         $biz['pluginConfigurationManager'] = $this->pluginConfigurationManager;
  151.     }
  152.     protected function registerSessionServiceProvider($biz)
  153.     {
  154.         if ($this->getContainer()->hasParameter('redis_host')) {
  155.             $biz->register(
  156.                 new \Codeages\Biz\Framework\Provider\SessionServiceProvider(),
  157.                 array(
  158.                     'session.options' => array(
  159.                         'max_life_time' => 7200,
  160.                         'session_storage' => 'redis'// exapmle: db, redis
  161.                     ),
  162.                 )
  163.             );
  164.         } else {
  165.             $biz->register(new \Codeages\Biz\Framework\Provider\SessionServiceProvider());
  166.         }
  167.     }
  168.     protected function registerCacheServiceProvider($biz)
  169.     {
  170.         if ($this->getContainer()->hasParameter('redis_host')) {
  171.             $biz->register(
  172.                 new Codeages\Biz\Framework\Provider\RedisServiceProvider(),
  173.                 array(
  174.                     'redis.options' => array(
  175.                         'host' => $this->getContainer()->getParameter('redis_host'),
  176.                         'timeout' => $this->getContainer()->getParameter('redis_timeout'),
  177.                         'reserved' => $this->getContainer()->getParameter('redis_reserved'),
  178.                         'redis_interval' => $this->getContainer()->getParameter('redis_retry_interval'),
  179.                         'password' => $this->getContainer()->hasParameter('redis_password') ? $this->getContainer()->getParameter('redis_password') : '',
  180.                     ),
  181.                     'dao.cache.enabled' => true,
  182.                 )
  183.             );
  184.         }
  185.     }
  186.     protected function initializeServiceKernel()
  187.     {
  188.         if (!$this->isServiceKernelInit) {
  189.             $container $this->getContainer();
  190.             $biz $container->get('biz');
  191.             $serviceKernel ServiceKernel::create($this->getEnvironment(), $this->isDebug());
  192.             $currentUser = array(
  193.                 'currentIp' => $this->request->getClientIp() ?: '127.0.0.1',
  194.                 'isSecure' => $this->request->isSecure(),
  195.                 'invitedCode' => '',
  196.             );
  197.             $currentUser = new \Biz\User\AnonymousUser($currentUser);
  198.             $biz['user'] = $currentUser;
  199.             $serviceKernel
  200.                 ->setBiz($biz)
  201.                 ->setCurrentUser($currentUser)
  202.                 ->setEnvVariable(
  203.                     array(
  204.                         'host' => $this->request->getHttpHost(),
  205.                         'schemeAndHost' => $this->request->getSchemeAndHttpHost(),
  206.                         'basePath' => $this->request->getBasePath(),
  207.                         'baseUrl' => $this->request->getSchemeAndHttpHost() . $this->request->getBasePath(),
  208.                     )
  209.                 )
  210.                 ->setTranslatorEnabled(true)
  211.                 ->setTranslator($container->get('translator'))
  212.                 ->setParameterBag($container->getParameterBag())
  213.                 ->registerModuleDirectory(dirname(__DIR__) . '/plugins');
  214.             $this->isServiceKernelInit true;
  215.         }
  216.     }
  217.     public function getCacheDir()
  218.     {
  219.         $theme $this->pluginConfigurationManager->getActiveThemeName();
  220.         $theme = empty($theme) ? '' ucfirst(str_replace('-''_'$theme));
  221.         return $this->getRootDir() .'/cache/' $this->environment '/' $theme;
  222.     }
  223. }