Skip to content

Commit

Permalink
Fixed #473
Browse files Browse the repository at this point in the history
Migrate from `cache/cache` to `symfony/cache`
  • Loading branch information
Anton Shevchuk committed Jul 5, 2021
1 parent 153ccc2 commit b40e303
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"ext-json": "*",
"ext-ctype": "*",
"bluzphp/collection": "~1.0",
"cache/cache": "~1.0",
"psr/log": "~1.1",
"laminas/laminas-diactoros": "~2.6",
"laminas/laminas-httphandlerrunner": "~1.4"
"laminas/laminas-httphandlerrunner": "~1.4",
"symfony/cache": "^5.3"
},
"require-dev": {
"codeception/codeception": "~4.1",
Expand All @@ -27,7 +27,7 @@
"ext-redis": "required by Redis adapter for Bluz\\Cache (https://github.com/bluzphp/framework/wiki/Cache)",
"ext-memcached": "required by Memcached adapter for Bluz\\Cache (https://github.com/bluzphp/framework/wiki/Cache)",
"phpmailer/phpmailer": "required by Bluz\\Mailer (https://github.com/bluzphp/framework/wiki/Mailer)",
"predis/predis": "required by Cache\\Adapter\\Predis (https://github.com/bluzphp/framework/wiki/Cache)"
"predis/predis": "required by Redis adapter for Bluz\\Cache (https://github.com/bluzphp/framework/wiki/Cache)"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 0 additions & 2 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public function getEnvironment(): string
* Get path to Application
*
* @return string
* @throws ReflectionException
*/
public function getPath(): string
{
Expand Down Expand Up @@ -180,7 +179,6 @@ public function init(string $environment = 'production'): void
*
* @return void
* @throws ConfigException
* @throws ReflectionException
*/
protected function initConfig(): void
{
Expand Down
12 changes: 4 additions & 8 deletions src/Proxy/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Bluz\Proxy;

use Bluz\Common\Exception\ComponentException;
use Cache\Hierarchy\HierarchicalPoolInterface;
use Cache\TagInterop\TaggableCacheItemPoolInterface as Instance;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface as Instance;
use Psr\Cache\InvalidArgumentException;

/**
Expand Down Expand Up @@ -175,12 +175,10 @@ public static function prepare(string $key): string
*
* @return bool
* @throws InvalidArgumentException
* @see TaggableCacheItemPoolInterface::invalidateTag()
*
*/
public static function clearTag(string $tag): bool
{
if (self::getInstance() instanceof HierarchicalPoolInterface) {
if (self::getInstance() instanceof TagAwareAdapterInterface) {
return self::getInstance()->invalidateTag($tag);
}
return false;
Expand All @@ -193,12 +191,10 @@ public static function clearTag(string $tag): bool
*
* @return bool
* @throws InvalidArgumentException
* @see TaggableCacheItemPoolInterface::invalidateTags()
*
*/
public static function clearTags(array $tags): bool
{
if (self::getInstance() instanceof HierarchicalPoolInterface) {
if (self::getInstance() instanceof TagAwareAdapterInterface) {
return self::getInstance()->invalidateTags($tags);
}
return false;
Expand Down
29 changes: 17 additions & 12 deletions src/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ class Session
*/
protected $namespace = 'bluz';

/**
* @var string Session handler name
*/
protected $adapter = 'files';

/**
* @var SessionHandlerInterface Session save handler
*/
protected $adapter;
protected $sessionHandler;

/**
* Attempt to set the session name
Expand Down Expand Up @@ -197,7 +202,7 @@ public function start(): bool
return true;
}

if ($this->initAdapter()) {
if ($this->init()) {
return session_start();
}

Expand Down Expand Up @@ -225,23 +230,23 @@ public function destroy(): void
}

/**
* Set session save handler object
* Set session handler name
*
* @param SessionHandlerInterface|string $saveHandler
* @param string $adapter
*
* @return void
*/
public function setAdapter($saveHandler): void
public function setAdapter(string $adapter): void
{
$this->adapter = $saveHandler;
$this->adapter = $adapter;
}

/**
* Get SaveHandler Object
* Get session handler name
*
* @return SessionHandlerInterface
* @return string
*/
public function getAdapter(): SessionHandlerInterface
public function getAdapter(): string
{
return $this->adapter;
}
Expand All @@ -255,16 +260,16 @@ public function getAdapter(): SessionHandlerInterface
* @return bool
* @throws ComponentException
*/
protected function initAdapter(): bool
protected function init(): bool
{
if (null === $this->adapter || 'files' === $this->adapter) {
if ('files' === $this->adapter) {
// try to apply settings
if ($settings = $this->getOption('settings', 'files')) {
$this->setSavePath($settings['save_path']);
}
return true;
}
if (is_string($this->adapter)) {
if (null === $this->sessionHandler) {
$adapterClass = '\\Bluz\\Session\\Adapter\\' . ucfirst($this->adapter);
if (!class_exists($adapterClass) || !is_subclass_of($adapterClass, SessionHandlerInterface::class)) {
throw new ComponentException("Class for session adapter `{$this->adapter}` not found");
Expand Down
17 changes: 7 additions & 10 deletions tests/configs/default/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@
'adapter' => 'memcached',
'pools' => [
/**
* @link https://github.com/php-cache/apc-adapter
* @link https://symfony.com/doc/current/components/cache/adapters/apcu_adapter.html
*/
'apc' => function () {
return new \Cache\Adapter\Apc\ApcCachePool();
'apcu' => function () {
return new Symfony\Component\Cache\Adapter\ApcuAdapter('bluz');
},
/**
* @link https://github.com/php-cache/filesystem-adapter
* @link https://symfony.com/doc/current/components/cache/adapters/filesystem_adapter.html
*/
'filesystem' => function () {
$filesystemAdapter = new \League\Flysystem\Adapter\Local(PATH_DATA . '/cache');
$filesystem = new \League\Flysystem\Filesystem($filesystemAdapter);

return new \Cache\Adapter\Filesystem\FilesystemCachePool($filesystem);
return new Symfony\Component\Cache\Adapter\FilesystemTagAwareAdapter('bluz', 0, PATH_DATA . '/cache');
},
/**
* @link https://github.com/php-cache/predis-adapter
* @link https://symfony.com/doc/current/components/cache/adapters/redis_adapter.html
* @link https://github.com/nrk/predis/wiki/Connection-Parameters
* @link https://github.com/nrk/predis/wiki/Client-Options
*/
'predis' => function () {
$client = new \Predis\Client('tcp:/127.0.0.1:6379');
return new \Cache\Adapter\Predis\PredisCachePool($client);
return new Symfony\Component\Cache\Adapter\RedisTagAwareAdapter($client, 'bluz');
}
]
];

0 comments on commit b40e303

Please sign in to comment.