-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from danieltoader/add_own_filewriter_to_avoid_…
…ocramius_permission_bug_from_2.1_and_up Add own filewriter to avoid ocramius permission bug from 2.1 and up
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ tests/app/logs/* | |
reports/ | ||
composer.phar | ||
composer.lock | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Emag\CacheBundle\ProxyManager\GeneratorStrategy; | ||
|
||
use ProxyManager\GeneratorStrategy\GeneratorStrategyInterface; | ||
use Zend\Code\Generator\ClassGenerator; | ||
use ProxyManager\Exception\FileNotWritableException; | ||
use ProxyManager\FileLocator\FileLocatorInterface; | ||
|
||
/** | ||
* Class FileWriter | ||
* | ||
* @package Emag\CacheBundle\ProxyManager\GeneratorStrategy | ||
*/ | ||
class FileWriter implements GeneratorStrategyInterface | ||
{ | ||
|
||
/** | ||
* @var \ProxyManager\FileLocator\FileLocatorInterface | ||
*/ | ||
protected $fileLocator; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
private $emptyErrorHandler; | ||
|
||
/** | ||
* @param \ProxyManager\FileLocator\FileLocatorInterface $fileLocator | ||
*/ | ||
public function __construct(FileLocatorInterface $fileLocator) | ||
{ | ||
$this->fileLocator = $fileLocator; | ||
$this->emptyErrorHandler = function () { | ||
}; | ||
} | ||
|
||
/** | ||
* Write generated code to disk and return the class code | ||
* | ||
* {@inheritDoc} | ||
* | ||
* @throws FileNotWritableException | ||
*/ | ||
public function generate(ClassGenerator $classGenerator): string | ||
{ | ||
$className = trim($classGenerator->getNamespaceName(), '\\') | ||
.'\\'.trim($classGenerator->getName(), '\\'); | ||
$generatedCode = $classGenerator->generate(); | ||
$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 | ||
* | ||
* @param string $source | ||
* @param string $location | ||
* | ||
* @throws FileNotWritableException | ||
*/ | ||
private function writeFile(string $source, string $location) | ||
{ | ||
$tmpFileName = tempnam($location, 'temporaryProxyManagerFile'); | ||
|
||
file_put_contents($tmpFileName, $source); | ||
chmod($tmpFileName, 0666 & ~umask()); | ||
|
||
if (!rename($tmpFileName, $location)) { | ||
unlink($tmpFileName); | ||
|
||
throw FileNotWritableException::fromInvalidMoveOperation($tmpFileName, $location); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters