[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/GeneratorStrategy/ -> FileWriterGeneratorStrategy.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace ProxyManager\GeneratorStrategy;
   6  
   7  use ProxyManager\Exception\FileNotWritableException;
   8  use ProxyManager\FileLocator\FileLocatorInterface;
   9  use Zend\Code\Generator\ClassGenerator;
  10  
  11  /**
  12   * Generator strategy that writes the generated classes to disk while generating them
  13   *
  14   * {@inheritDoc}
  15   *
  16   * @author Marco Pivetta <ocramius@gmail.com>
  17   * @license MIT
  18   */
  19  class FileWriterGeneratorStrategy implements GeneratorStrategyInterface
  20  {
  21      /**
  22       * @var \ProxyManager\FileLocator\FileLocatorInterface
  23       */
  24      protected $fileLocator;
  25  
  26      /**
  27       * @var callable
  28       */
  29      private $emptyErrorHandler;
  30  
  31      /**
  32       * @param \ProxyManager\FileLocator\FileLocatorInterface $fileLocator
  33       */
  34      public function __construct(FileLocatorInterface $fileLocator)
  35      {
  36          $this->fileLocator       = $fileLocator;
  37          $this->emptyErrorHandler = function () {
  38          };
  39      }
  40  
  41      /**
  42       * Write generated code to disk and return the class code
  43       *
  44       * {@inheritDoc}
  45       *
  46       * @throws FileNotWritableException
  47       */
  48      public function generate(ClassGenerator $classGenerator) : string
  49      {
  50          $className     = trim($classGenerator->getNamespaceName(), '\\')
  51              . '\\' . trim($classGenerator->getName(), '\\');
  52          $generatedCode = $classGenerator->generate();
  53          $fileName      = $this->fileLocator->getProxyFileName($className);
  54  
  55          set_error_handler($this->emptyErrorHandler);
  56  
  57          try {
  58              $this->writeFile("<?php\n\n" . $generatedCode, $fileName);
  59  
  60              return $generatedCode;
  61          } finally {
  62              restore_error_handler();
  63          }
  64      }
  65  
  66      /**
  67       * Writes the source file in such a way that race conditions are avoided when the same file is written
  68       * multiple times in a short time period
  69       *
  70       * @param string $source
  71       * @param string $location
  72       *
  73       * @throws FileNotWritableException
  74       */
  75      private function writeFile(string $source, string $location) : void
  76      {
  77          $tmpFileName = tempnam($location, 'temporaryProxyManagerFile');
  78  
  79          file_put_contents($tmpFileName, $source);
  80          chmod($tmpFileName, 0666 & ~umask());
  81  
  82          if (! rename($tmpFileName, $location)) {
  83              unlink($tmpFileName);
  84  
  85              throw FileNotWritableException::fromInvalidMoveOperation($tmpFileName, $location);
  86          }
  87      }
  88  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1