-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add autowiring for actions and views
- Loading branch information
Showing
12 changed files
with
188 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Sys25\RnBase\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Sys25\RnBase\Frontend\Controller\AbstractAction; | ||
use Sys25\RnBase\Frontend\Service\FrontendServiceProvider; | ||
use Sys25\RnBase\Frontend\View\AbstractView; | ||
|
||
class FrontendServicePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
// always first check if the primary service is defined | ||
if (!$container->has(FrontendServiceProvider::class)) { | ||
return; | ||
} | ||
|
||
$definition = $container->findDefinition(FrontendServiceProvider::class); | ||
|
||
// find all tagged service IDs | ||
$taggedServices = $container->findTaggedServiceIds(AbstractAction::SERVICE_TAG); | ||
|
||
foreach ($taggedServices as $id => $tags) { | ||
// add the service to the ServiceProvider service | ||
$definition->addMethodCall('addFrontendAction', [new Reference($id)]); | ||
} | ||
|
||
// find all tagged service IDs | ||
$taggedServices = $container->findTaggedServiceIds(AbstractView::SERVICE_TAG); | ||
|
||
foreach ($taggedServices as $id => $tags) { | ||
// add the service to the ServiceProvider service | ||
$definition->addMethodCall('addFrontendView', [new Reference($id)]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
<?php | ||
|
||
namespace Sys25\RnBase\Frontend\Controller; | ||
|
||
use Sys25\RnBase\Configuration\ConfigurationInterface; | ||
use Sys25\RnBase\Configuration\Processor; | ||
use Sys25\RnBase\Exception\ExceptionHandler; | ||
use Sys25\RnBase\Exception\ExceptionHandlerInterface; | ||
use Sys25\RnBase\Exception\PageNotFound404; | ||
use Sys25\RnBase\Exception\SkipActionException; | ||
use Sys25\RnBase\Frontend\Request\Parameters; | ||
use Sys25\RnBase\Frontend\Request\ParametersInterface; | ||
use Sys25\RnBase\Frontend\Service\FrontendServiceProvider; | ||
use Sys25\RnBase\Utility\Arrays; | ||
use Sys25\RnBase\Utility\Logger; | ||
use Sys25\RnBase\Utility\Misc; | ||
use Sys25\RnBase\Utility\Strings; | ||
use Sys25\RnBase\Utility\TYPO3; | ||
use Throwable; | ||
use tx_rnbase; | ||
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; | ||
|
||
/*************************************************************** | ||
* Copyright notice | ||
* | ||
* (c) 2007-2013 René Nitzsche | ||
* (c) 2007-2024 René Nitzsche | ||
* All rights reserved | ||
* | ||
* Based on code by Elmar Hinz Contact: [email protected] | ||
|
@@ -112,7 +118,7 @@ | |
* FIXME: move to PSR-4 | ||
*/ | ||
|
||
class tx_rnbase_controller | ||
class MainController | ||
{ | ||
public $configurationsClassName = Processor::class; // You may overwrite this in your subclass with an own configurations class. | ||
|
||
|
@@ -130,6 +136,13 @@ class tx_rnbase_controller | |
|
||
private $errors = []; | ||
|
||
private $frontendServiceProvider; | ||
|
||
public function __construct(?FrontendServiceProvider $frontendServiceProvider = null) | ||
{ | ||
$this->frontendServiceProvider = $frontendServiceProvider ?: null; | ||
} | ||
|
||
public function setContentObjectRenderer(ContentObjectRenderer $cObj): void | ||
{ | ||
$this->cObj = $cObj; | ||
|
@@ -212,7 +225,7 @@ public function main($out, $configurationArray) | |
|
||
// Making the configurations object | ||
Misc::pushTT('init configuration', ''); | ||
/** @var $configurations Processor */ | ||
/** @var Processor $configurations */ | ||
$configurations = $this->_makeConfigurationsObject($configurationArray); | ||
Misc::pullTT(); | ||
|
||
|
@@ -260,22 +273,42 @@ public function main($out, $configurationArray) | |
return $out; | ||
} | ||
|
||
/** | ||
* @param mixed $actionName | ||
* @return AbstractAction | ||
*/ | ||
private function lookupActionService($actionName) | ||
{ | ||
// Zuerst im DI-Container suchen | ||
$action = null; | ||
if ($this->frontendServiceProvider) { | ||
$action = $this->frontendServiceProvider->getActionForClass($actionName); | ||
} | ||
|
||
if (!$action) { | ||
// Fallback auf alte Services. In T3 8 und 9 müssen sich die Extension ggf. selber kümmern. | ||
$action = tx_rnbase::makeInstance($actionName); | ||
} | ||
|
||
return $action; | ||
} | ||
|
||
/** | ||
* Call a single action. | ||
* | ||
* @param string $actionName class name | ||
* @param tx_rnbase_IParams $parameters | ||
* @param Tx_Rnbase_Configuration_ProcessorInterface $configurations | ||
* @param string $actionName class name | ||
* @param ParametersInterface $parameters | ||
* @param ConfigurationInterface $configurations | ||
* | ||
* @return string | ||
*/ | ||
public function doAction($actionName, &$parameters, &$configurations) | ||
public function doAction(string $actionName, ParametersInterface $parameters, ConfigurationInterface $configurations) | ||
{ | ||
$ret = ''; | ||
|
||
try { | ||
// Creating the responsible Action | ||
$action = tx_rnbase::makeInstance($actionName); | ||
$action = $this->lookupActionService($actionName); | ||
if (is_object($action)) { | ||
$ret = $action->execute($parameters, $configurations); | ||
} | ||
|
@@ -433,7 +466,7 @@ protected function _findAction($parameters, $configurations) | |
* | ||
* @return string the action value | ||
*/ | ||
public function _getParameterAction($parameters) | ||
private function _getParameterAction($parameters) | ||
{ | ||
$action = $parameters->offsetExists('action') ? $parameters->offsetGet('action') : ''; | ||
if (!is_array($action)) { | ||
|
@@ -450,9 +483,9 @@ public function _getParameterAction($parameters) | |
* | ||
* @param array $configurationArray the local configuration array | ||
* | ||
* @return Tx_Rnbase_Configuration_ProcessorInterface the configurations | ||
* @return ConfigurationInterface the configurations | ||
*/ | ||
public function _makeConfigurationsObject($configurationArray) | ||
private function _makeConfigurationsObject($configurationArray) | ||
{ | ||
// TODO, die Configklasse sollte über TS variabel gehalten werden | ||
// Make configurations object | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
/*************************************************************** | ||
* Copyright notice | ||
* | ||
* (c) 2009-2023 Rene Nitzsche | ||
* (c) 2009-2024 Rene Nitzsche | ||
* Contact: [email protected] | ||
* All rights reserved | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
/*************************************************************** | ||
* Copyright notice | ||
* | ||
* (c) 2009 Rene Nitzsche | ||
* (c) 2009-2024 Rene Nitzsche | ||
* Contact: [email protected] | ||
* All rights reserved | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Sys25\RnBase\Frontend\Service; | ||
|
||
use ReflectionClass; | ||
use Sys25\RnBase\Frontend\Controller\AbstractAction; | ||
use Sys25\RnBase\Frontend\View\AbstractView; | ||
|
||
/*************************************************************** | ||
* Copyright notice | ||
* | ||
* (c) 2007-2024 Rene Nitzsche <[email protected]> | ||
* All rights reserved | ||
* | ||
* This script is part of the TYPO3 project. The TYPO3 project is | ||
* free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The GNU General Public License can be found at | ||
* http://www.gnu.org/copyleft/gpl.html. | ||
* | ||
* This script is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* This copyright notice MUST APPEAR in all copies of the script! | ||
***************************************************************/ | ||
|
||
class FrontendServiceProvider | ||
{ | ||
private $actions = []; | ||
private $views = []; | ||
private $filters = []; | ||
|
||
public function addFrontendAction(AbstractAction $action): void | ||
{ | ||
$this->actions[get_class($action)] = $action; | ||
} | ||
|
||
public function getActionForClass(string $actionClass): ?AbstractAction | ||
{ | ||
if (!array_key_exists($actionClass, $this->actions) && class_exists($actionClass)) { | ||
// Hier werden häufig aliases verwendet. | ||
$reflection = new ReflectionClass($actionClass); | ||
$action = $reflection->getName(); | ||
$this->actions[$actionClass] = $this->actions[$action] ?? null; | ||
} | ||
|
||
return $this->actions[$actionClass] ?? null; | ||
} | ||
|
||
public function addFrontendView(AbstractView $view): void | ||
{ | ||
$this->views[get_class($view)] = $view; | ||
} | ||
|
||
public function getViewForClass(string $viewClass): ?AbstractView | ||
{ | ||
return $this->views[$viewClass] ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
/*************************************************************** | ||
* Copyright notice | ||
* | ||
* (c) 2007-2019 René Nitzsche <[email protected]> | ||
* (c) 2007-2024 René Nitzsche <[email protected]> | ||
* All rights reserved | ||
* | ||
* This script is part of the TYPO3 project. The TYPO3 project is | ||
|
@@ -29,6 +29,8 @@ | |
|
||
abstract class AbstractView | ||
{ | ||
public const SERVICE_TAG = 'sys25.frontend.view'; | ||
|
||
protected $pathToTemplates; | ||
|
||
protected $templateFile; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters