app/AppKernel.php line 346

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\Component\Config\Loader\LoaderInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Kernel;
  11. use Topxia\Service\Common\ServiceKernel;
  12. class AppKernel extends Kernel implements PluginableHttpKernelInterface
  13. {
  14.     protected $plugins = [];
  15.     /**
  16.      * @var Request
  17.      */
  18.     protected $request;
  19.     protected $extensionManger;
  20.     private $isServiceKernelInit false;
  21.     protected $pluginConfigurationManager;
  22.     private $requestStackSize 0;
  23.     private $resetServices false;
  24.     public function __construct($environment$debug)
  25.     {
  26.         parent::__construct($environment$debug);
  27.         date_default_timezone_set('Asia/Shanghai');
  28.         $this->extensionManger ExtensionManager::init($this);
  29.         $this->pluginConfigurationManager = new PluginConfigurationManager($this->getRootDir());
  30.     }
  31.     public function boot()
  32.     {
  33.         if (true === $this->booted) {
  34.             if (!$this->requestStackSize && $this->resetServices) {
  35.                 if ($this->container->has('services_resetter')) {
  36.                     $this->container->get('services_resetter')->reset();
  37.                 }
  38.                 $this->resetServices false;
  39.                 if ($this->debug) {
  40.                     $this->startTime microtime(true);
  41.                     error_reporting(3);
  42.                 }
  43.             }
  44.             return;
  45.         }
  46.         if ($this->debug) {
  47.             $this->startTime microtime(true);
  48.             error_reporting(3);
  49.         }
  50.         if ($this->debug && !isset($_ENV['SHELL_VERBOSITY']) && !isset($_SERVER['SHELL_VERBOSITY'])) {
  51.             putenv('SHELL_VERBOSITY=3');
  52.             $_ENV['SHELL_VERBOSITY'] = 3;
  53.             $_SERVER['SHELL_VERBOSITY'] = 3;
  54.         }
  55. //        if ($this->loadClassCache) {
  56. //            $this->doLoadClassCache($this->loadClassCache[0], $this->loadClassCache[1]);
  57. //        }
  58.         // init bundles
  59.         $this->initializeBundles();
  60.         // init container
  61.         $this->initializeContainer();
  62.         $this->initializeBiz($this->getContainer()->get('biz'));
  63.         $this->initializeServiceKernel();
  64.         foreach ($this->getBundles() as $bundle) {
  65.             $bundle->setContainer($this->container);
  66.             $bundle->boot();
  67.         }
  68.         $this->booted true;
  69.     }
  70.     public function registerBundles()
  71.     {
  72.         $bundles = [
  73.             new Codeages\PluginBundle\FrameworkBundle(),
  74.             new Symfony\Bundle\SecurityBundle\SecurityBundle(),
  75.             new Symfony\Bundle\TwigBundle\TwigBundle(),
  76.             new Symfony\Bundle\MonologBundle\MonologBundle(),
  77.             new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
  78.             new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
  79.             new Endroid\QrCode\Bundle\EndroidQrCodeBundle(),
  80.             new Topxia\WebBundle\TopxiaWebBundle(),
  81.             new Topxia\AdminBundle\TopxiaAdminBundle(),
  82.             new Topxia\MobileBundleV2\TopxiaMobileBundleV2(),
  83.             new Bazinga\Bundle\JsTranslationBundle\BazingaJsTranslationBundle(),
  84.             new OAuth2\ServerBundle\OAuth2ServerBundle(),
  85.             new Codeages\PluginBundle\CodeagesPluginBundle(),
  86.             new AppBundle\AppBundle(),
  87.             new CustomBundle\CustomBundle(),
  88.             new ApiBundle\ApiBundle(),
  89.             new Symfony\Bundle\AsseticBundle\AsseticBundle(),
  90.             new MarketingMallBundle\MarketingMallBundle(),
  91.             new \AgentBundle\AgentBundle(),
  92.         ];
  93.         if (is_file($this->getRootDir().'/config/sentry.yml')) {
  94.             $bundles[] = new Sentry\SentryBundle\SentryBundle();
  95.         }
  96.         if ('test' !== $this->getEnvironment()) {
  97.             $plugins $this->pluginConfigurationManager->getInstalledPlugins();
  98.             foreach ($plugins as $plugin) {
  99.                 if ('plugin' != $plugin['type']) {
  100.                     continue;
  101.                 }
  102.                 if (!= $plugin['protocol']) {
  103.                     continue;
  104.                 }
  105.                 $code ucfirst($plugin['code']);
  106.                 $class "{$code}Plugin\\{$code}Plugin";
  107.                 $bundles[] = new $class();
  108.             }
  109.         }
  110.         if (in_array($this->getEnvironment(), ['dev''test'])) {
  111.             if (class_exists('Symfony\Bundle\WebProfilerBundle\WebProfilerBundle')) {
  112.                 $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
  113.             }
  114.             if (class_exists('Symfony\Bundle\WebServerBundle\WebServerBundle')) {
  115.                 $bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle();
  116.             }
  117.             if (class_exists('Sensio\Bundle\DistributionBundle\SensioDistributionBundle')) {
  118.                 $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
  119.             }
  120.             if (class_exists('Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle')) {
  121.                 $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
  122.             }
  123.         }
  124.         return $bundles;
  125.     }
  126.     public function registerContainerConfiguration(LoaderInterface $loader)
  127.     {
  128.         $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
  129.     }
  130.     public function getPlugins()
  131.     {
  132.         return $this->pluginConfigurationManager->getInstalledPlugins();
  133.     }
  134.     public function getPluginConfigurationManager()
  135.     {
  136.         return $this->pluginConfigurationManager;
  137.     }
  138.     public function setRequest(Request $request)
  139.     {
  140.         $this->request $request;
  141.         return $this;
  142.     }
  143.     public function initializeBiz(Biz $biz)
  144.     {
  145.         $biz['migration.directories'][] = dirname(__DIR__).'/migrations';
  146.         $biz['env'] = [
  147.             'base_url' => $this->request->getSchemeAndHttpHost().$this->request->getBasePath(),
  148.         ];
  149.         $biz['activity_dir'] = $this->getContainer()->getParameter('edusoho.activities_dir');
  150.         $biz->register(new DoctrineServiceProvider());
  151.         $biz->register(new MonologServiceProvider(), [
  152.             'monolog.logfile' => $this->getContainer()->getParameter('kernel.logs_dir').'/biz.log',
  153.             'monolog.level' => $this->isDebug() ? \Monolog\Logger::DEBUG : \Monolog\Logger::INFO,
  154.             'monolog.permission' => 0666,
  155.             'monolog.formatter' => new \Codeages\Biz\Framework\Util\ReadableJsonFormatter(),
  156.         ]);
  157.         $biz->extend('monolog', function ($monolog) {
  158.             $monolog->pushProcessor(new \AppBundle\Processor\TraceProcessor());
  159.             return $monolog;
  160.         });
  161.         $biz->register(new \Codeages\Biz\Framework\Provider\SchedulerServiceProvider());
  162.         $biz->register(new \Codeages\Biz\Framework\Provider\TargetlogServiceProvider(), ['targetlog.interceptor_enable' => false]);
  163.         $biz->register(new \Biz\System\LogServiceProvider());
  164.         $biz->register(new \Biz\DefaultServiceProvider());
  165.         $biz->register(new \Biz\DefaultSdkProvider());
  166.         if ($this->getContainer()->hasParameter('school_mode')) {
  167.             $schoolMode $this->getContainer()->getParameter('school_mode');
  168.         }
  169.         $biz->register(
  170.             new \Biz\S2B2C\S2B2CProvider(),
  171.             [
  172.                 's2b2c.options' => [
  173.                     'enabled' => !empty($schoolMode['type']) && 'merchant' == $schoolMode['type'],
  174.                     'supplierId' => empty($schoolMode['supplier']) ? null $schoolMode['supplier']['id'],
  175.                     'supplierDomain' => empty($schoolMode['supplier']) ? null $schoolMode['supplier']['domain'],
  176.                     'businessMode' => empty($schoolMode['business_mode']) ? null $schoolMode['business_mode'],
  177.                 ],
  178.             ]
  179.         );
  180.         $biz->register(new \Biz\Plumber\PlumberProvider());
  181.         $collector $this->getContainer()->get('biz.service_provider.collector');
  182.         foreach ($collector->all() as $provider) {
  183.             $biz->register($provider);
  184.         }
  185.         $biz->register(new Codeages\Biz\RateLimiter\RateLimiterServiceProvider());
  186.         $this->registerCacheServiceProvider($biz);
  187.         $biz->register(new Codeages\Biz\Order\OrderServiceProvider());
  188.         $biz->register(new Codeages\Biz\Pay\PayServiceProvider());
  189.         $biz->register(new Codeages\Biz\Invoice\InvoiceServiceProvider());
  190.         $biz->register(new Codeages\Biz\ItemBank\ItemBankServiceProvider(), [
  191.             'item_bank.html_helper.options' => [
  192.                 'cacheDir' => $this->getCacheDir().'/htmlpurifier',
  193.                 'safeDomains' => $this->getSafeDomains(),
  194.             ],
  195.         ]);
  196.         $biz->register(new \Biz\Accessor\AccessorServiceProvider());
  197.         $biz->register(new \Biz\OrderFacade\OrderFacadeServiceProvider());
  198.         $this->registerSessionServiceProvider($biz);
  199.         $biz->register(new \Codeages\Biz\Framework\Provider\QueueServiceProvider());
  200.         $biz->register(new \Biz\Goods\GoodsServiceProvider());
  201.         $biz->boot();
  202.         $activeTheme $this->pluginConfigurationManager->getActiveThemeName();
  203.         if (empty($activeTheme)) {
  204.             $this->pluginConfigurationManager->setActiveThemeName('jianmo')->save();
  205.         }
  206.         $biz['pluginConfigurationManager'] = $this->pluginConfigurationManager;
  207.     }
  208.     protected function registerSessionServiceProvider($biz)
  209.     {
  210.         if ($this->getContainer()->hasParameter('session_redis')) {
  211.             $session_redis $this->getContainer()->getParameter('session_redis');
  212.             $biz->register(
  213.                 new \Codeages\Biz\Framework\Provider\SessionServiceProvider(),
  214.                 [
  215.                     'session.options' => [
  216.                         'max_life_time' => 7200,
  217.                         'session_storage' => 'redis',
  218.                         'session_redis_host' => $session_redis['host'],
  219.                         'session_redis_port' => $session_redis['port'],
  220.                         'session_redis_timeout' => $session_redis['timeout'],
  221.                         'session_redis_reserved' => $session_redis['reserved'],
  222.                         'session_redis_retry_interval' => $session_redis['retry_interval'],
  223.                         'session_redis_key_prefix' => $session_redis['key_prefix'],
  224.                         'session_redis_password' => $session_redis['password'],
  225.                     ],
  226.                 ]
  227.             );
  228.         } else {
  229.             $biz->register(new \Codeages\Biz\Framework\Provider\SessionServiceProvider());
  230.         }
  231.     }
  232.     protected function registerCacheServiceProvider($biz)
  233.     {
  234.         if ($this->getContainer()->hasParameter('redis_host')) {
  235.             $biz->register(
  236.                 new Codeages\Biz\Framework\Provider\RedisServiceProvider(),
  237.                 [
  238.                     'redis.options' => [
  239.                         'host' => $this->getContainer()->getParameter('redis_host'),
  240.                         'timeout' => $this->getContainer()->getParameter('redis_timeout'),
  241.                         'reserved' => $this->getContainer()->getParameter('redis_reserved'),
  242.                         'redis_interval' => $this->getContainer()->getParameter('redis_retry_interval'),
  243.                         'password' => $this->getContainer()->hasParameter('redis_password') ? $this->getContainer()->getParameter('redis_password') : '',
  244.                     ],
  245.                     'dao.cache.enabled' => true,
  246.                 ]
  247.             );
  248.         }
  249.     }
  250.     protected function initializeServiceKernel()
  251.     {
  252.         if (!$this->isServiceKernelInit) {
  253.             $container $this->getContainer();
  254.             $biz $container->get('biz');
  255.             $serviceKernel ServiceKernel::create($this->getEnvironment(), $this->isDebug());
  256.             try {
  257.                 //初始化项目时,biz_session 尚未创建
  258.                 $invitedCode = @$container->get('session')->get('invitedCode''');
  259.             } catch (\Exception $e) {
  260.                 $invitedCode '';
  261.             }
  262.             $currentUser = [
  263.                 'currentIp' => $this->request->getClientIp() ?: '127.0.0.1',
  264.                 'isSecure' => $this->request->isSecure(),
  265.                 'invitedCode' => $invitedCode,
  266.             ];
  267.             $currentUser = new \Biz\User\AnonymousUser($currentUser);
  268.             $biz['user'] = $currentUser;
  269.             $serviceKernel
  270.                 ->setBiz($biz)
  271.                 ->setCurrentUser($currentUser)
  272.                 ->setEnvVariable(
  273.                 [
  274.                     'host' => $this->request->getHttpHost(),
  275.                     'schemeAndHost' => $this->request->getSchemeAndHttpHost(),
  276.                     'basePath' => $this->request->getBasePath(),
  277.                     'baseUrl' => $this->request->getSchemeAndHttpHost().$this->request->getBasePath(),
  278.                 ]
  279.             )
  280.                 ->setTranslatorEnabled(true)
  281.                 ->setTranslator($container->get('translator'))
  282.                 ->setParameterBag($container->getParameterBag())
  283.                 ->registerModuleDirectory(dirname(__DIR__).'/plugins');
  284.             $this->isServiceKernelInit true;
  285.         }
  286.     }
  287.     public function getCacheDir()
  288.     {
  289.         $theme $this->pluginConfigurationManager->getActiveThemeName();
  290.         $theme = empty($theme) ? '' ucfirst(str_replace('-''_'$theme));
  291.         return $this->rootDir.'/cache/'.$this->environment.'/'.$theme;
  292.     }
  293.     public function getSafeDomains()
  294.     {
  295.         if ('test' === $this->getEnvironment()) {
  296.             return [];
  297.         }
  298.         try {
  299.             $safeIframeDomains $this->getContainer()->get('biz')->service('System:CacheService')->get('safe_iframe_domains');
  300.         } catch (\Exception $e) {
  301.             return [];
  302.         }
  303.         return empty($safeIframeDomains) ? [] : $safeIframeDomains;
  304.     }
  305. }