Feature: integration with webimpress/safe-writer
Use external library to write files
MODIFY
composer.json
composer.json
"require": {
"php": "7.4.*",
"zendframework/zend-code": "^3.4.0",
- "ocramius/package-versions": "^1.5.1"
+ "ocramius/package-versions": "^1.5.1",
+ "webimpress/safe-writer": "^1.0"
},
"conflict": {
"zendframework/zend-stdlib": "<3.2.1",

MODIFY
src/ProxyManager/Exception/FileNotWritableException.php
src/ProxyManager/Exception/FileNotWritableException.php
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace ProxyManager\Exception;
use UnexpectedValueException;
+use Webimpress\SafeWriter\Exception\ExceptionInterface as FileWriterException;
use function sprintf;
/**
@@ -30,4 +31,9 @@ class FileNotWritableException extends UnexpectedValueException implements Excep
$directory
));
}
+
+ public static function exception(FileWriterException $e) : self
+ {
+ return new self($e->getMessage(), $e->getCode(), $e);
+ }
}

MODIFY
src/ProxyManager/GeneratorStrategy/FileWriterGeneratorStrategy.php
src/ProxyManager/GeneratorStrategy/FileWriterGeneratorStrategy.php
@@ -7,17 +7,11 @@ namespace ProxyManager\GeneratorStrategy;
use Closure;
use ProxyManager\Exception\FileNotWritableException;
use ProxyManager\FileLocator\FileLocatorInterface;
+use Webimpress\SafeWriter\Exception\ExceptionInterface as FileWriterException;
+use Webimpress\SafeWriter\FileWriter;
use Zend\Code\Generator\ClassGenerator;
-use function chmod;
-use function dirname;
-use function file_put_contents;
-use function rename;
use function restore_error_handler;
use function set_error_handler;
-use function tempnam;
-use function trim;
-use function umask;
-use function unlink;
/**
* Generator strategy that writes the generated classes to disk while generating them
@@ -53,36 +47,13 @@ class FileWriterGeneratorStrategy implements GeneratorStrategyInterface
set_error_handler($this->emptyErrorHandler);
try {
- $this->writeFile("<?php\n\n" . $generatedCode, $fileName);
+ FileWriter::writeFile($fileName, "<?php\n\n" . $generatedCode);
return $generatedCode;
+ } catch (FileWriterException $e) {
+ throw FileNotWritableException::exception($e);
} finally {
restore_error_handler();
}
}
-
- /**
- * Writes the source file in such a way that race conditions are avoided when the same file is written
- * multiple times in a short time period
- *
- * @throws FileNotWritableException
- */
- private function writeFile(string $source, string $location) : void
- {
- $directory = dirname($location);
- $tmpFileName = tempnam($directory, 'temporaryProxyManagerFile');
-
- if ($tmpFileName === false) {
- throw FileNotWritableException::fromNotWritableDirectory($directory);
- }
-
- file_put_contents($tmpFileName, $source);
- chmod($tmpFileName, 0666 & ~umask());
-
- if (! rename($tmpFileName, $location)) {
- unlink($tmpFileName);
-
- throw FileNotWritableException::fromInvalidMoveOperation($tmpFileName, $location);
- }
- }
}

Feature: integration with webimpress/safe-writer

Use external library to write files