[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/Factory/ -> AbstractBaseFactory.php (source)

   1  <?php
   2  /*
   3   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   4   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   5   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   6   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   7   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   8   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   9   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14   *
  15   * This software consists of voluntary contributions made by many individuals
  16   * and is licensed under the MIT license.
  17   */
  18  
  19  declare(strict_types=1);
  20  
  21  namespace ProxyManager\Factory;
  22  
  23  use ProxyManager\Configuration;
  24  use ProxyManager\Generator\ClassGenerator;
  25  use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
  26  use ProxyManager\Signature\Exception\InvalidSignatureException;
  27  use ProxyManager\Signature\Exception\MissingSignatureException;
  28  use ProxyManager\Version;
  29  use ReflectionClass;
  30  
  31  /**
  32   * Base factory common logic
  33   *
  34   * @author Marco Pivetta <ocramius@gmail.com>
  35   * @license MIT
  36   */
  37  abstract class AbstractBaseFactory
  38  {
  39      /**
  40       * @var \ProxyManager\Configuration
  41       */
  42      protected $configuration;
  43  
  44      /**
  45       * Cached checked class names
  46       *
  47       * @var string[]
  48       */
  49      private $checkedClasses = [];
  50  
  51      /**
  52       * @param \ProxyManager\Configuration $configuration
  53       */
  54      public function __construct(Configuration $configuration = null)
  55      {
  56          $this->configuration = $configuration ?: new Configuration();
  57      }
  58  
  59      /**
  60       * Generate a proxy from a class name
  61       *
  62       * @param string  $className
  63       * @param mixed[] $proxyOptions
  64       *
  65       * @return string proxy class name
  66       *
  67       * @throws InvalidSignatureException
  68       * @throws MissingSignatureException
  69       * @throws \OutOfBoundsException
  70       */
  71      protected function generateProxy(string $className, array $proxyOptions = []) : string
  72      {
  73          if (\array_key_exists($className, $this->checkedClasses)) {
  74              return $this->checkedClasses[$className];
  75          }
  76  
  77          $proxyParameters = [
  78              'className'           => $className,
  79              'factory'             => get_class($this),
  80              'proxyManagerVersion' => Version::getVersion(),
  81          ];
  82          $proxyClassName  = $this
  83              ->configuration
  84              ->getClassNameInflector()
  85              ->getProxyClassName($className, $proxyParameters);
  86  
  87          if (! class_exists($proxyClassName)) {
  88              $this->generateProxyClass(
  89                  $proxyClassName,
  90                  $className,
  91                  $proxyParameters,
  92                  $proxyOptions
  93              );
  94          }
  95  
  96          $this
  97              ->configuration
  98              ->getSignatureChecker()
  99              ->checkSignature(new ReflectionClass($proxyClassName), $proxyParameters);
 100  
 101          return $this->checkedClasses[$className] = $proxyClassName;
 102      }
 103  
 104      abstract protected function getGenerator() : ProxyGeneratorInterface;
 105  
 106      /**
 107       * Generates the provided `$proxyClassName` from the given `$className` and `$proxyParameters`
 108       *
 109       * @param string  $proxyClassName
 110       * @param string  $className
 111       * @param array   $proxyParameters
 112       * @param mixed[] $proxyOptions
 113       */
 114      private function generateProxyClass(
 115          string $proxyClassName,
 116          string $className,
 117          array $proxyParameters,
 118          array $proxyOptions = []
 119      ) : void {
 120          $className = $this->configuration->getClassNameInflector()->getUserClassName($className);
 121          $phpClass  = new ClassGenerator($proxyClassName);
 122  
 123          $this->getGenerator()->generate(new ReflectionClass($className), $phpClass, $proxyOptions);
 124  
 125          $phpClass = $this->configuration->getClassSignatureGenerator()->addSignature($phpClass, $proxyParameters);
 126  
 127          $this->configuration->getGeneratorStrategy()->generate($phpClass, $proxyOptions);
 128  
 129          $autoloader = $this->configuration->getProxyAutoloader();
 130  
 131          $autoloader($proxyClassName);
 132      }
 133  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1