Skip to content

Commit

Permalink
AgnosticRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 24, 2021
1 parent 5795b9b commit 96412a1
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Services/Agnostic/BitrixInitializerRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Prokl\BitrixSymfonyRouterBundle\Services\Agnostic;

use Prokl\BitrixSymfonyRouterBundle\Services\Router\InitRouter;
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\Contracts\RouterInitializerInterface;

/**
* Class BitrixInitializerRouter
* @package Prokl\BitrixSymfonyRouterBundle\Services\Agnostic
*
* @since 24.07.2021
*/
class BitrixInitializerRouter implements RouterInitializerInterface
{
/**
* @inheritDoc
*/
public function init(InitRouter $router)
{
AddEventHandler('main', 'OnProlog', [$router, 'handle']);
}
}
23 changes: 23 additions & 0 deletions Services/Agnostic/Contracts/RouterInitializerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Prokl\WpSymfonyRouterBundle\Services\Agnostic\Contracts;

use Prokl\BitrixSymfonyRouterBundle\Services\Router\InitRouter;

/**
* Interface Prokl\WpSymfonyRouterBundle\Services\Agnostic\Contracts
* @package Local\Bitrix
*
* @since 24.07.2021
*/
interface RouterInitializerInterface
{
/**
* Инициализация роутера.
*
* @param InitRouter $router Инициализированный роутер.
*
* @return mixed
*/
public function init(InitRouter $router);
}
72 changes: 72 additions & 0 deletions Services/Agnostic/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Prokl\BitrixSymfonyRouterBundle\Services\Agnostic;

use Prokl\BitrixSymfonyRouterBundle\Services\Controllers\ErrorJsonController;
use Prokl\BitrixSymfonyRouterBundle\Services\Router\InitRouter;
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\Contracts\RouterInitializerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

/**
* Class Router
* @package Prokl\BitrixSymfonyRouterBundle\Services\Agnostic
*
* @since 24.07.2021
*/
class Router
{
/**
* @var InitRouter $router Инициализированный экземпляр роутера.
*/
private $router;

/**
* AgnosticRouter constructor.
*
* @param RouterInterface $router Инициализированный экземпляр роутера.
* @param RouterInitializerInterface $routerInitializer Инициализатор роутера.
*/
public function __construct(
RouterInterface $router,
RouterInitializerInterface $routerInitializer
) {
$this->router = new InitRouter(
$router,
new ErrorJsonController(
new Serializer(
[new ObjectNormalizer],
[new JsonEncoder]

)
),
new EventDispatcher(),
new ControllerResolver(),
new ArgumentResolver(
new ArgumentMetadataFactory(),
[
new RequestAttributeValueResolver(),
new RequestValueResolver(),
new SessionValueResolver(),
new DefaultValueResolver(),
new VariadicValueResolver(),
]
),
new RequestStack()
);

$routerInitializer->init($this->router);
}
}
8 changes: 8 additions & 0 deletions Services/Agnostic/RoutesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ public function purgeCache() : void
$filesystem->remove($this->cacheDir);
}

/**
* @return RouterInterface
*/
public function getRouter(): RouterInterface
{
return $this->router;
}

/**
* Создать (если надо), кэш.
*
Expand Down
27 changes: 26 additions & 1 deletion readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,29 @@ public_page:
```

Если установлен Битрикс с версией модуля младше `21.400.0`, то соответствующие сервисы будут
удалены из бандла на стадии компиляции.
удалены из бандла на стадии компиляции.

## Использование роутера без контейнера и вне фреймворка

`init.php`:

```php
use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesLoader;
use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\Router;
use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\BitrixInitializerRouter;

$agnosticRouter = new RoutesLoader(
$_SERVER['DOCUMENT_ROOT'] . '/local/configs/standalone_routes.yaml',
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/cache/routes_agnostic', // Кэш; если null - без кэширования.
$_ENV['DEBUG'] // Режим отладки или нет
);

$agnosticRouterInstance = new Router(
$agnosticRouter->getRouter(),
new BitrixInitializerRouter()
);
```

Все. Подтянутся роуты из `/local/configs/standalone_routes.yaml`. Автоматически подцепятся события.

Допускается наличие нескольких таких "агностических" роутеров в один момент.

0 comments on commit 96412a1

Please sign in to comment.