Skip to content
Permalink
Browse files

wip

  • Loading branch information
rodolfoberrios committed Feb 4, 2020
1 parent 07c502b commit 0242ae891e85f680b7839cddc0de16ad6a63a725
Showing with 386 additions and 62 deletions.
  1. +2 −2 Chevere/Components/App/Build.php
  2. +5 −5 Chevere/Components/Cache/Cache.php
  3. +3 −3 Chevere/Components/Cache/Interfaces/CacheInterface.php
  4. +5 −5 Chevere/Components/Data/Tests/DataTest.php
  5. +34 −3 Chevere/Components/Filesystem/FileCompile.php
  6. +1 −1 Chevere/Components/Filesystem/FileReturn.php
  7. +12 −0 Chevere/Components/Message/Interfaces/MessageInterface.php
  8. +27 −25 Chevere/Components/Message/Message.php
  9. +23 −0 Chevere/Components/Router/Exceptions/RouteCacheTypeException.php
  10. +18 −0 Chevere/Components/Router/Interfaces/RouteCacheInterface.php
  11. +70 −0 Chevere/Components/Router/RouteCache.php
  12. +0 −1 Chevere/Components/Router/RouterProperties.php
  13. +98 −0 Chevere/Components/Router/Tests/RouteCacheTest.php
  14. +1 −0 Chevere/Components/Router/Tests/_resources/cached/0.php
  15. +1 −0 Chevere/Components/Router/Tests/_resources/cached/1.php
  16. 0 Chevere/Components/Router/Tests/_resources/empty/.keep
  17. 0 Chevere/Components/Router/Tests/_resources/working/.keep
  18. +1 −1 Chevere/Components/Runtime/Sets/SetAbstractHandler.php
  19. +1 −1 Chevere/Components/Runtime/Sets/SetTimeZone.php
  20. +2 −1 Chevere/Components/Stopwatch/Stopwatch.php
  21. +2 −2 Chevere/Components/Type/Type.php
  22. +1 −2 Chevere/Components/VarDump/Highlighters/ConsoleHighlight.php
  23. +5 −5 Chevere/Components/VarDump/VarDumpeable.php
  24. +11 −0 Chevere/Components/Variable/VariableExport.php
  25. +3 −2 composer.json
  26. +57 −0 psalm.xml
  27. +3 −3 utils/phpcheck.php
@@ -259,7 +259,7 @@ private function makeApi(): void
->withCache(
new Cache($this->dir->getChild(ApiInterface::CACHE_ID))
);
$this->checksums[ApiInterface::CACHE_ID] = $this->apiMaker->cache()->toArray();
$this->checksums[ApiInterface::CACHE_ID] = $this->apiMaker->cache()->puts();
}

private function makeRouter(): void
@@ -300,7 +300,7 @@ private function makeRouter(): void
->withServices($services);

$this->checksums = [
RouterInterface::CACHE_ID => $routerCache->cache()->toArray()
RouterInterface::CACHE_ID => $routerCache->cache()->puts()
];
}
}
@@ -56,19 +56,19 @@ public function __construct(DirInterface $dir)
$this->puts = [];
}

public function withPut(CacheKeyInterface $cacheKey, VariableExportInterface $variableExport): CacheInterface
public function withPut(CacheKeyInterface $key, VariableExportInterface $variableExport): CacheInterface
{
$path = $this->getPath($cacheKey->toString());
$path = $this->getPath($key->toString());
$file = new File($path);
if (!$file->exists()) {
$file->create();
}
$filePhp = new PhpFile($file);
$fileReturn = new FileReturn($filePhp);
$fileReturn->put($variableExport);
new FileCompile($filePhp);
(new FileCompile($filePhp))->compile();
$new = clone $this;
$new->puts[$cacheKey->toString()] = [
$new->puts[$key->toString()] = [
'path' => $fileReturn->filePhp()->file()->path()->absolute(),
'checksum' => $fileReturn->filePhp()->file()->checksum(),
];
@@ -119,7 +119,7 @@ public function get(CacheKeyInterface $cacheKey): CacheItemInterface
);
}

public function toArray(): array
public function puts(): array
{
return $this->puts;
}
@@ -19,7 +19,7 @@
use Chevere\Components\Variable\Interfaces\VariableExportInterface;
use Chevere\Components\Filesystem\Exceptions\File\FileUnableToRemoveException;

interface CacheInterface extends ToArrayInterface
interface CacheInterface
{
const ILLEGAL_KEY_CHARACTERS = '\.\/\\\~\:';

@@ -53,7 +53,7 @@ public function withPut(CacheKeyInterface $cacheKey, VariableExportInterface $va
public function withRemove(CacheKeyInterface $cacheKey): CacheInterface;

/**
* Returns a boolean indicating whether the alleged key exists in the cache.
* Returns a boolean indicating whether the cache exists for the given key.
*/
public function exists(CacheKeyInterface $cacheKey): bool;

@@ -67,5 +67,5 @@ public function get(CacheKeyInterface $cacheKey): CacheItemInterface;
/**
* @return array [key => [checksum => , path =>]]
*/
public function toArray(): array;
public function puts(): array;
}
@@ -54,7 +54,7 @@ public function testWithArray(): void
$array = $this->getBasicArray();
$mixed = $this->getMixedArray();
$data = (new Data($array))
->withArray($mixed);
->withArray($mixed);
$this->assertSame($mixed, $data->toArray());
}

@@ -64,15 +64,15 @@ public function testWithMergedArray(): void
$mixed = $this->getMixedArray();
$merge = array_merge_recursive($mixed, $array);
$data = (new Data($mixed))
->withMergedArray($array);
->withMergedArray($array);
$this->assertSame($merge, $data->toArray());
}

public function testWithAppend(): void
{
$array = $this->getBasicArray();
$data = (new Data($array))
->withAppend('var');
->withAppend('var');
$expected = $array;
$expected[] = 'var';
$this->assertSame($expected, $data->toArray());
@@ -84,7 +84,7 @@ public function testWithAddedKey(): void
$value = ['value', 1];
$array = $this->getMixedArray();
$data = (new Data($array))
->withAddedKey('test', $value);
->withAddedKey('test', $value);
$this->assertTrue($data->hasKey('test'));
$this->assertSame($value, $data->key('test'));
}
@@ -95,7 +95,7 @@ public function testWithRemovedkey(): void
$data = new Data($array);
$this->assertTrue($data->hasKey('key'));
$data = $data
->withRemovedKey('key');
->withRemovedKey('key');
$this->assertFalse($data->hasKey('key'));
}
}
@@ -18,6 +18,7 @@
use Chevere\Components\Message\Message;
use Chevere\Components\Filesystem\Interfaces\File\FileCompileInterface;
use Chevere\Components\Filesystem\Interfaces\File\FilePhpInterface;
use Throwable;

/**
* OPCache compiler.
@@ -29,11 +30,30 @@ final class FileCompile implements FileCompileInterface
/**
* Applies OPCache to the PHP file.
*
* @throws RuntimeException If Zend OPcache is not available
* @throws FileNotPhpException if $file is not a PHP file
* @throws FileNotFoundException if $file doesn't exists
*/
public function __construct(FilePhpInterface $filePhp)
{
if (!extension_loaded('Zend OPcache')) {
throw new RuntimeException(
(new Message('Extension %extension% is not loaded'))
->code('%extension%', 'Zend OPcache')
->toString()
);
}

if (ini_get('opcache.enable') === '0' || ini_get('opcache.enable_cli') === '0') {
throw new RuntimeException(
(new Message('Extension %extension% must be enabled with %enable% and %enableCli% in a parsed configuration file (%iniFile%)'))
->code('%extension%', 'Zend OPcache')
->code('%enable%', 'opcache.enable = 1')
->code('%enableCli%', 'opcache.enable_cli = 1')
->code('%iniFile%', '.ini')
->toString()
);
}
$this->filePhp = $filePhp;
}

@@ -48,18 +68,29 @@ public function compile(): void
$path = $this->filePhp->file()->path()->absolute();
$past = BootstrapInstance::get()->time() - 10;
touch($path, $past);
/** @scrutinizer ignore-unhandled */ @opcache_invalidate($path, true);
if (!opcache_compile_file($path)) {
try {
if (!opcache_compile_file($path)) {
throw new RuntimeException(
(new Message('Zend OPcache is disabled'))
->toString()
);
}
} catch (Throwable $e) {
throw new RuntimeException(
(new Message('Unable to compile cache for file %path% (Opcache is disabled)'))
(new Message('Unable to compile cache for file %path%'))
->code('%path%', $path)
->code('%thrown%', $e->getMessage())
->toString()
);
}
}

public function destroy(): void
{
if (!opcache_is_script_cached($this->filePhp->file()->path()->absolute())) {

return;
}
if (!opcache_invalidate($this->filePhp->file()->path()->absolute())) {
throw new RuntimeException(
(new Message('Opcode cache is disabled'))
@@ -100,7 +100,7 @@ public function put(VariableExportInterface $variableExport): void

private function getReturnVar($var)
{
if (is_string($var)) {
if (is_string($var) && !ctype_space($var)) {
try {
$unserialize = new Unserialize($var);
$var = $unserialize->var();
@@ -25,6 +25,18 @@ public function __construct(string $message);
*/
public function strtr(string $search, string $replace): MessageInterface;

/**
* @param string $search the value being searched for, otherwise known as the needle
* @param string $replace the replacement value that replaces found search value
*/
public function code(string $search, string $replace): MessageInterface;

/**
* @param string $search the value being searched for, otherwise known as the needle
* @param string $replace the replacement value that replaces found search value
*/
public function strong(string $search, string $replace): MessageInterface;

/**
* @return string The message after the translation table, cli aware.
*/
@@ -13,7 +13,6 @@

namespace Chevere\Components\Message;

use Chevere\Components\App\Instances\BootstrapInstance;
use Chevere\Components\Message\Interfaces\MessageInterface;
use JakubOnderka\PhpConsoleColor\ConsoleColor;

@@ -27,18 +26,15 @@
*/

/**
* @method Message MessageContact code(string $search, string $replace) Wraps found $replace in a `code` tag
* @method Message MessageContact strong(string $search, string $replace) Wraps found $replace in a `strong` tag
* @method Message MessageContact *any*(string $search, string $replace) Wraps found $replace in a `any` tag
* @method Message MessageInterface code(string $search, string $replace) Wraps found $replace in a `code` tag
* @method Message MessageInterface strong(string $search, string $replace) Wraps found $replace in a `strong` tag
*/
final class Message implements MessageInterface
{
private string $message;

private ConsoleColor $consoleColor;

private string $string;

/** @var array Translation table [search => replace] */
private array $trTable = [];

@@ -58,40 +54,33 @@ public function __construct(string $message)
$this->consoleColor = new ConsoleColor;
}

/**
* Magic call method for wrap tags.
*
* @param string $tag Tag name
* @param array $args the arguments, being $args[0] (from) and $args[1] (to)
*/
public function __call(string $tag, array $args): MessageInterface
public function strtr(string $search, string $replace): MessageInterface
{
$search = (string) $args[0]; // $search String to replace for
$replace = (string) $args[1]; // $replace String to replace with
$tagged = '' != $replace ? "<$tag>$replace</$tag>" : '';
$new = clone $this;
$new->trTable[$search] = $replace;
$new->message = strtr($new->message, $new->trTable);

return $new->strtr($search, $tagged);
return $new;
}

public function strtr(string $search, string $replace): MessageInterface
public function code(string $search, string $replace): MessageInterface
{
$new = clone $this;
$new->trTable[$search] = $replace;
$new->message = strtr($new->message, $new->trTable);

return $new;
return $new->wrap($search, $replace);
}

public function toString(): string
public function strong(string $search, string $replace): MessageInterface
{
return $this->string ??= $this->cliAware();
$new = clone $this;

return $new->wrap($search, $replace);
}

private function cliAware(): string

public function toString(): string
{
$message = $this->message;
// $hasBoo
// if (BootstrapInstance::get()->isCli()) {
// foreach ($this->consolePallete as $tag => $color) {
// $message = preg_replace_callback('#<' . $tag . '>(.*?)<\/' . $tag . '>#', function ($matches) use ($color) {
@@ -102,4 +91,17 @@ private function cliAware(): string

return $message;
}

private function wrap(string $search, string $replace): MessageInterface
{
$tagged = $replace;
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
if ($bt[1]['function'] && is_string($bt[1]['function'])) {
$tag = $bt[1]['function'];
$tagged = "<$tag>$replace</$tag>";
}
$new = clone $this;

return $new->strtr($search, $tagged);
}
}
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Components\Router\Exceptions;

use Exception;

/**
* Exception thrown when the cached route doesn't match the expected type.
*/
final class RouteCacheTypeException extends Exception
{
}
@@ -0,0 +1,18 @@
<?php

namespace Chevere\Components\Router\Interfaces;

use Chevere\Components\Route\Interfaces\RouteInterface;

interface RouteCacheInterface
{
public function has(int $id): bool;

public function get(int $id): RouteInterface;

public function put(int $id, RouteableInterface $routeable): RouteCacheInterface;

public function remove(int $id): RouteCacheInterface;

public function puts(): array;
}

0 comments on commit 0242ae8

Please sign in to comment.
You can’t perform that action at this time.