Esempio n. 1
0
 /**
  * @inheritDoc
  */
 public static function loadFromFile($file)
 {
     if (false === ($xml = simplexml_load_file($file))) {
         throw new LogicException(Message::get(Message::DEFINITION_FORMAT_ERR, $file), Message::DEFINITION_FORMAT_ERR);
     }
     return static::xmlToArray($xml);
 }
Esempio n. 2
0
 /**
  * {@inheritDoc}
  *
  * @return ParameterInterface
  */
 public function getResolver()
 {
     if (!$this->hasResolver()) {
         throw new NotFoundException(Message::get(Message::PARAM_RESOLVER_MISS), Message::PARAM_RESOLVER_MISS);
     }
     return $this->resolver;
 }
Esempio n. 3
0
 /**
  * Provides a static interface for container dynamic methods
  *
  * @param  string $name method name
  * @param  array $arguments arguments
  * @return mixed
  * @access public
  * @static
  * @internal
  */
 public static function __callStatic($name, array $arguments)
 {
     $container = static::getContainer();
     if (method_exists($container, $name)) {
         return call_user_func_array([$container, $name], $arguments);
     }
     throw new BadMethodCallException(Message::get(Message::METHOD_NOT_FOUND, get_called_class(), $name), Message::METHOD_NOT_FOUND);
 }
Esempio n. 4
0
 /**
  * @inheritDoc
  */
 public function getContainer()
 {
     // found
     if ($this->hasContainer()) {
         return $this->container;
     }
     // not found
     throw new NotFoundException(Message::get(Message::CONTAINER_NOT_FOUND, get_class($this)), Message::CONTAINER_NOT_FOUND);
 }
Esempio n. 5
0
 /**
  * @inheritDoc
  */
 public function get($id)
 {
     // has() will insert definitions into $container
     if ($this->has($id)) {
         // get service from container then
         return $this->getContainer()->get($id);
     } else {
         throw new NotFoundException(Message::get(Message::SERVICE_ID_NOT_FOUND, $id), Message::SERVICE_ID_NOT_FOUND);
     }
 }
Esempio n. 6
0
 /**
  * Get from the delegator
  *
  * {@inheritDoc}
  */
 public function get($id)
 {
     /* @var $container InteropContainerInterface */
     foreach ($this->getContainers() as $container) {
         if ($container->has($id)) {
             return $container->get($id);
         }
     }
     // not found
     throw new NotFoundException(Message::get(Message::SERVICE_ID_NOT_FOUND, $id), Message::SERVICE_ID_NOT_FOUND);
 }
Esempio n. 7
0
 /**
  * @inheritDoc
  *
  * services.s.json,
  * <code>
  * {
  *     'streamhandler': {
  *         'class' : [
  *               '\Phossa\Logger\Handler\StreamHandler',
  *               [ '%logger.file%', 'debug' ]
  *         ],
  *         'scope' : '__SINGLE__'
  *     },
  *     ...
  * }
  * </code>
  *
  * parameters.p.json,
  * <code>
  * {
  *     'logger': {
  *         'file':  '/var/local/app.log',
  *         'mail': {
  *             'to_address'   : '*****@*****.**',
  *             'from_address' : '*****@*****.**',
  *             'subject'      : 'App Logs'
  *         }
  *     }
  * }
  * </code>
  */
 public static function loadFromFile($file)
 {
     // readin json content
     $json = file_get_contents($file);
     if (false === $json) {
         throw new LogicException(Message::get(Message::DEFINITION_NOT_FOUND, $file), Message::DEFINITION_NOT_FOUND);
     }
     if (!is_array($definitions = json_decode($json, true))) {
         throw new LogicException(Message::get(Message::DEFINITION_FORMAT_ERR, $file), Message::DEFINITION_FORMAT_ERR);
     }
     return $definitions;
 }
Esempio n. 8
0
 /**
  * Get the reference value
  *
  * @param  ReferenceAbstract $reference
  * @param  int $level current recursive level
  * @return mixed
  * @access protected
  */
 protected function getReferenceValue(ReferenceAbstract $reference, $level = 0)
 {
     $name = $reference->getName();
     // loop found
     if ($level > 2) {
         throw new NotFoundException(Message::get(Message::PARAMETER_LOOP_FOUND, $name), Message::PARAMETER_LOOP_FOUND);
     }
     // get service reference value
     if ($reference instanceof ServiceReference) {
         return $this->delegatedGet($name);
         // get parameter value, if value is another reference, go deeper
     } else {
         $val = $this->getParameter($name);
         if (is_string($val) && ($ref = $this->isReference($val))) {
             return $this->getReferenceValue($ref, ++$level);
         }
         return $val;
     }
 }
Esempio n. 9
0
 /**
  * Get this paramter's value either a string or an associate array
  *
  * ```php
  * $this->set('cache.dir', '/var/tmp');
  *
  * // will return an array ['dir' => '/var/tmp'];
  * $result = $this->getParameter('cache');
  *
  * // will return a string, 'var/tmp'
  * $result = $this->getParameter('cache.dir');
  * ```
  *
  * @param  string $name parameter name
  * @return string|array
  * @throws NotFoundException if not found
  * @access protected
  */
 protected function getParameter($name)
 {
     if ($this->hasResolver()) {
         $found = $this->getResolver()->get($name);
     } else {
         // break into parts by '.'
         $parts = explode('.', $name);
         $found = $this->parameters;
         while (null !== ($part = array_shift($parts))) {
             if (!isset($found[$part])) {
                 $found = null;
                 break;
             }
             $found = $found[$part];
         }
     }
     if (null === $found) {
         throw new NotFoundException(Message::get(Message::PARAMETER_NOT_FOUND, $name), Message::PARAMETER_NOT_FOUND);
     }
     return $found;
 }
Esempio n. 10
0
 /**
  * Constructor, check EXTENION_CLASS set or not
  *
  * @throws LogicException if something goes wrong
  * @access public
  * @internal
  */
 public function __construct()
 {
     if (get_called_class() !== static::EXTENSION_CLASS) {
         throw new LogicException(Message::get(Message::EXTENION_INVALID_CLASS, get_class($this)), Message::EXTENION_INVALID_CLASS);
     }
 }
Esempio n. 11
0
 /**
  * Get the provider object
  *
  * @param  string|ProviderAbstract $providerOrClass class or object
  * @return ProviderAbstract
  * @throws LogicException
  * @access protected
  */
 public function getProviderInstance($providerOrClass)
 {
     if (is_a($providerOrClass, ProviderAbstract::PROVIDER_CLASS, true)) {
         if (!is_object($providerOrClass)) {
             $providerOrClass = new $providerOrClass();
         }
         return $providerOrClass;
     } else {
         throw new LogicException(Message::get(Message::EXT_PROVIDER_ERROR, $providerOrClass), Message::EXT_PROVIDER_ERROR);
     }
 }
Esempio n. 12
0
 /**
  * @inheritDoc
  *
  * services.s.php
  * <code>
  * <?php
  * use Phossa\Logger;
  * use Psr\Log\LogLevel;
  *
  * return [
  *     'streamhandler' => [
  *         'class' => [
  *              Logger\Handler\StreamHandler::class,
  *              [ '%logger.file%', LogLevel::WARNING ],
  *         ],
  *         'methods' => [
  *              [ 'setFormatter', ['@ansiFormatter@'] ]
  *         ]
  *     ],
  *
  *     'logger' => [
  *         'class' => [
  *             Logger\Logger::class,    // class
  *             [ '%logger.channel%' ]   // constructor arguments
  *         ],
  *         'methods' => [
  *             [ 'addHandler', [ '@streamhandler@' ] ],
  *             [ 'addDecorator', [ '@interpolate@' ] ],
  *             [ 'addDecorator', [ '@profiler@' ] ]
  *         ],
  *     ],
  *
  *     'ansiFormatter' => [
  *         'class' => function() {
  *              return new Logger\Formatter\AnsiFormatter();
  *         },
  *         'scope' => Container::SCOPE_SINGLE
  *     ],
  *
  *     // use closure directly
  *     'interpolate' => function() {
  *         return new Logger\Decorator\InterpolateDecorator();
  *     },
  *
  *     // use array directly
  *     'profiler' => [
  *         Logger\Decorator\ProfileHandler::class
  *     ],
  *     ...
  * ];
  * </code>
  *
  * parameters.p.php,
  * <code>
  * <?php
  * return [
  *     'logger' => [
  *         'channel' =>  'myLogger',
  *         'file'    =>  __DIR__.'/../app.log',
  *         'mail'    => [
  *             'to_address'   => '*****@*****.**',
  *             'from_address' => '*****@*****.**',
  *             'subject'      => 'App Logs'
  *         ]
  *     ]
  * ];
  * </code>
  */
 public static function loadFromFile($file)
 {
     if (!file_exists($file) || !is_array($definitions = (include $file))) {
         throw new LogicException(Message::get(Message::DEFINITION_NOT_FOUND, $file), Message::DEFINITION_NOT_FOUND);
     }
     return $definitions;
 }
Esempio n. 13
0
 /**
  * Check circular for get()
  *
  * @param  string $id service id
  * @return void
  * @throws LogicException if circular found
  * @access protected
  */
 protected function checkCircularMark($id)
 {
     // reference id "@$id@" of current service object
     $refId = $this->getServiceReferenceId($id);
     // circular detected
     if (isset($this->circular[$refId])) {
         throw new LogicException(Message::get(Message::SERVICE_CIRCULAR, $id), Message::SERVICE_CIRCULAR);
         // mark it
     } else {
         $this->circular[$refId] = ++$this->counter;
     }
 }
Esempio n. 14
0
 /**
  * Initialize service object by runing its defined methods
  *
  * @param  string $id service id
  * @param  object $service service object
  * @return void
  * @throws LogicException
  * @access protected
  */
 protected function runDefinedMethods($id, $service)
 {
     try {
         if (isset($this->services[$id]['methods'])) {
             $methods = $this->services[$id]['methods'];
             foreach ($methods as $method) {
                 if (!is_array($method) || !isset($method[0]) || !is_string($method[0])) {
                     throw new LogicException(Message::get(Message::SERVICE_METHOD_ERROR, $id, isset($method[0]) ? $method[0] : ''), Message::SERVICE_METHOD_ERROR);
                 }
                 // execute with arguments
                 $this->executeCallable([$service, $method[0]], isset($method[1]) ? (array) $method[1] : []);
             }
         }
     } catch (\Exception $e) {
         throw new LogicException($e->getMessage(), $e->getCode(), $e);
     }
 }
Esempio n. 15
0
 /**
  * Get loader class name base on suffix
  *
  * @param  string $suffix
  * @param  string $filename
  * @return string classname
  * @throws LogicException if not class not found
  * @access protected
  */
 protected function getLoaderClass($suffix, $filename)
 {
     $class = __NAMESPACE__ . '\\Loader' . ucfirst($suffix);
     if (!class_exists($class)) {
         throw new LogicException(Message::get(Message::FILE_SUFFIX_UNKNOWN, $filename), Message::FILE_SUFFIX_UNKNOWN);
     }
     return $class;
 }