Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a different path for the Kernel cache if possible #2036

Merged
merged 5 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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