Skip to content

Commit

Permalink
Fix for ocramius 2 1+ file permission bug in emag cache bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltoader committed Oct 1, 2018
1 parent d5b98a4 commit 90fa8d5
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
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
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);
}
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
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 90fa8d5

Please sign in to comment.