Skip to content

Commit

Permalink
Merge pull request #33 from danieltoader/add_own_filewriter_to_avoid_…
Browse files Browse the repository at this point in the history
…ocramius_permission_bug_from_2.1_and_up

Add own filewriter to avoid ocramius permission bug from 2.1 and up
  • Loading branch information
Belibov committed Oct 1, 2018
2 parents d5b98a4 + 0e052e0 commit cdb6a68
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ tests/app/logs/*
reports/
composer.phar
composer.lock
.idea/
87 changes: 87 additions & 0 deletions src/ProxyManager/GeneratorStrategy/FileWriter.php
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/services.yml
Expand Up @@ -5,7 +5,7 @@ parameters:
emag.cache.proxy.manager.class: Emag\CacheBundle\ProxyManager\Factory\ProxyCachingObjectFactory
emag.cache.proxy.generator.class: Emag\CacheBundle\ProxyManager\ProxyGenerator\CachedObjectGenerator
emag.cache.proxy.configuration.class: ProxyManager\Configuration
emag.cache.proxy.persister.class: ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy
emag.cache.proxy.persister.class: Emag\CacheBundle\ProxyManager\GeneratorStrategy\FileWriter
emag.cache.proxy.locator.class: ProxyManager\FileLocator\FileLocator
emag.cache.proxy.cache-locator.class: Symfony\Component\DependencyInjection\ServiceLocator

Expand Down

0 comments on commit cdb6a68

Please sign in to comment.