-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
318 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kdyby\Autowired\Caching; | ||
|
||
use Nette; | ||
|
||
final class Cache | ||
{ | ||
|
||
private Nette\Caching\Cache $cache; | ||
|
||
/** | ||
* @var class-string<Nette\Application\UI\Component> | ||
*/ | ||
private string $componentClass; | ||
|
||
private string $containerFile; | ||
|
||
/** | ||
* @param Nette\Caching\Cache $cache | ||
* @param class-string<Nette\Application\UI\Component> $componentClass | ||
* @param string $containerFile | ||
*/ | ||
public function __construct(Nette\Caching\Cache $cache, string $componentClass, string $containerFile) | ||
{ | ||
$this->cache = $cache; | ||
$this->componentClass = $componentClass; | ||
$this->containerFile = $containerFile; | ||
} | ||
|
||
public function load(): mixed | ||
{ | ||
return $this->cache->load($this->getCacheKey()); | ||
} | ||
|
||
public function save(mixed $value): void | ||
{ | ||
$this->cache->save( | ||
$this->getCacheKey(), | ||
$value, | ||
[ | ||
Nette\Caching\Cache::FILES => $this->getFileDependencies(), | ||
], | ||
); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
private function getCacheKey(): array | ||
{ | ||
return [$this->componentClass, $this->containerFile]; | ||
} | ||
|
||
/** | ||
* @return list<string> | ||
*/ | ||
private function getFileDependencies(): array | ||
{ | ||
/** @var list<class-string> $nettePresenterParents */ | ||
$nettePresenterParents = class_parents(Nette\Application\UI\Presenter::class); | ||
assert(is_array($nettePresenterParents)); | ||
$ignore = $nettePresenterParents + ['ui' => Nette\Application\UI\Presenter::class]; | ||
|
||
/** @var list<class-string> $componentParents */ | ||
$componentParents = class_parents($this->componentClass); | ||
assert(is_array($componentParents)); | ||
$files = array_map( | ||
fn (string $class): string => (string) (new \ReflectionClass($class))->getFileName(), | ||
array_diff($componentParents + ['me' => $this->componentClass], $ignore), | ||
); | ||
|
||
$files[] = $this->containerFile; | ||
|
||
return array_values(array_unique($files)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kdyby\Autowired\Caching; | ||
|
||
use Nette; | ||
|
||
final class CacheFactory | ||
{ | ||
|
||
private Nette\Caching\Storage $cacheStorage; | ||
|
||
private string $containerFile; | ||
|
||
public function __construct(Nette\Caching\Storage $cacheStorage, string $containerFile) | ||
{ | ||
$this->cacheStorage = $cacheStorage; | ||
$this->containerFile = $containerFile; | ||
} | ||
|
||
public static function fromContainer(Nette\DI\Container $container, ?Nette\Caching\Storage $cacheStorage = NULL): self | ||
{ | ||
$containerFile = (string) (new \ReflectionClass($container))->getFileName(); | ||
$cacheStorage ??= $container->getByType(Nette\Caching\Storage::class); | ||
return new self($cacheStorage, $containerFile); | ||
} | ||
|
||
/** | ||
* @param class-string<Nette\Application\UI\Component> $componentClass | ||
* @param string $namespace | ||
* @return Cache | ||
*/ | ||
public function create(string $componentClass, string $namespace): Cache | ||
{ | ||
$cache = new Nette\Caching\Cache($this->cacheStorage, $namespace); | ||
return new Cache($cache, $componentClass, $this->containerFile); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/KdybyTests/Autowired/IntegrationFixtures/IntegrationControl.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace KdybyTests\Autowired\IntegrationFixtures; | ||
|
||
class IntegrationControl extends BaseControl | ||
{ | ||
|
||
/** | ||
* @autowire(factory=\KdybyTests\Autowired\IntegrationFixtures\DatagridFactory) | ||
*/ | ||
public DatagridComponent $factoryResult; | ||
|
||
protected function createComponentSilly(DatagridFactory $factory): DatagridComponent | ||
{ | ||
return $factory->create(); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
tests/KdybyTests/Autowired/IntegrationFixtures/TestStorage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace KdybyTests\Autowired\IntegrationFixtures; | ||
|
||
use Nette; | ||
|
||
final class TestStorage implements Nette\Caching\Storage | ||
{ | ||
|
||
/** | ||
* @var array<string, array{value: mixed, dependencies: array<string, mixed>}> | ||
*/ | ||
public array $records = []; | ||
|
||
public function read(string $key): mixed | ||
{ | ||
return $this->records[$key]['value'] ?? NULL; | ||
} | ||
|
||
public function lock(string $key): void | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param mixed $data | ||
* @param array<string, mixed> $dependencies | ||
*/ | ||
public function write(string $key, mixed $data, array $dependencies): void | ||
{ | ||
$this->records[$key] = ['value' => $data, 'dependencies' => $dependencies]; | ||
} | ||
|
||
public function remove(string $key): void | ||
{ | ||
unset($this->records[$key]); | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $conditions | ||
*/ | ||
public function clean(array $conditions): void | ||
{ | ||
if ($conditions[Nette\Caching\Cache::ALL] === TRUE) { | ||
$this->records = []; | ||
} | ||
} | ||
|
||
/** | ||
* @return array<string, array{value: mixed, dependencies: array<string, mixed>}> | ||
*/ | ||
public function getRecords(): array | ||
{ | ||
return $this->records; | ||
} | ||
|
||
} |
Oops, something went wrong.