Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
[TASK] Inject dependencies in middlewares fetched from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Apr 14, 2017
1 parent 6948fe9 commit 51115eb
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
92 changes: 92 additions & 0 deletions Classes/Core/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/*
* 2017 Romain CANON <romain.hydrocanon@gmail.com>
*
* This file is part of the TYPO3 FormZ project.
* It is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, either
* version 3 of the License, or any later version.
*
* For the full copyright and license information, see:
* http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Romm\Formz\Core;

use Romm\Formz\Service\Traits\SelfInstantiateTrait;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\Container\ClassInfo;
use TYPO3\CMS\Extbase\Object\Container\Container as ExtbaseContainer;
use TYPO3\CMS\Extbase\Reflection\PropertyReflection;

/**
* This container allows injecting dependencies in objects. It is used on
* objects that were fetched from cache.
*
* Unfortunately, Extbase container does not have a public method for this, so
* this is a pure copy of the original container functions.
*/
class Container extends ExtbaseContainer
{
use SelfInstantiateTrait;

/**
* @var ExtbaseContainer
*/
protected $extbaseContainer;

/**
* Injects extbase real container.
*/
public function __construct()
{
$this->extbaseContainer = GeneralUtility::makeInstance(ExtbaseContainer::class);
}

/**
* @see \Romm\Formz\Core\Container
*
* @param object $instance
*/
public function injectDependenciesInInstance($instance)
{
$classInfo = $this->getClassInfo(get_class($instance));

if (!$classInfo->hasInjectMethods() && !$classInfo->hasInjectProperties()) {
return;
}

foreach ($classInfo->getInjectMethods() as $injectMethodName => $classNameToInject) {
$instanceToInject = $this->extbaseContainer->getInstance($classNameToInject);
if (is_callable([$instance, $injectMethodName])) {
$instance->{$injectMethodName}($instanceToInject);
}
}
foreach ($classInfo->getInjectProperties() as $injectPropertyName => $classNameToInject) {
$instanceToInject = $this->extbaseContainer->getInstance($classNameToInject);
$propertyReflection = GeneralUtility::makeInstance(PropertyReflection::class, $instance, $injectPropertyName);

$propertyReflection->setAccessible(true);
$propertyReflection->setValue($instance, $instanceToInject);
}
}

/**
* @see \Romm\Formz\Core\Container
*
* @param string $className
* @return ClassInfo
*/
private function getClassInfo($className)
{
$classNameHash = md5($className);
$classInfo = $this->getClassInfoCache()->get($classNameHash);

if (!$classInfo instanceof ClassInfo) {
$classInfo = $this->getClassInfoFactory()->buildClassInfoFromClassName($className);
$this->getClassInfoCache()->set($classNameHash, $classInfo);
}

return $classInfo;
}
}
19 changes: 19 additions & 0 deletions Classes/Form/FormObject/Service/FormObjectConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Romm\ConfigurationObject\ConfigurationObjectInstance;
use Romm\Formz\Configuration\ConfigurationFactory;
use Romm\Formz\Configuration\Form\Form;
use Romm\Formz\Core\Container;
use Romm\Formz\Form\FormObject\FormObjectStatic;
use Romm\Formz\Service\CacheService;
use Romm\Formz\Validation\Validator\Internal\FormConfigurationValidator;
Expand Down Expand Up @@ -150,6 +151,8 @@ protected function getConfigurationObjectFromCache()

if ($cacheInstance->has($cacheIdentifier)) {
$configurationObject = $cacheInstance->get($cacheIdentifier);
$this->injectDependenciesInConfiguration($configurationObject);

$this->configurationIsValid = true;
} else {
$configurationArray = $this->sanitizeConfiguration($this->configurationArray);
Expand Down Expand Up @@ -189,6 +192,22 @@ protected function sanitizeConfiguration(array $configuration)
return $configuration;
}

/**
* Middlewares of the form are stored in cache, so their dependencies must
* be injected again when they are fetched from cache.
*
* @param ConfigurationObjectInstance $configurationObject
*/
protected function injectDependenciesInConfiguration(ConfigurationObjectInstance $configurationObject)
{
/** @var Form $formConfiguration */
$formConfiguration = $configurationObject->getObject(true);

foreach ($formConfiguration->getAllMiddlewares() as $middleware) {
Container::get()->injectDependenciesInInstance($middleware);
}
}

/**
* @param array $configuration
* @return ConfigurationObjectInstance
Expand Down

0 comments on commit 51115eb

Please sign in to comment.