src/Topxia/Service/Common/ServiceKernel.php line 219

Open in your IDE?
  1. <?php
  2. namespace Topxia\Service\Common;
  3. use Biz\User\CurrentUser;
  4. use Codeages\Biz\Framework\Context\Biz;
  5. use Codeages\Biz\Framework\Dao\Connection;
  6. use Symfony\Component\EventDispatcher\EventDispatcher;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. class ServiceKernel
  9. {
  10.     private static $_instance;
  11.     private static $_dispatcher;
  12.     protected $_moduleDirectories = [];
  13.     protected $_moduleConfig = [];
  14.     protected $environment;
  15.     protected $debug;
  16.     protected $booted;
  17.     protected $translator;
  18.     protected $translatorEnabled;
  19.     protected $pluginKernel;
  20.     protected $parameterBag;
  21.     protected $currentUser;
  22.     protected $pool = [];
  23.     protected $connection;
  24.     protected $classMaps = [];
  25.     /**
  26.      * @var Biz
  27.      */
  28.     protected $biz null;
  29.     protected $env = [];
  30.     public function getRedis($group 'default')
  31.     {
  32.         $biz $this->getBiz();
  33.         if (empty($biz['redis'])) {
  34.             return false;
  35.         }
  36.         return $biz['redis'];
  37.     }
  38.     public static function create($environment$debug)
  39.     {
  40.         if (self::$_instance) {
  41.             return self::$_instance;
  42.         }
  43.         $instance = new self();
  44.         $instance->environment $environment;
  45.         $instance->debug = (bool) $debug;
  46.         $instance->registerModuleDirectory(realpath(__DIR__.'/../../../'));
  47.         self::$_instance $instance;
  48.         return $instance;
  49.     }
  50.     /**
  51.      * @return ServiceKernel
  52.      */
  53.     public static function instance()
  54.     {
  55.         if (empty(self::$_instance)) {
  56.             throw new \RuntimeException('The instance of ServiceKernel is not created!');
  57.         }
  58.         self::$_instance->boot();
  59.         return self::$_instance;
  60.     }
  61.     /**
  62.      * @return EventDispatcherInterface
  63.      */
  64.     public static function dispatcher()
  65.     {
  66.         if (self::$_dispatcher) {
  67.             return self::$_dispatcher;
  68.         }
  69.         //@ TODO 新的事件机制通过Symfony Tag形式注入,但是APP接口并没有使用Symfony, 重构接口时去掉判断
  70.         if (defined('RUNTIME_ENV') && RUNTIME_ENV === 'API') {
  71.             self::$_dispatcher = new EventDispatcher();
  72.         } else {
  73.             self::$_dispatcher self::instance()->biz['dispatcher'];
  74.         }
  75.         return self::$_dispatcher;
  76.     }
  77.     public function boot()
  78.     {
  79.         if (true === $this->booted) {
  80.             return;
  81.         }
  82.         $this->booted true;
  83.     }
  84.     public function setParameterBag($parameterBag)
  85.     {
  86.         $this->parameterBag $parameterBag;
  87.         return $this;
  88.     }
  89.     public function getParameter($name)
  90.     {
  91.         if (is_null($this->parameterBag)) {
  92.             throw new \RuntimeException('The `ParameterBag` of ServiceKernel is not setted!');
  93.         }
  94.         return $this->parameterBag->get($name);
  95.     }
  96.     public function setTranslator($translator)
  97.     {
  98.         $this->translator $translator;
  99.         return $this;
  100.     }
  101.     public function getTranslator()
  102.     {
  103.         if (is_null($this->translator)) {
  104.             throw new \RuntimeException('The `Translator` of ServiceKernel is not setted!');
  105.         }
  106.         return $this->translator;
  107.     }
  108.     public function setTranslatorEnabled($boolean true)
  109.     {
  110.         $this->translatorEnabled $boolean;
  111.         return $this;
  112.     }
  113.     public function getTranslatorEnabled()
  114.     {
  115.         return $this->translatorEnabled;
  116.     }
  117.     public function hasParameter($name)
  118.     {
  119.         if (is_null($this->parameterBag)) {
  120.             throw new \RuntimeException('The `ParameterBag` of ServiceKernel is not setted!');
  121.         }
  122.         return $this->parameterBag->has($name);
  123.     }
  124.     public function setCurrentUser($currentUser)
  125.     {
  126.         $biz $this->getBiz();
  127.         $biz['user'] = $currentUser;
  128.         return $this;
  129.     }
  130.     /**
  131.      * @return CurrentUser
  132.      */
  133.     public function getCurrentUser()
  134.     {
  135.         $biz $this->getBiz();
  136.         if (!isset($biz['user'])) {
  137.             throw new \RuntimeException('The `CurrentUser` of ServiceKernel is not setted!');
  138.         }
  139.         return $biz['user'];
  140.     }
  141.     public function setEnvVariable(array $env)
  142.     {
  143.         $this->env $env;
  144.         return $this;
  145.     }
  146.     public function getEnvVariable($key null)
  147.     {
  148.         if (empty($key)) {
  149.             return $this->env;
  150.         }
  151.         if (!isset($this->env[$key])) {
  152.             throw new \RuntimeException("Environment variable `{$key}` is not exist.");
  153.         }
  154.         return $this->env[$key];
  155.     }
  156.     /**
  157.      * @return Connection
  158.      */
  159.     public function getConnection()
  160.     {
  161.         return $this->biz['db'];
  162.     }
  163.     public function createService($name)
  164.     {
  165.         if (empty($this->pool[$name])) {
  166.             $this->pool[$name] = $this->biz->service($name);
  167.             // $class = $this->getClassName('service', $name);
  168.             // if (class_exists($class)) {
  169.             //     $this->pool[$name] = new $class();
  170.             // } else {
  171.             //     $this->pool[$name] = $this->biz->service($name);
  172.             // }
  173.         }
  174.         return $this->pool[$name];
  175.     }
  176.     public function createDao($name)
  177.     {
  178.         if (empty($this->pool[$name])) {
  179.             $this->pool[$name] = $this->biz->dao($name);
  180.         }
  181.         return $this->pool[$name];
  182.     }
  183.     public function getEnvironment()
  184.     {
  185.         return $this->environment;
  186.     }
  187.     public function isDebug()
  188.     {
  189.         return $this->debug;
  190.     }
  191.     public function registerModuleDirectory($dir)
  192.     {
  193.         $this->_moduleDirectories[] = $dir;
  194.         return $this;
  195.     }
  196.     public function getModuleDirectories()
  197.     {
  198.         return $this->_moduleDirectories;
  199.     }
  200.     public function getModuleConfig($key$default null)
  201.     {
  202.         if (!isset($this->_moduleConfig[$key])) {
  203.             return $default;
  204.         }
  205.         return $this->_moduleConfig[$key];
  206.     }
  207.     public function transArray($messages$arguments = [], $domain null$locale null)
  208.     {
  209.         foreach ($messages as &$message) {
  210.             $message $this->trans($message$arguments$domain$locale);
  211.         }
  212.         return $messages;
  213.     }
  214.     public function trans($message$arguments = [], $domain null$locale null)
  215.     {
  216.         if ($this->getTranslatorEnabled()) {
  217.             return $this->getTranslator()->trans($message$arguments$domain$locale);
  218.         }
  219.         return strtr((string) $message$arguments);
  220.     }
  221.     protected function getClassName($type$name)
  222.     {
  223.         $classMap $this->getClassMap($type);
  224.         if (isset($classMap[$name])) {
  225.             return $classMap[$name];
  226.         }
  227.         return $this->getServiceClassName($type$name);
  228.     }
  229.     protected function getServiceClassName($type$name)
  230.     {
  231.         $type strtolower($type);
  232.         list($namespace$name) = explode(':'$name2);
  233.         if (strpos($name'.') > 0) {
  234.             $namespace .= '\\Service';
  235.             list($module$className) = explode('.'$name);
  236.             if ('dao' == $type) {
  237.                 return $namespace.'\\'.$module.'\\Dao\\Impl\\'.$className.'Impl';
  238.             }
  239.             return $namespace.'\\'.$module.'\\Impl\\'.$className.'Impl';
  240.         } else {
  241.             $namespace substr(__NAMESPACE__0, -strlen('Common') - 1).'\\'.$namespace;
  242.         }
  243.         if ('dao' == $type) {
  244.             return $namespace.'\\Dao\\Impl\\'.$name.'Impl';
  245.         }
  246.         return $namespace.'\\Impl\\'.$name.'Impl';
  247.     }
  248.     public function setBiz(Biz $biz)
  249.     {
  250.         $this->biz $biz;
  251.         return $this;
  252.     }
  253.     public function getBiz()
  254.     {
  255.         if (!$this->biz) {
  256.             throw new \RuntimeException('The `Biz Container` of ServiceKernel is not setted!');
  257.         }
  258.         return $this->biz;
  259.     }
  260.     protected function getClassMap($type)
  261.     {
  262.         if (isset($this->classMaps[$type])) {
  263.             return $this->classMaps[$type];
  264.         }
  265.         $key = ('dao' == $type) ? 'topxia_daos' 'topxia_services';
  266.         if (!$this->hasParameter($key)) {
  267.             $this->classMaps[$type] = [];
  268.         } else {
  269.             $this->classMaps[$type] = $this->getParameter($key);
  270.         }
  271.         return $this->classMaps[$type];
  272.     }
  273. }