Skip to content

Commit

Permalink
Merge pull request #44 from jackmakiyama/develop
Browse files Browse the repository at this point in the history
Fix PSR2 code standard
  • Loading branch information
henriquemoody committed Jan 7, 2015
2 parents 0ecbd5d + 16b13f6 commit aef3bd3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 84 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2009-2014, Alexandre Gomes Gaigalas.
Copyright (c) 2009-2015, Alexandre Gomes Gaigalas.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ good work and you'll never face them.
License Information
===================

Copyright (c) 2009-2012, Alexandre Gomes Gaigalas.
Copyright (c) 2009-2015, Alexandre Gomes Gaigalas.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
142 changes: 89 additions & 53 deletions library/Respect/Config/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ class Container extends ArrayObject
{
protected $configurator;

public function __construct($configurator=null)
public function __construct($configurator = null)
{
$this->configurator = $configurator;
}

public function __isset($name)
{
if ($this->configurator)
if ($this->configurator) {
$this->configure();
}

return parent::offsetExists($name);
}
Expand All @@ -46,8 +47,9 @@ function ($param) use ($container) {
},
$mirror->getParameters()
);
if ($object)
if ($object) {
return $mirror->invokeArgs($object, $arguments);
}
return $mirror->invokeArgs($arguments);
}
if ((bool) array_filter(func_get_args(), 'is_object')) {
Expand All @@ -56,11 +58,13 @@ function ($param) use ($container) {
}
}

foreach ($spec as $name => $item)
foreach ($spec as $name => $item) {
parent::offsetSet($name, $item);
}

if ($this->configurator)
if ($this->configurator) {
$this->configure();
}

return $this;
}
Expand All @@ -76,54 +80,70 @@ protected function configure()
$configurator = $this->configurator;
$this->configurator = null;

if (is_null($configurator))
if (is_null($configurator)) {
return;
}

if (is_array($configurator))
if (is_array($configurator)) {
return $this->loadArray($configurator);
}

if (file_exists($configurator))
if (is_file($configurator))
if (file_exists($configurator)) {
if (is_file($configurator)) {
return $this->loadFile($configurator);
elseif (is_dir($configurator))
} elseif (is_dir($configurator)) {
return $this->loadFileMultiple($configurator, scandir($configurator));
}
}

if (is_string($configurator))
if (is_string($configurator)) {
return $this->loadString($configurator);
}

throw new Argument("Invalid input. Must be a valid file or array");
}

public function getItem($name, $raw=false)
public function getItem($name, $raw = false)
{
if ($this->configurator)
if ($this->configurator) {
$this->configure();
}

if (!isset($this[$name]))
if (!isset($this[$name])) {
throw new Argument("Item $name not found");
}

if ($raw || !is_callable($this[$name]))
if ($raw || !is_callable($this[$name])) {
return $this[$name];
}

return $this->lazyLoad($name);
}

public function loadString($configurator)
{
$iniData = parse_ini_string($configurator, true);
if (false === $iniData || count($iniData) == 0)
if (false === $iniData || count($iniData) == 0) {
throw new Argument("Invalid configuration string");
}

return $this->loadArray($iniData);
}

public function loadFileMultiple($folder, array $configurators)
{
return $this->loadStringMultiple(
array_map('file_get_contents',
array_filter(array_map(function ($v) use ($folder) {
return $folder.DIRECTORY_SEPARATOR.$v;
}, $configurators), 'is_file')
array_map(
'file_get_contents',
array_filter(
array_map(
function ($v) use ($folder) {
return $folder.DIRECTORY_SEPARATOR.$v;
},
$configurators
),
'is_file'
)
)
);
}
Expand All @@ -147,20 +167,23 @@ public function loadStringMultiple(array $configurators)
$usagesFirst = count($usedByFirstDeclaredBySecond);
$usagesSecond = count($usedBySecondDeclaredByFirst);

if ($usagesFirst == $usagesSecond)
if ($usagesFirst == $usagesSecond) {
return 0;
else
} else {
return $usagesFirst < $usagesSecond ? -1 : 1;
}
});
foreach ($configurators as $c)
foreach ($configurators as $c) {
$this->loadString($c);
}
}

public function loadFile($configurator)
{
$iniData = parse_ini_file($configurator, true);
if (false === $iniData)
if (false === $iniData) {
throw new Argument("Invalid configuration INI file");
}

return $this->loadArray($iniData);
}
Expand All @@ -174,7 +197,7 @@ protected function state()

public function loadArray(array $configurator)
{
foreach ($this->state() + $configurator as $key => $value){
foreach ($this->state() + $configurator as $key => $value) {
if ($value instanceof \Closure) {
continue;
}
Expand All @@ -189,8 +212,9 @@ public function __get($name)

public function __set($name, $value)
{
if (isset($this[$name]) && $this[$name] instanceof Instantiator)
if (isset($this[$name]) && $this[$name] instanceof Instantiator) {
$this[$name]->setInstance($value);
}
$this[$name] = $value;
}

Expand All @@ -207,28 +231,33 @@ protected function keyHasInstantiator($key)
protected function parseItem($key, $value)
{
$key = trim($key);
if ($this->keyHasInstantiator($key))
if ($this->keyHasStateInstance($key, $k))
$this->offsetSet($key, $this[$k]);
else
if ($this->keyHasInstantiator($key)) {
if ($this->keyHasStateInstance($key, $k)) {
$this->offsetSet($key, $this[$k]);
} else {
$this->parseInstantiator($key, $value);
else
}
} else {
$this->parseStandardItem($key, $value);
}
}

protected function parseSubValues(&$value)
{
foreach ($value as &$subValue)
foreach ($value as &$subValue) {
$subValue = $this->parseValue($subValue);
}

return $value;
}

protected function parseStandardItem($key, &$value)
{
if (is_array($value))
if (is_array($value)) {
$this->parseSubValues($value);
else
} else {
$value = $this->parseValue($value);
}

$this->offsetSet($key, $value);
}
Expand All @@ -247,23 +276,26 @@ protected function parseInstantiator($key, $value)
}
$instantiator = new Instantiator($keyClass);

if (is_array($value))
foreach ($value as $property => $pValue)
if (is_array($value)) {
foreach ($value as $property => $pValue) {
$instantiator->setParam($property, $this->parseValue($pValue));
else
}
} else {
$instantiator->setParam('__construct', $this->parseValue($value));
}

$this->offsetSet($keyName, $instantiator);
}

protected function parseValue($value)
{
if (is_array($value))
if (is_array($value)) {
return $this->parseSubValues($value);
elseif (empty($value))
} elseif (empty($value)) {
return null;
else
} else {
return $this->parseSingleValue($value);
}
}

protected function hasCompleteBrackets($value)
Expand All @@ -274,50 +306,56 @@ protected function hasCompleteBrackets($value)
protected function parseSingleValue($value)
{
$value = trim($value);
if ($this->hasCompleteBrackets($value))
if ($this->hasCompleteBrackets($value)) {
return $this->parseBrackets($value);
else
} else {
return $this->parseConstants($value);
}
}

protected function parseConstants($value)
{
if (preg_match('/^[A-Z_]+([:]{2}[A-Z_]+)?$/', $value) && defined($value))
if (preg_match('/^[A-Z_]+([:]{2}[A-Z_]+)?$/', $value) && defined($value)) {
return constant($value);
else
} else {
return $value;
}
}

protected function matchSequence(&$value)
{
if (preg_match('/^\[(.*?,.*?)\]$/', $value, $match))
if (preg_match('/^\[(.*?,.*?)\]$/', $value, $match)) {
return (boolean) ($value = $match[1]);
}
}

protected function matchReference(&$value)
{
if (preg_match('/^\[(\w+)+\]$/', $value, $match))
if (preg_match('/^\[(\w+)+\]$/', $value, $match)) {
return (boolean) ($value = $match[1]);
}
}

protected function parseBrackets($value)
{
if ($this->matchSequence($value))
if ($this->matchSequence($value)) {
return $this->parseArgumentList($value);
elseif ($this->matchReference($value))
} elseif ($this->matchReference($value)) {
return $this->getItem($value, true);
else
} else {
return $this->parseVariables($value);
}
}

protected function parseVariables($value)
{
$self = $this;
return preg_replace_callback(
'/\[(\w+)\]/',
function($match) use(&$self) {
function($match) use (&$self) {
return $self[$match[1]] ? : '';
}, $value
},
$value
);
}

Expand All @@ -336,6 +374,4 @@ protected function lazyLoad($name)

return $callback();
}

}

Loading

0 comments on commit aef3bd3

Please sign in to comment.