Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI routing refactoring #3

Merged
merged 3 commits into from
Sep 21, 2021
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
8 changes: 5 additions & 3 deletions config/common/cli/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
'uri_path_start_index' => 0,
'services' => [
// all requests starting with /example (php console.php /worker)
'worker' => [
'class' => \App\ExampleCli\ExampleService::class
'cli' => [
'class' => \App\Example\ExampleService::class,
// path to service dependency config related to service directory
'dependency' => 'config/cli/dependency.php'
],
],
'default_service' => 'worker'
'default_service' => 'cli'
];
4 changes: 3 additions & 1 deletion config/common/web/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
'services' => [
// all requests starting with /example (http:://my.host/example/api)
'example' => [
'class' => \App\Example\ExampleService::class
'class' => \App\Example\ExampleService::class,
// path to service dependency config related to service directory
'dependency' => 'config/web/dependency.php'
],
],
'default_service' => 'example'
Expand Down
27 changes: 19 additions & 8 deletions docs/add_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ Creat directory `services/example`

Service files structure:

config/ // settings
dependency.php // DependencyConteiner configuration
routes.php // routing settings
config/

cli/ // CLI settings
dependency.php // DependencyConteiner configuration
routes.php // routing settings

web/ // WEB settings
dependency.php // DependencyConteiner configuration
routes.php // routing settings

src/ // code
Action // Actions directory (if needed)
ExampleService.php // Service class
Action/ // Actions directory (if needed)
Cli/ // Comand line actions
Web/ // http api actions

ExampleService.php // Service class

docs/ // documentation
readme.md // documentation index file

Expand All @@ -31,14 +42,14 @@ For example: adding RuntimeCache for dictionaries and other static application d
### DI Container
Every service using own DependencyContainer. [ExampleService](../services/example/src/ExampleService.php)

Specialized service configurations should be registered as service dependencies. [Example dependencies](../services/example/config/dependency.php)
Specialized service configurations should be registered as service dependencies. [Example dependencies](../services/example/config/web/dependency.php)


### Service routing
Use ```KSamuel\RrService\Service\ActionRouter``` to specify service routes

[ExampleService](../services/example/src/ExampleService.php)

[Example routes](../services/example/config/routes.php)
[Example routes](../services/example/config/web/routes.php)

Action Router should be added into [Example dependencies](../services/example/config/dependency.php)
Action Router should be added into [Example dependencies](../services/example/config/web/dependency.php)
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Documentation
* [Installation](install.md)
* [Application manifest](manifest.md)
* [Project files structure](fs.md)
* [Routing](routing.md)
* [How to add new service](add_service.md)
* [Caching](cache.md)
* [Testing](tests.md)
* [Testing](tests.md)
6 changes: 4 additions & 2 deletions docs/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ return [
'services' => [
// All requests starting with /example/
'example' => [
'class' => \App\Example\ExampleService::class
'class' => \App\Example\ExampleService::class,
// path to service dependency config related to service directory
'dependency' => 'config/web/dependency.php'
],
],
'default_service' => 'example'
Expand All @@ -49,7 +51,7 @@ specific action route: /example/api

In the Action routing settings, specify only `api`

Settings example: services/example/config/routes.php
Settings example: services/example/config/web/routes.php
```php
<?php
return [
Expand Down
15 changes: 0 additions & 15 deletions services/example-cli/config/routes.php

This file was deleted.

Empty file removed services/example-cli/docs/.gitkeep
Empty file.
1 change: 0 additions & 1 deletion services/example-cli/readme.md

This file was deleted.

49 changes: 0 additions & 49 deletions services/example-cli/src/ExampleService.php

This file was deleted.

18 changes: 18 additions & 0 deletions services/example/config/cli/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

return [
// uri path part with service code (other parts routes in service)
'uri_path_start_index' => 0,
'services' => [
'worker1' => [
'class' => \App\Example\Action\Cli\LongWorker::class
],
'worker2' => [
'class' => \App\Example\Action\Cli\FastWorker::class
],
'index' => [
'class' => \App\Example\Action\Cli\Index::class
]
],
'default_route' => 'index'
];
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
'uri_path_start_index' => 1,
'services' => [
'index' => [
'class' => \App\Example\Action\Index::class
'class' => \App\Example\Action\Web\Index::class
],
'api' => [
'class' => \App\Example\Action\Api::class
'class' => \App\Example\Action\Web\Api::class
],
],
'default_route' => 'index'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\ExampleCli\Action;
namespace App\Example\Action\Cli;

use KSamuel\RrService\Service\ActionInterface;
use KSamuel\RrService\Service\ResultInterface;
Expand All @@ -12,7 +12,7 @@
/**
* Example console worker
*/
class Index implements ActionInterface
class FastWorker implements ActionInterface
{
/**
* @param ServerRequestInterface $req
Expand All @@ -22,6 +22,7 @@ class Index implements ActionInterface
*/
public function run(ServerRequestInterface $req, ResultInterface $res, ContainerInterface $container): void
{
$res->setData(['message' => 'Please select console action']);
$result = ['success' => true];
$res->setData($result);
}
}
32 changes: 32 additions & 0 deletions services/example/src/Action/Cli/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Example\Action\Cli;

use KSamuel\RrService\Service\ActionInterface;
use KSamuel\RrService\Service\ResultInterface;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* Example action for web API
*/
class Index implements ActionInterface
{
/**
* @param ServerRequestInterface $req
* @param ResultInterface $res
* @param ContainerInterface $container
* @throws \Exception
*/
public function run(ServerRequestInterface $req, ResultInterface $res, ContainerInterface $container): void
{
$res->setData(
[
'success' => true,
'message' => 'Please select worker action',
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\ExampleCli\Action;
namespace App\Example\Action\Cli;

use KSamuel\RrService\Service\ActionInterface;
use KSamuel\RrService\Service\ResultInterface;
Expand All @@ -12,7 +12,7 @@
/**
* Example console worker
*/
class Worker implements ActionInterface
class LongWorker implements ActionInterface
{
/**
* @param ServerRequestInterface $req
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Example\Action;
namespace App\Example\Action\Web;

use KSamuel\RrService\Service\ActionInterface;
use KSamuel\RrService\Service\ResultInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Example\Action;
namespace App\Example\Action\Web;

use KSamuel\RrService\Service\ActionInterface;
use KSamuel\RrService\Service\ResultInterface;
Expand Down
10 changes: 8 additions & 2 deletions services/example/src/ExampleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ class ExampleService implements ServiceInterface
public function __construct(
Connection\Manager $connectionManager,
LoggerInterface $logger,
Config\Storage $configStorage
Config\Storage $configStorage,
array $serviceConfig
) {
$container = new DependencyContainer();
$container->bind(LoggerInterface::class, $logger);
$container->bind(Connection\Manager::class, $connectionManager);
$container->bind(Config\Storage::class, $configStorage);
$container->bindPhpConfig(dirname(__DIR__) . '/config/dependency.php');

if (!isset($serviceConfig['dependency'])) {
throw new \InvalidArgumentException('Undefined service dependency config');
}

$container->bindPhpConfig(dirname(__DIR__) . '/' . $serviceConfig['dependency']);

$this->di = $container;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Loader/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public function __construct(Manager $connections, Storage $configStore, LoggerIn
}

/**
* @param array<string,mixed> $serviceConfig
* @param array{class:string,dependency:string} $serviceConfig
* @return ServiceInterface
*/
public function loadService(array $serviceConfig): ServiceInterface
{
return new $serviceConfig['class']($this->connections, $this->log, $this->configStore);
return new $serviceConfig['class']($this->connections, $this->log, $this->configStore, $serviceConfig);
}
}
12 changes: 7 additions & 5 deletions src/Service/Loader/LoaderContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ public function __construct(Manager $connections, Storage $configStore, LoggerIn
}

/**
* @param array<string,mixed> $serviceConfig
* @param array{class:string,dependency:string} $serviceConfig
* @return ServiceInterface
*/
public function loadService(array $serviceConfig): ServiceInterface
{
if (!isset($this->services[$serviceConfig['class']])) {
$this->services[$serviceConfig['class']] = new $serviceConfig['class'](
$key = $serviceConfig['class'] . '::' . $serviceConfig['dependency'];
if (!isset($this->services[$key])) {
$this->services[$key] = new $serviceConfig['class'](
$this->connections,
$this->log,
$this->configStore
$this->configStore,
$serviceConfig
);
}
return $this->services[$serviceConfig['class']];
return $this->services[$key];
}
}
2 changes: 1 addition & 1 deletion src/Service/Loader/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
interface LoaderInterface
{
/**
* @param array<string,mixed> $serviceConfig
* @param array{class:string,dependency:string} $serviceConfig
* @return ServiceInterface
*/
public function loadService(array $serviceConfig): ServiceInterface;
Expand Down
8 changes: 7 additions & 1 deletion src/Service/ServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ interface ServiceInterface
* @param Connection\Manager $connectionManager
* @param LoggerInterface $logger
* @param Storage $configStorage
* @param array{class:string,dependency:string} $serviceConfig
*/
public function __construct(Connection\Manager $connectionManager, LoggerInterface $logger, Storage $configStorage);
public function __construct(
Connection\Manager $connectionManager,
LoggerInterface $logger,
Storage $configStorage,
array $serviceConfig
);

/**
* Warm up service cache, load static dictionaries
Expand Down
Loading