Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
FileWriterGeneratorStrategy.php
001 <?php
002 /*
003 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
004 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
005 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
006 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
007 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
008 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
009 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
010 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
011 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
012 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
013 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014 *
015 * This software consists of voluntary contributions made by many individuals
016 * and is licensed under the MIT license.
017 */
018
019 namespace ProxyManager\GeneratorStrategy;
020
021 use ProxyManager\Exception\FileNotWritableException;
022 use ProxyManager\FileLocator\FileLocatorInterface;
023 use Zend\Code\Generator\ClassGenerator;
024
025 /**
026 * Generator strategy that writes the generated classes to disk while generating them
027 *
028 * {@inheritDoc}
029 *
030 * @author Marco Pivetta <ocramius@gmail.com>
031 * @license MIT
032 */
033 class FileWriterGeneratorStrategy implements GeneratorStrategyInterface
034 {
035 /**
036 * @var \ProxyManager\FileLocator\FileLocatorInterface
037 */
038 protected $fileLocator;
039
040 /**
041 * @var callable
042 */
043 private $emptyErrorHandler;
044
045 /**
046 * @param \ProxyManager\FileLocator\FileLocatorInterface $fileLocator
047 */
048 public function __construct(FileLocatorInterface $fileLocator)
049 {
050 $this->fileLocator = $fileLocator;
051 $this->emptyErrorHandler = function () {
052 };
053 }
054
055 /**
056 * Write generated code to disk and return the class code
057 *
058 * {@inheritDoc}
059 */
060 public function generate(ClassGenerator $classGenerator)
061 {
062 $className = trim($classGenerator->getNamespaceName(), '\\')
063 . '\\' . trim($classGenerator->getName(), '\\');
064 $generatedCode = $classGenerator->generate();
065 $fileName = $this->fileLocator->getProxyFileName($className);
066
067 set_error_handler($this->emptyErrorHandler);
068
069 try {
070 $this->writeFile("<?php\n\n" . $generatedCode, $fileName);
071 } catch (FileNotWritableException $fileNotWritable) {
072 restore_error_handler();
073
074 throw $fileNotWritable;
075 }
076
077 restore_error_handler();
078
079 return $generatedCode;
080 }
081
082 /**
083 * Writes the source file in such a way that race conditions are avoided when the same file is written
084 * multiple times in a short time period
085 *
086 * @param string $source
087 * @param string $location
088 *
089 * @throws FileNotWritableException
090 */
091 private function writeFile($source, $location)
092 {
093 $tmpFileName = $location . '.' . uniqid('', true);
094
095 if (! file_put_contents($tmpFileName, $source)) {
096 throw FileNotWritableException::fromNonWritableLocation($tmpFileName);
097 }
098
099 if (! rename($tmpFileName, $location)) {
100 unlink($tmpFileName);
101
102 throw FileNotWritableException::fromInvalidMoveOperation($tmpFileName, $location);
103 }
104 }
105 }
106