Skip to content

Commit

Permalink
AgnosticRoutesLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 24, 2021
1 parent e62324b commit 1661ed0
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 4 deletions.
90 changes: 87 additions & 3 deletions Services/Agnostic/RoutesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
use Symfony\Component\Config\ResourceCheckerConfigCache;
use Symfony\Component\Config\ResourceCheckerConfigCacheFactory;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\Loader\XmlFileLoader;
Expand All @@ -30,6 +32,26 @@ class RoutesLoader
*/
private $router;

/**
* @var ResourceCheckerConfigCacheFactory $cacheFactory
*/
private $cacheFactory;

/**
* @var SelfCheckingResourceChecker $checker
*/
private $checker;

/**
* @var ResourceCheckerConfigCache $cacheFreshChecker
*/
private $cacheFreshChecker;

/**
* @var string|null $cacheDir Путь к кэшу. Null -> не кэшировать.
*/
private $cacheDir;

/**
* AgnosticRouteLoader constructor.
*
Expand All @@ -42,6 +64,8 @@ public function __construct(
?string $cacheDir = null,
bool $debug = true
) {
$this->cacheDir = $cacheDir;

$resolver = new LoaderResolver(
[
new YamlFileLoader(new FileLocator()),
Expand All @@ -55,8 +79,8 @@ public function __construct(
$requestContext = new RequestContext();
$request = Request::createFromGlobals();

$checker = new SelfCheckingResourceChecker();
$cacheFactory = new ResourceCheckerConfigCacheFactory([$checker]);
$this->checker = new SelfCheckingResourceChecker();
$this->cacheFactory = new ResourceCheckerConfigCacheFactory([$this->checker]);

$this->router = new Router(
$delegatingLoader,
Expand All @@ -73,7 +97,12 @@ public function __construct(
);

if ($cacheDir) {
$this->router->setConfigCacheFactory($cacheFactory);
$this->cacheFreshChecker = new ResourceCheckerConfigCache(
$this->cacheDir . '/url_generating_routes.php',
[$this->checker]
);

$this->warmUpCache();
}
}

Expand All @@ -84,6 +113,61 @@ public function __construct(
*/
public function getRoutes() : RouteCollection
{
if ($this->cacheDir) {
$compiledRoutesFile = $this->cacheDir . '/route_collection.json';

if ($this->cacheFreshChecker !== null
&&
$this->cacheFreshChecker->isFresh()
&&
@file_exists($compiledRoutesFile)) {
$collection = file_get_contents($compiledRoutesFile);
if ($collection) {
$collection = unserialize($collection);
return $collection;
}
}
}

return $this->router->getRouteCollection();
}

/**
* Удалить кэш.
*
* @return void
*/
public function purgeCache() : void
{
$filesystem = new Filesystem();

if (!$filesystem->exists($this->cacheDir)) {
return;
}

$filesystem->remove($this->cacheDir);
}

/**
* Создать (если надо), кэш.
*
* @return void
*/
private function warmUpCache() : void
{
$this->router->setConfigCacheFactory($this->cacheFactory);

if (!$this->cacheFreshChecker->isFresh()) {
if (!@file_exists($this->cacheDir)) {
@mkdir($this->cacheDir, 0777);
}

file_put_contents(
$this->cacheDir . '/route_collection.json',
serialize($this->router->getRouteCollection())
);
}

$this->router->getGenerator(); // Трюк по созданию кэша.
}
}
76 changes: 75 additions & 1 deletion Tests/Cases/AgnosticRoutesLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesLoader;
use Prokl\TestingTools\Base\BaseTestCase;
use Symfony\Component\Filesystem\Filesystem;

/**
* Class AgnosticRoutesLoaderTest
Expand All @@ -19,19 +20,49 @@ class AgnosticRoutesLoaderTest extends BaseTestCase
*/
protected $obTestObject;

/**
* @var Filesystem $filesystem Файловая система.
*/
private $filesystem;

/**
* @var string $cacheDir
*/
private $cacheDir = __DIR__ . '/../Fixture/cache';

/**
* @var string $routesConfig
*/
private $routesConfig = __DIR__ . '/../Fixture/bitrix_routes.yaml';

/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();

$this->filesystem = new Filesystem();

$this->obTestObject = new RoutesLoader(
__DIR__ . '/../Fixture/bitrix_routes.yaml',
$this->routesConfig,
null,
true
);
}

/**
* @inheritdoc
*/
protected function tearDown(): void
{
parent::tearDown();

if ($this->filesystem->exists($this->cacheDir)) {
$this->filesystem->remove($this->cacheDir);
}
}

/**
* getRoutes().
*
Expand All @@ -45,4 +76,47 @@ public function testGetRoutes() : void

$this->assertSame($routes->getPath(), '/foo/{param}/');
}


/**
* Caching.
*
* @return void
*/
public function testCaching() : void
{
$this->obTestObject = new RoutesLoader(
$this->routesConfig,
$this->cacheDir,
true
);

$this->assertFileExists($this->cacheDir . '/route_collection.json');
$this->assertFileExists($this->cacheDir . '/url_generating_routes.php');
$this->assertFileExists($this->cacheDir . '/url_generating_routes.php.meta');
}

/**
* purgeCache().
*
* @return void
*/
public function testPurgeCache(): void
{
$this->obTestObject = new RoutesLoader(
$this->routesConfig,
$this->cacheDir,
true
);

if (!$this->filesystem->exists($this->cacheDir)) {
@mkdir($this->cacheDir);
}

file_put_contents($this->cacheDir . '/test', 'OK');

$this->obTestObject->purgeCache();

$this->assertDirectoryDoesNotExist($this->cacheDir);
}
}

0 comments on commit 1661ed0

Please sign in to comment.