- Small refractoring of code, merge tokens

- Completed block token
This commit is contained in:
Dave Mc Nicoll committed 2020-05-04 22:50:02 -04:00
1 parent bf3816c3db
commit ce4ef6351a
21 files changed
+370 -194

No files matched your search

+37 -11
View File
@@ -2,7 +2,8 @@
namespace Picea\Caching;
use Picea\Compiler\Context;
use Picea\Builder,
Picea\Compiler\Context;
class Opcache implements Cache {
@@ -11,10 +12,15 @@ class Opcache implements Cache {
protected string $cachePath = "";
protected array $compiled = [];
protected ? Context $context;
public function __construct(?string $cachePath = null)
public function __construct(?string $cachePath = null, ? Context $context)
{
$this->cachePath = $cachePath ?? sys_get_temp_dir();
$this->context = $context;
$this->registerNamespace($this->context->namespace);
}
public function load(string $viewPath, ...$arguments) : object
@@ -29,10 +35,6 @@ class Opcache implements Cache {
$compiledContext = $this->compiled[$viewPath];
if ( $compiledContext['extends'] ) {
$this->load($compiledContext['extends'], ...$arguments);
}
$fullName = isset($compiledContext['namespace']) ? "\\{$compiledContext['namespace']}\\{$compiledContext['classname']}" : $compiledContext['classname'];
return new $fullName(...$arguments);
@@ -40,7 +42,7 @@ class Opcache implements Cache {
public function save(Context $context) : bool
{
$fileName = $context->compiledClassPath();
$context->compiledClassPath();
$this->validateCachePath();
file_put_contents($this->cachePath($context->viewPath), $context->compiledSource);
@@ -55,9 +57,15 @@ class Opcache implements Cache {
if ( file_exists($path = $this->cachePath($viewPath)) ) {
$this->compiled[$viewPath] = include($path);
# if ( $this->compiled[$viewPath]['extends'] ?? false ) {
# $this->compiled($this->compiled[$viewPath]['extends']);
# }
return true;
}
return false;
}
@@ -73,8 +81,6 @@ class Opcache implements Cache {
throw new \RuntimeException(
sprintf("Given cache path `%s` is not writeable by `%s`", $fullPath, static::class)
);
return false;
}
return true;
@@ -82,6 +88,26 @@ class Opcache implements Cache {
protected function cachePath(string $fileName = "") : string
{
return implode(DIRECTORY_SEPARATOR, array_filter([ $this->cachePath, $this->cacheFolder, $fileName ? str_replace([ "/", DIRECTORY_SEPARATOR ], "~", $fileName . ".php") : null ]));
return implode(DIRECTORY_SEPARATOR, array_filter([ $this->cachePath, $this->cacheFolder, $fileName ? str_replace([ "/", DIRECTORY_SEPARATOR ], "~", Builder::generateClassName($fileName) . ".php") : null ]));
}
public function registerNamespace(string $namespace) {
spl_autoload_register(function ($class) use ($namespace) {
$prefix = "$namespace\\";
$baseDir = $this->cachePath() . "/";
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$file = $baseDir . str_replace('\\', '/', substr($class, $len)) . '.php';
if ( file_exists($file) ) {
require $file;
}
});
}
}