Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

FileWriterGeneratorStrategy.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 2.34 KiB


01  <?php
02   
03  declare(strict_types=1);
04   
05  namespace ProxyManager\GeneratorStrategy;
06   
07  use ProxyManager\Exception\FileNotWritableException;
08  use ProxyManager\FileLocator\FileLocatorInterface;
09  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  }
89