Skip to content

Commit

Permalink
Improve documentation of ContextHelper, fix issue with config empty s…
Browse files Browse the repository at this point in the history
…torage. Issue #182
  • Loading branch information
shakaran committed Oct 3, 2017
1 parent 4f20fb3 commit 89d74ae
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions Helper/ContextHelper.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<?php
/**
* ContextHelper.php
* symfony3
* Date: 12.06.16
* ContextHelper class
*
* Instead of fully relying on blocks and includes, this class that support
* the twig global named avanzu_admin_context to store and retrieve particular
* values throughout the page rendering.
*
* This is basically a parameter bag "on-page" with some pre-defined values
* based on the bundle configuration.
*
* The implemenation relies in a ArrayObject native PHP object, so it recieves
* all the changes via avanzu_admin_context.options to store the new modified
* values in the internal storage of ArrayObject, which is accessible via the
* getter and setter in option attribute class.
*/

namespace Avanzu\AdminThemeBundle\Helper;

use Avanzu\AdminThemeBundle\Routing\RouteAliasCollection;
Expand All @@ -17,61 +26,57 @@ class ContextHelper extends \ArrayObject
* @var RouteAliasCollection
*/
private $router;

/**
* ContextHelper constructor.
*
* @param array $config
* @param RouteAliasCollection $router
* @param array $config The data under avanzu_admin_theme.options config
* @param RouteAliasCollection $router avanzu_admin_theme.admin_route class route service
*/
public function __construct(array $config, RouteAliasCollection $router)
{
$this->initialize($config);
$this->router = $router;
}

/**
* @param array $config
* Create a OptionResolver with default parameters and overwrite the context
* with the default options in avanzu_admin_theme.options
*
* @param array $config The data under avanzu_admin_theme.options config
*/
protected function initialize(array $config = [])
{
// Create a resolve and configure the defaults
$resolver = new OptionsResolver();
$this->configureDefaults($resolver);

if(!empty($config))
try
{
try
{
$newConfig = $resolver->resolve($config);

foreach($newConfig as $configKey => $configValue)
{
//if(!is_array($configValue))
//{
$this->setOption($configKey, $configValue);
//}
//else @todo Not sure if it is needed handle second level index of arrays
//{

//}
}
}
catch(UndefinedOptionsException $e)
{
echo $e->getMessage() . PHP_EOL;
print_r($config, TRUE);
}
// Parse the config in avanzu_admin_theme.options as array object in avanzu_admin_context.options
$newConfig = $resolver->resolve($config);
// Change the internal storage array in the ArrayObject
$this->exchangeArray($newConfig);
}
catch(UndefinedOptionsException $e)
{
echo $e->getMessage() . PHP_EOL;
print_r($config, TRUE);
}

}

/**
* Get attribute method for options. It uses a interal copy array of the
* storage in the ArrayObject
*
* @return array
*/
public function getOptions()
{
return $this->getArrayCopy();
}

/**
* @param $name
* @param $value
Expand All @@ -81,10 +86,10 @@ public function getOptions()
public function setOption($name, $value)
{
$this->offsetSet($name, $value);

return $this;
}

/**
* @param $name
*
Expand All @@ -94,7 +99,7 @@ public function hasOption($name)
{
return $this->offsetExists($name);
}

/**
* @param $name
* @param null $default
Expand All @@ -105,7 +110,7 @@ public function getOption($name, $default = null)
{
return $this->offsetExists($name) ? $this->offsetGet($name) : $default;
}

/**
* @param $name
*
Expand All @@ -115,7 +120,7 @@ public function hasAlias($name)
{
return $this->router->hasAlias($name);
}

/**
* @param $name
*
Expand All @@ -125,7 +130,7 @@ public function fromAlias($name)
{
return $this->router->getRouteByAlias($name);
}

/**
* @param OptionsResolver $resolver
*/
Expand Down

0 comments on commit 89d74ae

Please sign in to comment.