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

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Jul 1, 2017
1 parent c96bc74 commit 8f1e4dd
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 2 deletions.
19 changes: 19 additions & 0 deletions Classes/Exceptions/ClassNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ClassNotFoundException extends FormzException

const CONDITION_CLASS_NAME_NOT_FOUND = 'The class name for the condition "%s" was not found (given value: "%s").';

const MIDDLEWARE_CLASS_NAME_NOT_FOUND = 'The class name "%s" was not found.';

/**
* @code 1489602455
*
Expand Down Expand Up @@ -130,4 +132,21 @@ final public static function ajaxControllerFormClassNameNotFound($className)

return $exception;
}

/**
* @code 1490180343
*
* @param string $className
* @return self
*/
final public static function middlewareClassNameNotFound($className)
{
/** @var self $exception */
$exception = self::getNewExceptionInstance(
self::MIDDLEWARE_CLASS_NAME_NOT_FOUND,
[$className]
);

return $exception;
}
}
20 changes: 20 additions & 0 deletions Classes/Exceptions/InvalidArgumentTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Romm\Formz\Condition\Items\ConditionItemInterface;
use Romm\Formz\Form\FormInterface;
use Romm\Formz\Form\FormObject\FormObject;
use Romm\Formz\Middleware\MiddlewareInterface;
use Romm\Formz\ViewHelpers\FieldViewHelper;
use Romm\Formz\ViewHelpers\FormatMessageViewHelper;
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
Expand Down Expand Up @@ -44,6 +45,8 @@ class InvalidArgumentTypeException extends FormzException

const FORM_ARGUMENT_NOT_ARRAY = 'The request argument for the form "%s" (class: "%s") was not an array (found type is "%s"); there must have been a manual overriding of the argument.';

const MIDDLEWARE_WRONG_CLASS_NAME = 'The middleware class must be an instance of "%s", given class is of type "%s".';

/**
* @code 1477468571
*
Expand Down Expand Up @@ -233,4 +236,21 @@ final public static function formArgumentNotArray(FormObject $formObject, $formD

return $exception;
}

/**
* @code 1492613743
*
* @param string $className
* @return self
*/
final public static function middlewareWrongClassName($className)
{
/** @var self $exception */
$exception = self::getNewExceptionInstance(
self::MIDDLEWARE_WRONG_CLASS_NAME,
[MiddlewareInterface::class, $className]
);

return $exception;
}
}
17 changes: 16 additions & 1 deletion Classes/Form/Definition/FormDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Romm\Formz\Form\Definition\Field\Field;
use Romm\Formz\Form\Definition\Middleware\PresetMiddlewares;
use Romm\Formz\Form\Definition\Settings\FormSettings;
use Romm\Formz\Middleware\MiddlewareFactory;
use Romm\Formz\Middleware\MiddlewareInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand Down Expand Up @@ -272,9 +273,23 @@ public function getMiddleware($name)
return $this->middlewares[$name];
}

public function addMiddleware($name, $className)
/**
* @param string $name
* @param string $className
* @param callable $optionsCallback
* @return MiddlewareInterface
*/
public function addMiddleware($name, $className, callable $optionsCallback = null)
{
$this->checkDefinitionFreezeState();

if ($this->hasMiddleware($name)) {
// throw DuplicateEntryException::; @todo
}

$this->middlewares[$name] = MiddlewareFactory::get()->create($className, $optionsCallback);

return $this->middlewares[$name];
}

/**
Expand Down
10 changes: 10 additions & 0 deletions Classes/Form/FormObject/Builder/AbstractFormObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public function getStaticInstance($className)

$this->process();

$middleware = $this->static->getDefinition()->addMiddleware(
'test',
\Romm\FormzExample\Middlewares\TestMiddleware::class,
function (\Romm\FormzExample\Middlewares\Options\TestOptions $options) {
$options->setFoo('pouet');
}
);
// debug($middleware);
// die('!!');

$formDefinitionObject->refreshValidationResult();

return $this->static;
Expand Down
2 changes: 1 addition & 1 deletion Classes/Middleware/Item/AbstractMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class AbstractMiddleware implements MiddlewareInterface, DataPreProcess

/**
* This is the default option class, this property can be overridden in
* child classes to be mapped to another option definition.
* children classes to be mapped to another option definition.
*
* @var \Romm\Formz\Middleware\Option\DefaultOptionDefinition
*/
Expand Down
90 changes: 90 additions & 0 deletions Classes/Middleware/MiddlewareFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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\Middleware;

use Romm\Formz\Core\Core;
use Romm\Formz\Exceptions\ClassNotFoundException;
use Romm\Formz\Exceptions\InvalidArgumentTypeException;
use Romm\Formz\Middleware\Option\AbstractOptionDefinition;
use Romm\Formz\Service\Traits\ExtendedSelfInstantiateTrait;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Extbase\Reflection\ReflectionService;

class MiddlewareFactory implements SingletonInterface
{
use ExtendedSelfInstantiateTrait;

/**
* @var ReflectionService
*/
protected $reflectionService;

/**
* @param ReflectionService $reflectionService
*/
public function __construct(ReflectionService $reflectionService)
{
$this->reflectionService = $reflectionService;
}

/**
* @param string $className
* @param callable $optionsCallback
* @return MiddlewareInterface
* @throws ClassNotFoundException
* @throws InvalidArgumentTypeException
*/
public function create($className, callable $optionsCallback = null)
{
if (false === class_exists($className)) {
throw ClassNotFoundException::middlewareClassNameNotFound($className);
}

if (false === in_array(MiddlewareInterface::class, class_implements($className))) {
throw InvalidArgumentTypeException::middlewareWrongClassName($className);
}

$optionsType = $this->getOptionsType($className);
$options = Core::instantiate($optionsType);

if (is_callable($optionsCallback)) {
call_user_func($optionsCallback, $options);
}

/** @var MiddlewareInterface $middleware */
$middleware = Core::instantiate($className, $options);

return $middleware;
}

/**
* @param string $className
* @return string
*/
protected function getOptionsType($className)
{
$property = $this->reflectionService->getClassSchema($className)->getProperty('options');
$optionsType = $property['type'];

if (false === class_exists($optionsType)) {
throw new \Exception('todo'); // todo exception
}

if (false === in_array(AbstractOptionDefinition::class, class_parents($optionsType))) {
throw new \Exception('todo'); // todo exception
}

return $optionsType;
}
}

0 comments on commit 8f1e4dd

Please sign in to comment.