Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Added listener for flushing Str cache between requests #86

Merged
merged 10 commits into from
Feb 9, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ The format is based on [Keep a Changelog][keepachangelog] and this project adher
### Added

- Laravel 9 support [#78]
- Listener `FlushStrCacheListener` for flushing `Str` cache between requests [#86]

### Removed

- Laravel 6 and 7 is no longer supported [#78]

[#78]:https://github.com/spiral/roadrunner-laravel/pull/78
[#86]:https://github.com/spiral/roadrunner-laravel/pull/86

## v5.6.0

Expand Down
1 change: 1 addition & 0 deletions src/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static function afterLoopIteration(): array
Listeners\FlushDumperStackListener::class,
Listeners\FlushLogContextListener::class,
Listeners\FlushArrayCacheListener::class,
Listeners\FlushStrCacheListener::class,
Listeners\FlushMonologStateListener::class,
Listeners\FlushTranslatorCacheListener::class,
Listeners\ResetDatabaseRecordModificationStateListener::class,
Expand Down
33 changes: 33 additions & 0 deletions src/Listeners/FlushStrCacheListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerLaravel\Listeners;

use Illuminate\Support\Str;

/**
* @link https://github.com/laravel/octane/blob/1.x/src/Listeners/FlushStrCache.php
*/
class FlushStrCacheListener implements ListenerInterface
{
use Traits\InvokerTrait;

/**
* {@inheritdoc}
*/
public function handle($event): void
{
/**
* Method `flushCache` for the Str available since Laravel v8.81.0.
*
* @link https://github.com/illuminate/support/blob/v8.81.0/Str.php#L994
* @see \Illuminate\Support\Str::flushCache
*/
if (! $this->invokeStaticMethod($class = Str::class, 'flushCache')) {
foreach (['snakeCache', 'camelCache', 'studlyCache'] as $property) {
$this->setStaticProperty($class, $property, []);
}
}
}
}
53 changes: 53 additions & 0 deletions src/Listeners/Traits/InvokerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ final protected function invokeMethod(object $object, string $method_name, ...$a
return false;
}

/**
* Invoke the static method on class.
*
* @param class-string $class
* @param string $method_name
* @param mixed ...$args
*
* @return bool TRUE if the method exists and invoked, FALSE otherwise.
*/
final protected function invokeStaticMethod(string $class, string $method_name, ...$args): bool
{
if (\method_exists($class, $method_name)) {
$class::{$method_name}(...$args);

return true;
}

return false;
}

/**
* Change object property value (even protected or private). Black magic is used.
*
Expand All @@ -55,4 +75,37 @@ final protected function setProperty(object $object, string $property_name, $val

return $changed;
}

/**
* Change static object property value (even protected or private). Black magic is used.
*
* @param class-string $class
* @param string $property_name
* @param mixed $value
*
* @return bool TRUE if the property exists and changed, FALSE otherwise.
*/
final protected function setStaticProperty(string $class, string $property_name, $value): bool
{
$changed = false;

try {
$instance = (new \ReflectionClass($class))->newInstanceWithoutConstructor();

$closure = function () use ($value, $property_name, &$changed): void {
if (\property_exists($this, $property_name) && static::${$property_name} !== null) {
static::${$property_name} = $value;

$changed = true;
}
};

$reset = $closure->bindTo($instance, $instance);
$reset();
} catch (\ReflectionException $e) {
return false;
}

return $changed;
}
}
45 changes: 42 additions & 3 deletions tests/Unit/Listeners/AbstractListenerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ abstract protected function listenerFactory();
abstract protected function testHandle(): void;

/**
* @deprecated
*
* @param object $object
* @param string $property
*
Expand All @@ -53,8 +51,29 @@ protected function getProperty($object, string $property)
}

/**
* @deprecated
* @param class-string $class
* @param string $property
*
* @return mixed
* @throws \ReflectionException
*/
protected function getStaticProperty(string $class, string $property)
{
$result = null;

$closure = function () use ($property, &$result): void {
$result = static::${$property};
};

$instance = (new \ReflectionClass($class))->newInstanceWithoutConstructor();

$getter = $closure->bindTo($instance, $instance);
$getter();

return $result;
}

/**
* @param object $object
* @param string $property
* @param mixed $value
Expand All @@ -70,4 +89,24 @@ protected function setProperty($object, string $property, $value): void
$setter = $closure->bindTo($object, $object);
$setter();
}

/**
* @param class-string $class
* @param string $property
* @param mixed $value
*
* @return void
* @throws \ReflectionException
*/
protected function setStaticProperty(string $class, string $property, $value): void
{
$closure = function () use ($property, &$value): void {
static::${$property} = $value;
};

$instance = (new \ReflectionClass($class))->newInstanceWithoutConstructor();

$setter = $closure->bindTo($instance, $instance);
$setter();
}
}
42 changes: 42 additions & 0 deletions tests/Unit/Listeners/FlushStrCacheListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;

use Illuminate\Support\Str;
use Spiral\RoadRunnerLaravel\Listeners\FlushStrCacheListener;

/**
* @covers \Spiral\RoadRunnerLaravel\Listeners\FlushStrCacheListener
*/
class FlushStrCacheListenerTest extends AbstractListenerTestCase
{
/**
* {@inheritdoc}
*/
public function testHandle(): void
{
Str::snake('Hello world');
Str::camel('Hello world');
Str::studly('Hello world');

$this->assertNotEmpty($this->getStaticProperty($class = Str::class, 'snakeCache'));
$this->assertNotEmpty($this->getStaticProperty($class, 'camelCache'));
$this->assertNotEmpty($this->getStaticProperty($class, 'studlyCache'));

$this->listenerFactory()->handle(new \stdClass());

$this->assertEmpty($this->getStaticProperty($class, 'snakeCache'));
$this->assertEmpty($this->getStaticProperty($class, 'camelCache'));
$this->assertEmpty($this->getStaticProperty($class, 'studlyCache'));
}

/**
* @return FlushStrCacheListener
*/
protected function listenerFactory(): FlushStrCacheListener
{
return new FlushStrCacheListener();
}
}