- Base is done; most of the language's components are now working!

- Implemented OPCACHE caching
- Added View() token logic
- New FileFetcher added to gather files from given view path
This commit is contained in:
mcndave committed 2019-11-01 14:46:01 -04:00
1 parent bcd49884fc
commit 6701a1cd59
16 files changed
+323 -69

No files matched your search

+54 -21
View File
@@ -2,48 +2,76 @@
namespace Picea\Caching;
use Picea\Compiler\Context;
class Opcache implements Cache {
protected string $cacheFolder = "picea";
protected string $cachePath = "";
protected array $compiled = [];
public function __construct(?string $cachePath = null)
{
$this->cachePath = $cachePath ?? sys_get_temp_dir();
}
public function handle(string $fileName) : bool
public function load(string $viewPath, ...$arguments) : object
{
if ( file_exists($path = $this->filePath($fileName)) ) {
require_once($path);
if ( empty($viewPath) ) {
throw new \RuntimeException("Given view path is empty.");
}
if ( ! $this->compiled($viewPath) ) {
throw new \RuntimeException("You must compile your view `$viewPath` before trying to load it.");
}
$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);
}
public function save(Context $context) : bool
{
$fileName = $context->compiledClassPath();
$this->validateCachePath();
file_put_contents($this->cachePath($context->viewPath), $context->compiledSource);
return false;
}
public function compiled(string $viewPath) : bool
{
if ( false !== ( $this->compiled[$viewPath] ?? false ) ) {
return true;
}
if ( file_exists($path = $this->cachePath($viewPath)) ) {
$this->compiled[$viewPath] = require_once($path);
return true;
}
return false;
}
public function saveFile(string $fileName, string $content) : bool
{
$this->validateCachePath();
file_put_contents($this->filePath($fileName), $content);
return false;
}
public function filePath(string $fileName) : string
{
return $this->cachePath . $fileName;
}
protected function validateCachePath() : bool
{
if ( ! file_exists($this->cachePath) ) {
mkdir($this->cachePath, 0750, true);
$fullPath = $this->cachePath();
if ( ! file_exists($fullPath) ) {
mkdir($fullPath, 0750, true);
}
if ( ! is_writeable($this->cachePath) ) {
if ( ! is_writeable($fullPath) ) {
throw new \RuntimeException(
sprintf("Given cache path `%s` is not writeable by `%s`", $this->cachePath, static::class)
sprintf("Given cache path `%s` is not writeable by `%s`", $fullPath, static::class)
);
return false;
@@ -51,4 +79,9 @@ class Opcache implements Cache {
return true;
}
protected function cachePath(string $fileName = "") : string
{
return implode(DIRECTORY_SEPARATOR, array_filter([ $this->cachePath, $this->cacheFolder, $fileName ? str_replace([ "/", DIRECTORY_SEPARATOR ], "_", $fileName . ".php") : null ]));
}
}