-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
FileWriterGeneratorStrategy.php
88 lines (74 loc) · 2.48 KB
/
FileWriterGeneratorStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace ProxyManager\GeneratorStrategy;
use Closure;
use ProxyManager\Exception\FileNotWritableException;
use ProxyManager\FileLocator\FileLocatorInterface;
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
*
* {@inheritDoc}
*/
class FileWriterGeneratorStrategy implements GeneratorStrategyInterface
{
protected FileLocatorInterface $fileLocator;
private Closure $emptyErrorHandler;
public function __construct(FileLocatorInterface $fileLocator)
{
$this->fileLocator = $fileLocator;
$this->emptyErrorHandler = static function () : void {
};
}
/**
* Write generated code to disk and return the class code
*
* {@inheritDoc}
*
* @throws FileNotWritableException
*/
public function generate(ClassGenerator $classGenerator) : string
{
/** @var string $generatedCode */
$generatedCode = $classGenerator->generate();
$className = $classGenerator->getNamespaceName() . '\\' . $classGenerator->getName();
$fileName = $this->fileLocator->getProxyFileName($className);
set_error_handler($this->emptyErrorHandler);
try {
$this->writeFile("<?php\n\n" . $generatedCode, $fileName);
return $generatedCode;
} 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);
}
}
}