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 May 15, 2017
1 parent 43968c2 commit 4c0830e
Show file tree
Hide file tree
Showing 31 changed files with 501 additions and 166 deletions.
74 changes: 63 additions & 11 deletions Classes/Configuration/AbstractConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@

namespace Romm\Formz\Configuration;

use Romm\ConfigurationObject\ConfigurationObjectFactory;
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
use Romm\ConfigurationObject\Traits\ConfigurationObject\MagicMethodsTrait;
use Romm\Formz\Exceptions\PropertyNotAccessibleException;
use TYPO3\CMS\Core\Utility\GeneralUtility;

abstract class AbstractConfiguration
{
use MagicMethodsTrait;
use ParentsTrait;
use MagicMethodsTrait {
handlePropertyMagicMethod as handlePropertyMagicMethodInternal;
}
use ParentsTrait {
attachParent as private attachParentInternal;
}

/**
* @param string $path
Expand All @@ -32,12 +38,20 @@ protected function getAbsolutePath($path)
}

/**
* @todo
* This method is used by setter methods, and other methods which goal is to
* modify a property value.
*
* It checks that the definition is not frozen, and if it is actually frozen
* an exception is thrown.
*
* @throws PropertyNotAccessibleException
*/
protected function checkDefinitionFreezeState()
protected function checkConfigurationFreezeState()
{
if ($this->isConfigurationFrozen()) {
throw new \Exception('todo');
$methodName = debug_backtrace()[1]['function'];

throw PropertyNotAccessibleException::rootConfigurationFrozenMethod(get_class($this), $methodName);
}
}

Expand All @@ -46,14 +60,52 @@ protected function checkDefinitionFreezeState()
*/
protected function isConfigurationFrozen()
{
$flag = false;
return $this->getState()
&& $this->getState()->isFrozen();
}

/**
* @return ConfigurationState
*/
protected function getState()
{
if ($this->hasParent(Configuration::class)) {
return $this->getFirstParent(Configuration::class)->getState();
}

if ($this instanceof Configuration) {
$flag = $this->isConfigurationFrozenRoot();
} elseif ($this->hasParent(Configuration::class)) {
$flag = $this->getFirstParent(Configuration::class)->isConfigurationFrozenRoot();
return null;
}

/**
* @param object $parent
* @param bool $direct
*/
public function attachParent($parent, $direct = true)
{
$this->checkConfigurationFreezeState();
$this->attachParentInternal($parent, $direct);
}

/**
* Overrides the magic methods handling from the Configuration Object API: a
* magic setter method must be accessible only for this API, otherwise an
* exception must be thrown.
*
* @param string $property
* @param string $type
* @param array $arguments
* @return mixed
* @throws PropertyNotAccessibleException
*/
protected function handlePropertyMagicMethod($property, $type, array $arguments)
{
if ($type === 'set'
&& $this->isPropertyAccessible($property)
&& false === ConfigurationObjectFactory::getInstance()->isRunning()
) {
throw PropertyNotAccessibleException::rootConfigurationFrozenProperty(get_class($this), $property);
}

return $flag;
return $this->handlePropertyMagicMethodInternal($property, $type, $arguments);
}
}
21 changes: 7 additions & 14 deletions Classes/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class Configuration extends AbstractConfiguration implements ConfigurationObject
protected $hash;

/**
* @var bool
* @var ConfigurationState
*/
private $configurationIsFrozen = false;
private $state;

/**
* Constructor.
Expand All @@ -60,6 +60,8 @@ public function __construct()

$this->view = GeneralUtility::makeInstance(View::class);
$this->view->attachParent($this);

$this->state = GeneralUtility::makeInstance(ConfigurationState::class);
}

/**
Expand Down Expand Up @@ -102,20 +104,11 @@ public function getHash()
}

/**
* Marks the configuration as frozen: its properties can not be modified
* after that.
*/
public function markConfigurationAsFrozen()
{
$this->configurationIsFrozen = true;
}

/**
* @return bool
* @return ConfigurationState
*/
public function isConfigurationFrozenRoot()
public function getState()
{
return $this->configurationIsFrozen;
return $this->state;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Classes/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected function buildRootConfiguration()
$this->callPostConfigurationProcessSignal($instanceObject);

$instanceObject->calculateHash();
$instanceObject->markConfigurationAsFrozen();
$instanceObject->getState()->markAsFrozen();

return $instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,31 @@
* http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Romm\Formz\Form\Definition;
namespace Romm\Formz\Configuration;

class FormDefinitionState
class ConfigurationState
{
/**
* @var bool
*/
protected $isDefinitionFrozen = false;
protected $isFrozen = false;

/**
* @return bool
*/
public function isDefinitionFrozen()
public function isFrozen()
{
return $this->isDefinitionFrozen;
return $this->isFrozen;
}

/**
* Marks the definition as frozen, meaning it cannot be edited anymore.
* Marks the configuration as frozen, meaning recursive properties cannot be
* edited anymore.
*
* @internal
*/
public function markDefinitionAsFrozen()
public function markAsFrozen()
{
$this->isDefinitionFrozen = true;
$this->isFrozen = true;
}
}
2 changes: 2 additions & 0 deletions Classes/Configuration/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function getDefaultBackendCache()
*/
public function setDefaultBackendCache($defaultBackendCache)
{
$this->checkConfigurationFreezeState();

$this->defaultBackendCache = $defaultBackendCache;
}
}
7 changes: 5 additions & 2 deletions Classes/Configuration/View/Classes/Classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,24 @@ class Classes extends AbstractConfiguration
public function __construct()
{
$this->errors = GeneralUtility::makeInstance(ViewClass::class);
$this->errors->attachParent($this);

$this->valid = GeneralUtility::makeInstance(ViewClass::class);
$this->valid->attachParent($this);
}

/**
* @return ViewClass
*/
public function getErrors()
public function getErrorsClasses()
{
return $this->errors;
}

/**
* @return ViewClass
*/
public function getValid()
public function getValidClasses()
{
return $this->valid;
}
Expand Down
4 changes: 2 additions & 2 deletions Classes/Configuration/View/Classes/ViewClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public function getItem($name)
* @param string $name
* @param string $value
*/
public function addItem($name, $value)
public function setItem($name, $value)
{
$this->checkDefinitionFreezeState();
$this->checkConfigurationFreezeState();

$this->items[$name] = $value;
}
Expand Down
17 changes: 7 additions & 10 deletions Classes/Configuration/View/Layouts/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,23 @@ class Layout extends AbstractConfiguration
*/
public function getTemplateFile()
{
if (null === $this->templateFile
$templateFile = $this->templateFile;

if (null === $templateFile
&& $this->hasParent(LayoutGroup::class)
) {
$this->templateFile = $this->withFirstParent(
LayoutGroup::class,
function (LayoutGroup $parent) {
return $parent->getTemplateFile();
}
);
$templateFile = $this->getFirstParent(LayoutGroup::class)->getTemplateFile();
}

return $this->getAbsolutePath($this->templateFile);
return $this->getAbsolutePath($templateFile);
}

/**
* @param string $templateFile
*/
public function setTemplateFile($templateFile)
{
$this->checkDefinitionFreezeState();
$this->checkConfigurationFreezeState();

$this->templateFile = $templateFile;
}
Expand All @@ -70,7 +67,7 @@ public function getLayout()
*/
public function setLayout($layout)
{
$this->checkDefinitionFreezeState();
$this->checkConfigurationFreezeState();

$this->layout = $layout;
}
Expand Down
8 changes: 5 additions & 3 deletions Classes/Configuration/View/Layouts/LayoutGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Romm\Formz\Configuration\View\Layouts;

use Romm\Formz\Configuration\AbstractConfiguration;
use Romm\Formz\Exceptions\DuplicateEntryException;
use Romm\Formz\Exceptions\EntryNotFoundException;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand Down Expand Up @@ -85,13 +86,14 @@ public function getItem($name)
/**
* @param string $name
* @return Layout
* @throws DuplicateEntryException
*/
public function addItem($name)
{
$this->checkDefinitionFreezeState();
$this->checkConfigurationFreezeState();

if ($this->hasItem($name)) {
throw new \Exception('todo'); // @todo
throw DuplicateEntryException::layoutItemAlreadyAdded($name, $this);
}

/** @var Layout $layout */
Expand All @@ -116,7 +118,7 @@ public function getTemplateFile()
*/
public function setTemplateFile($templateFile)
{
$this->checkDefinitionFreezeState();
$this->checkConfigurationFreezeState();

$this->templateFile = $templateFile;
}
Expand Down
12 changes: 7 additions & 5 deletions Classes/Configuration/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Romm\Formz\Configuration\AbstractConfiguration;
use Romm\Formz\Configuration\View\Classes\Classes;
use Romm\Formz\Configuration\View\Layouts\LayoutGroup;
use Romm\Formz\Exceptions\DuplicateEntryException;
use Romm\Formz\Exceptions\EntryNotFoundException;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand Down Expand Up @@ -96,13 +97,14 @@ public function getLayout($name)
/**
* @param string $name
* @return LayoutGroup
* @throws DuplicateEntryException
*/
public function addLayout($name)
{
$this->checkDefinitionFreezeState();
$this->checkConfigurationFreezeState();

if ($this->hasParent($name)) {
throw new \Exception('todo'); // @todo
if ($this->hasLayout($name)) {
throw DuplicateEntryException::viewLayoutAlreadyAdded($name);
}

/** @var LayoutGroup $layout */
Expand All @@ -120,7 +122,7 @@ public function addLayout($name)
*/
public function setLayoutRootPath($key, $path)
{
$this->checkDefinitionFreezeState();
$this->checkConfigurationFreezeState();

$this->layoutRootPaths[$key] = $path;
}
Expand Down Expand Up @@ -153,7 +155,7 @@ public function getAbsoluteLayoutRootPaths()
*/
public function setPartialRootPath($key, $path)
{
$this->checkDefinitionFreezeState();
$this->checkConfigurationFreezeState();

$this->partialRootPaths[$key] = $path;
}
Expand Down

0 comments on commit 4c0830e

Please sign in to comment.