Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Container/Container.php - 134 lines - 3362 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Container.
  26   *
  27   * Base container implementation for resolving and initialising classes.
  28   * Basic usage would happen with the getInstance() method:
  29   *
  30   * <code>
  31   * $container = new \Textpattern\Container\Container();
  32   * $container->getInstance('Abc_class', 'argument1', 'argument2');
  33   * </code>
  34   *
  35   * Normally you would write a static wrapper class for the container to keep the
  36   * instances and configuration between calls. See the 'Txp' class for
  37   * Textpattern's own implementation.
  38   *
  39   * @since   4.6.0
  40   * @package Container
  41   * @see     Txp
  42   */
  43  namespace Textpattern\Container;
  44  
  45  class Container implements \Textpattern\Container\ContainerInterface
  46  {
  47      /**
  48       * Stores registered classes.
  49       *
  50       * @var array
  51       */
  52  
  53      protected $registered = array();
  54  
  55      /**
  56       * Stores shared instances.
  57       *
  58       * @var array
  59       */
  60  
  61      protected $instances = array();
  62  
  63      /**
  64       * {@inheritdoc}
  65       */
  66  
  67      public function register($alias, $class)
  68      {
  69          if (isset($this->registered[$alias])) {
  70              throw new InvalidArgumentException(gTxt('alias_is_taken'));
  71          }
  72  
  73          $this->registered[$alias] = $class;
  74  
  75          return $this;
  76      }
  77  
  78      /**
  79       * {@inheritdoc}
  80       */
  81  
  82      public function remove($alias)
  83      {
  84          unset($this->registered[$alias], $this->instances[$alias]);
  85  
  86          return $this;
  87      }
  88  
  89      /**
  90       * Resolves an alias to the actual classname.
  91       *
  92       * @param  string $alias The alias
  93       * @return string The classname
  94       */
  95  
  96      protected function resolveAlias($alias)
  97      {
  98          if (isset($this->registered[$alias])) {
  99              return $this->registered[$alias];
 100          }
 101  
 102          return $alias;
 103      }
 104  
 105      /**
 106       * {@inheritdoc}
 107       */
 108  
 109      public function getInstance($alias, array $options)
 110      {
 111          if (isset($this->instances[$alias])) {
 112              $instance = $this->instances[$alias];
 113          } else {
 114              $class = $this->resolveAlias($alias);
 115  
 116              if ($options && method_exists($class, '__construct')) {
 117                  $reflection = new \ReflectionClass($class);
 118                  $instance = $reflection->newInstanceArgs($options);
 119              } else {
 120                  $instance = new $class;
 121              }
 122  
 123              if ($instance instanceof \Textpattern\Container\ReusableInterface) {
 124                  $this->instances[$alias] = $instance;
 125              }
 126          }
 127  
 128          if ($instance instanceof \Textpattern\Container\FactorableInterface) {
 129              $instance = call_user_func_array(array($instance, 'getInstance'), $options);
 130          }
 131  
 132          return $instance;
 133      }
 134  }

title

Description

title

Description

title

Description

title

title

Body