Skip to content

Commit

Permalink
Merge pull request #2036 from phalcon/fix/invalidate-cache
Browse files Browse the repository at this point in the history
Use a different path for the Kernel cache if possible
  • Loading branch information
sergeyklay authored Dec 26, 2019
2 parents 762e376 + 964eaf0 commit 75b425d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 46 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
There are no needs to dump it for every config change. Also, this patch
removes `Config::$changed` variable that is no longer needed
[#2035](https://github.com/phalcon/zephir/pull/2035)
- Use a different path for the Kernel cache if possible.
This patch fixes a cache collision issue. The issue is after creating the
cache and filling it with a project-specific configuration, there is no
way to invalidate it. Any next project will use the same Kernel cache and
the same Kernel configuration (if any).
[#2036](https://github.com/phalcon/zephir/pull/2036)

### Changed
- Improved type hint for arrays when generating stubs
Expand Down
61 changes: 45 additions & 16 deletions Library/DependencyInjection/ZephirKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
namespace Zephir\DependencyInjection;

use const DIRECTORY_SEPARATOR;
use Exception;
use Oneup\FlysystemBundle\OneupFlysystemBundle;
use RuntimeException;
use Symfony\Bundle\MonologBundle\MonologBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel;
use Throwable;
use Zephir\DependencyInjection\CompilerPass\CollectCommandsToApplicationCompilerPass;
use function Zephir\is_macos;
use function Zephir\is_windows;
Expand Down Expand Up @@ -61,7 +64,7 @@ public function registerBundles()
*
* @param LoaderInterface $loader
*
* @throws \Exception|\Throwable
* @throws Exception|Throwable
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
Expand All @@ -77,10 +80,8 @@ public function registerContainerConfiguration(LoaderInterface $loader)
*
* @return string
*/
public function getCacheDir()
public function getCacheDir(): string
{
// allows container rebuild when config or version changes
$hash = Zephir::VERSION.$this->environment.serialize($this->extraConfigFiles);
$home = getenv('HOME') ?: (getenv('HOMEDRIVE').DIRECTORY_SEPARATOR.getenv('HOMEPATH'));

if (is_macos()) {
Expand All @@ -94,20 +95,44 @@ public function getCacheDir()
$cacheDir .= '/zephir';
}

$path = $cacheDir.DIRECTORY_SEPARATOR.substr(md5($hash), 0, 16);
if (!is_dir($path) && !mkdir($path, 0777, true) && !is_dir($path)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $path));
$path = $cacheDir.DIRECTORY_SEPARATOR.$this->getPathSalt();
if (!is_dir($path) && !mkdir($path, 0755, true) && !is_dir($path)) {
throw new RuntimeException(
sprintf('Unable to create cache directory: "%s"', $path)
);
}

return $path;
}

/**
* Allows container rebuild when config or version changes.
* This also used to get unique path to kernel logs.
*
* @return string
*/
private function getPathSalt(): string
{
$prefix =
Zephir::VERSION.
$this->environment.
serialize($this->extraConfigFiles);

$localConfig = $this->startedDir.DIRECTORY_SEPARATOR.'config.json';
$suffix = $this->startedDir;
if (file_exists($localConfig)) {
$suffix = md5_file($localConfig);
}

return substr(md5("{$prefix}{$suffix}"), 0, 16);
}

/**
* {@inheritdoc}
*
* @return string
*/
public function getLogDir()
public function getLogDir(): string
{
$home = getenv('HOME') ?: (getenv('HOMEDRIVE').DIRECTORY_SEPARATOR.getenv('HOMEPATH'));

Expand All @@ -123,9 +148,11 @@ public function getLogDir()
$stateDir .= '/zephir';
}

$path = $stateDir.DIRECTORY_SEPARATOR.Zephir::VERSION;
if (!is_dir($path) && !mkdir($path, 0777, true) && !is_dir($path)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $path));
$path = $stateDir.DIRECTORY_SEPARATOR.$this->getPathSalt();
if (!is_dir($path) && !mkdir($path, 0755, true) && !is_dir($path)) {
throw new RuntimeException(
sprintf('Unable to create logs directory: "%s"', $path)
);
}

return $path;
Expand All @@ -136,15 +163,17 @@ public function getLogDir()
*
* @return string The project root dir
*/
public function getProjectDir()
public function getProjectDir(): string
{
return \dirname(\dirname(__DIR__));
return \dirname(__DIR__, 2);
}

/**
* {@inheritdoc}
*
* @return string
*/
public function getRootDir()
public function getRootDir(): string
{
return \dirname(__DIR__);
}
Expand All @@ -154,7 +183,7 @@ public function getRootDir()
*
* @return string The local cache dir
*/
public function getStartedDir()
public function getStartedDir(): string
{
return $this->startedDir.'/.zephir';
}
Expand All @@ -174,7 +203,7 @@ protected function build(ContainerBuilder $containerBuilder)
*
* @return array An array of kernel parameters
*/
protected function getKernelParameters()
protected function getKernelParameters(): array
{
$parameters = parent::getKernelParameters();
$parameters['kernel.local_cache_dir'] = $this->getStartedDir();
Expand Down
24 changes: 12 additions & 12 deletions Library/FileSystem/FileSystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface FileSystemInterface
*
* @return bool
*/
public function isInitialized();
public function isInitialized(): bool;

/**
* Initialize the filesystem.
Expand All @@ -32,7 +32,7 @@ public function initialize();
*
* @return bool
*/
public function exists($path);
public function exists(string $path): bool;

/**
* Creates a directory inside the temporary container.
Expand All @@ -41,7 +41,7 @@ public function exists($path);
*
* @return bool
*/
public function makeDirectory($path);
public function makeDirectory(string $path): bool;

/**
* Returns a temporary entry as an array.
Expand All @@ -50,7 +50,7 @@ public function makeDirectory($path);
*
* @return array
*/
public function file($path);
public function file(string $path): array;

/**
* Requires a file from the temporary directory.
Expand All @@ -59,7 +59,7 @@ public function file($path);
*
* @return mixed
*/
public function requireFile($path);
public function requireFile(string $path);

/**
* Attempts to remove recursively the temporary directory with all subdirectories and files.
Expand All @@ -72,7 +72,7 @@ public function clean();
* @param string $path
* @param string $data
*/
public function write($path, $data);
public function write(string $path, string $data);

/**
* Writes data from a temporary entry.
Expand All @@ -81,14 +81,14 @@ public function write($path, $data);
*
* @return string
*/
public function read($path);
public function read(string $path): string;

/**
* Deletes a temporary entry.
*
* @param string $path
*/
public function delete($path);
public function delete(string $path);

/**
* Generate a hash value using the contents of a given file.
Expand All @@ -99,7 +99,7 @@ public function delete($path);
*
* @return string
*/
public function getHashFile($algorithm, $sourceFile, $useCache = false);
public function getHashFile(string $algorithm, string $sourceFile, $useCache = false): string;

/**
* Returns the modification time of a temporary entry.
Expand All @@ -108,7 +108,7 @@ public function getHashFile($algorithm, $sourceFile, $useCache = false);
*
* @return int
*/
public function modificationTime($path);
public function modificationTime(string $path): int;

/**
* Executes a command and saves the result into a temporary entry.
Expand All @@ -117,7 +117,7 @@ public function modificationTime($path);
* @param string $descriptor
* @param string $destination
*/
public function system($command, $descriptor, $destination);
public function system(string $command, string $descriptor, string $destination);

/**
* Normalizes path to be used as a temporary entry.
Expand All @@ -126,5 +126,5 @@ public function system($command, $descriptor, $destination);
*
* @return string
*/
public function normalizePath($path);
public function normalizePath(string $path): string;
}
Loading

0 comments on commit 75b425d

Please sign in to comment.