Add explicit controller definition requirement for APIs

Updated API creation to require an explicit controller definition. This change has been reflected in the ChillMainExtension and ChillPersonExtension files. Also, it has introduced a new exception, the InvalidCrudConfiguration, which will be thrown when a new API or CRUD is created without this explicit controller definition.
This commit is contained in:
2024-04-08 15:38:05 +02:00
parent 78a3dfd65e
commit 76fdd6d889
16 changed files with 253 additions and 29 deletions

View File

@@ -11,6 +11,9 @@ declare(strict_types=1);
namespace Chill\MainBundle\CRUD\CompilerPass;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\CRUD\Controller\CRUDController;
use Chill\MainBundle\CRUD\Exception\InvalidCrudConfiguration;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -38,6 +41,9 @@ class CRUDControllerCompilerPass implements CompilerPassInterface
private function configureCrudController(ContainerBuilder $container, array $crudEntry, string $apiOrCrud): void
{
$controllerClass = $crudEntry['controller'];
if (ApiController::class === $controllerClass || CRUDController::class === $controllerClass) {
throw InvalidCrudConfiguration::configurationRequiresControllerDefinition($crudEntry['class'], $apiOrCrud);
}
$controllerServiceName = 'cs'.$apiOrCrud.'_'.$crudEntry['name'].'_controller';
// create config parameter in container

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\CRUD\Exception;
class InvalidCrudConfiguration extends \RuntimeException
{
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
public static function configurationRequiresControllerDefinition(string $class, string $apiOrCrud): self
{
return new self(sprintf('The definition requires that a controller definition exists in container configuration. Add a `controller` key to your %s definition, for class %s', $apiOrCrud, $class), 48);
}
}

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController;
class CountryApiController extends ApiController {}

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController;
class GeographicalUnitApiController extends ApiController {}

View File

@@ -15,7 +15,9 @@ use Chill\MainBundle\Controller\AddressApiController;
use Chill\MainBundle\Controller\CenterController;
use Chill\MainBundle\Controller\CivilityApiController;
use Chill\MainBundle\Controller\CivilityController;
use Chill\MainBundle\Controller\CountryApiController;
use Chill\MainBundle\Controller\CountryController;
use Chill\MainBundle\Controller\GeographicalUnitApiController;
use Chill\MainBundle\Controller\LanguageController;
use Chill\MainBundle\Controller\LocationController;
use Chill\MainBundle\Controller\LocationTypeController;
@@ -667,6 +669,7 @@ class ChillMainExtension extends Extension implements
],
[
'class' => Country::class,
'controller' => CountryApiController::class,
'name' => 'country',
'base_path' => '/api/1.0/main/country',
'base_role' => 'ROLE_USER',
@@ -787,6 +790,7 @@ class ChillMainExtension extends Extension implements
],
[
'class' => GeographicalUnitLayer::class,
'controller' => GeographicalUnitApiController::class,
'name' => 'geographical-unit-layer',
'base_path' => '/api/1.0/main/geographical-unit-layer',
'base_role' => 'ROLE_USER',