Skip to content

Commit

Permalink
Auto detected controllers are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Jun 22, 2020
1 parent ba29eb4 commit 18260ee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
42 changes: 30 additions & 12 deletions src/Adapter/Module/Tab/ModuleTabRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,7 @@ protected function addUndeclaredTabs($moduleName, array $tabs)
}
}, $tabs);

$legacyControllersFilenames = $this->getModuleAdminControllersFilename($moduleName);
$legacyControllers = array_map(function ($legacyControllersFilename) {
return str_replace('Controller.php', '', $legacyControllersFilename);
}, $legacyControllersFilenames);
$legacyControllers = $legacyControllers ?? [];

$routingControllers = $this->getModuleControllersFromRouting($moduleName);
$routingControllers = $routingControllers ?? [];

$detectedControllers = array_merge($legacyControllers, $routingControllers);
$detectedControllers = $this->getDetectedModuleControllers($moduleName);
foreach ($detectedControllers as $adminControllerName) {
if (in_array($adminControllerName, $tabsNames)) {
continue;
Expand All @@ -190,6 +181,30 @@ protected function addUndeclaredTabs($moduleName, array $tabs)
return $tabs;
}

/**
* Returns a list of all detected controllers, either from admin/controllers folder
* or from the routing file.
*
* @param string $moduleName
*
* @return array
*
* @throws Exception
*/
protected function getDetectedModuleControllers(string $moduleName): array
{
$legacyControllersFilenames = $this->getModuleAdminControllersFilename($moduleName);
$legacyControllers = array_map(function ($legacyControllersFilename) {
return str_replace('Controller.php', '', $legacyControllersFilename);
}, $legacyControllersFilenames);
$legacyControllers = $legacyControllers ?? [];

$routingControllers = $this->getModuleControllersFromRouting($moduleName);
$routingControllers = $routingControllers ?? [];

return array_merge($legacyControllers, $routingControllers);
}

/**
* Check mandatory data for tab registration, such as class name and class exists.
*
Expand All @@ -206,10 +221,13 @@ protected function checkIsValid($moduleName, ParameterBag $data)
if (null === $className) {
throw new Exception('Missing class name of tab');
}

// Check controller exists
if (empty($data->get('route_name')) && !in_array($className . 'Controller.php', $this->getModuleAdminControllersFilename($moduleName))) {
throw new Exception(sprintf('Class "%sController" not found in controllers/admin', $className));
$detectedControllers = $this->getDetectedModuleControllers($moduleName);
if (empty($data->get('route_name')) && !in_array($className, $detectedControllers)) {
throw new Exception(sprintf('Class "%sController" not found in controllers/admin nor routing file', $className));
}

// Deprecation check
if ($data->has('ParentClassName') && !$data->has('parent_class_name')) {
$this->logger->warning('Tab attribute "ParentClassName" is deprecated. You must use "parent_class_name" instead.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

use LegacyTests\TestCase\UnitTestCase;
use PrestaShop\PrestaShop\Adapter\Module\Tab\ModuleTabRegister;
use PrestaShopBundle\Routing\YamlModuleLoader;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\ParameterBag;
Expand All @@ -54,7 +53,7 @@ class ModuleTabRegisterTest extends UnitTestCase
// Non-existing class file, must throw an exception
array(
'class_name' => 'AdminMissing',
'exception' => 'Class "AdminMissingController" not found in controllers/admin',
'exception' => 'Class "AdminMissingController" not found in controllers/admin nor routing file',
),
),
'symfony' => array(
Expand All @@ -65,7 +64,11 @@ class ModuleTabRegisterTest extends UnitTestCase
),
),
// No tabs by default, the undeclared one comes from the routing parsing
'undeclared_symfony' => array(),
'undeclared_symfony' => array(
array(
'class_name' => 'UndeclaredLegacyController',
),
),
);

protected $moduleAdminControllers = array(
Expand Down

0 comments on commit 18260ee

Please sign in to comment.