[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/GeneratorStrategy/ -> FileWriterGeneratorStrategy.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  namespace ProxyManager\GeneratorStrategy;
  20  
  21  use ProxyManager\Exception\FileNotWritableException;
  22  use ProxyManager\FileLocator\FileLocatorInterface;
  23  use Zend\Code\Generator\ClassGenerator;
  24  
  25  /**
  26   * Generator strategy that writes the generated classes to disk while generating them
  27   *
  28   * {@inheritDoc}
  29   *
  30   * @author Marco Pivetta <ocramius@gmail.com>
  31   * @license MIT
  32   */
  33  class FileWriterGeneratorStrategy implements GeneratorStrategyInterface
  34  {
  35      /**
  36       * @var \ProxyManager\FileLocator\FileLocatorInterface
  37       */
  38      protected $fileLocator;
  39  
  40      /**
  41       * @var callable
  42       */
  43      private $emptyErrorHandler;
  44  
  45      /**
  46       * @param \ProxyManager\FileLocator\FileLocatorInterface $fileLocator
  47       */
  48      public function __construct(FileLocatorInterface $fileLocator)
  49      {
  50          $this->fileLocator       = $fileLocator;
  51          $this->emptyErrorHandler = function () {
  52          };
  53      }
  54  
  55      /**
  56       * Write generated code to disk and return the class code
  57       *
  58       * {@inheritDoc}
  59       */
  60      public function generate(ClassGenerator $classGenerator)
  61      {
  62          $className     = trim($classGenerator->getNamespaceName(), '\\')
  63              . '\\' . trim($classGenerator->getName(), '\\');
  64          $generatedCode = $classGenerator->generate();
  65          $fileName      = $this->fileLocator->getProxyFileName($className);
  66  
  67          set_error_handler($this->emptyErrorHandler);
  68  
  69          try {
  70              $this->writeFile("<?php\n\n" . $generatedCode, $fileName);
  71          } catch (FileNotWritableException $fileNotWritable) {
  72              restore_error_handler();
  73  
  74              throw $fileNotWritable;
  75          }
  76  
  77          restore_error_handler();
  78  
  79          return $generatedCode;
  80      }
  81  
  82      /**
  83       * Writes the source file in such a way that race conditions are avoided when the same file is written
  84       * multiple times in a short time period
  85       *
  86       * @param string $source
  87       * @param string $location
  88       *
  89       * @throws FileNotWritableException
  90       */
  91      private function writeFile($source, $location)
  92      {
  93          $tmpFileName   = $location . '.' . uniqid('', true);
  94  
  95          if (! file_put_contents($tmpFileName, $source)) {
  96              throw FileNotWritableException::fromNonWritableLocation($tmpFileName);
  97          }
  98  
  99          if (! rename($tmpFileName, $location)) {
 100              unlink($tmpFileName);
 101  
 102              throw FileNotWritableException::fromInvalidMoveOperation($tmpFileName, $location);
 103          }
 104      }
 105  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1