From e30ec95fac27826e8d9a90116cdfe9fde40a5ea3 Mon Sep 17 00:00:00 2001 From: Nick Lombard Date: Wed, 29 Jan 2014 03:30:20 +0200 Subject: [PATCH 1/7] Added more packagist bling And rearranged to conformance with other projects --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 3f5a5df..9dc92d7 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,6 @@ -Respect\Config [![Build Status](https://travis-ci.org/Respect/Config.png?branch=develop)](https://travis-ci.org/Respect/Config) +Respect\Config [![Build Status](https://travis-ci.org/Respect/Config.png?branch=develop)](https://travis-ci.org/Respect/Config) [![Latest Stable Version](https://poser.pugx.org/respect/config/v/stable.png)](https://packagist.org/packages/respect/config) [![Total Downloads](https://poser.pugx.org/respect/config/downloads.png)](https://packagist.org/packages/respect/config) [![Latest Unstable Version](https://poser.pugx.org/respect/config/v/unstable.png)](https://packagist.org/packages/respect/config) [![License](https://poser.pugx.org/respect/config/license.png)](https://packagist.org/packages/respect/config) ============== -[![Total Downloads](https://poser.pugx.org/respect/config/downloads.png)](https://packagist.org/packages/respect/config) -[![Latest Stable Version](https://poser.pugx.org/respect/config/v/stable.png)](https://packagist.org/packages/respect/config) - A powerful, small, deadly simple configurator and dependency injection container made to be easy. Featuring: From 78b26bd0ecc60a30cf1d0ffe19c9acd72f354227 Mon Sep 17 00:00:00 2001 From: Nick Lombard Date: Wed, 29 Jan 2014 03:32:11 +0200 Subject: [PATCH 2/7] Moved badges to fit screen width --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9dc92d7..8265906 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ -Respect\Config [![Build Status](https://travis-ci.org/Respect/Config.png?branch=develop)](https://travis-ci.org/Respect/Config) [![Latest Stable Version](https://poser.pugx.org/respect/config/v/stable.png)](https://packagist.org/packages/respect/config) [![Total Downloads](https://poser.pugx.org/respect/config/downloads.png)](https://packagist.org/packages/respect/config) [![Latest Unstable Version](https://poser.pugx.org/respect/config/v/unstable.png)](https://packagist.org/packages/respect/config) [![License](https://poser.pugx.org/respect/config/license.png)](https://packagist.org/packages/respect/config) +Respect\Config ============== +[![Build Status](https://travis-ci.org/Respect/Config.png?branch=develop)](https://travis-ci.org/Respect/Config) [![Latest Stable Version](https://poser.pugx.org/respect/config/v/stable.png)](https://packagist.org/packages/respect/config) [![Total Downloads](https://poser.pugx.org/respect/config/downloads.png)](https://packagist.org/packages/respect/config) [![Latest Unstable Version](https://poser.pugx.org/respect/config/v/unstable.png)](https://packagist.org/packages/respect/config) [![License](https://poser.pugx.org/respect/config/license.png)](https://packagist.org/packages/respect/config) A powerful, small, deadly simple configurator and dependency injection container made to be easy. Featuring: From ac22895dd0e419acd414599f92bb9efb9e7cfc84 Mon Sep 17 00:00:00 2001 From: Claudson Oliveira Date: Wed, 29 Jan 2014 09:49:46 -0200 Subject: [PATCH 3/7] Update copyright date --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 9e42168..05b7b55 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2009-2013, Alexandre Gomes Gaigalas. +Copyright (c) 2009-2014, Alexandre Gomes Gaigalas. All rights reserved. Redistribution and use in source and binary forms, with or without modification, From 738700c48417fd89f4c0bae146b4b846561ae4a4 Mon Sep 17 00:00:00 2001 From: cloudson Date: Thu, 13 Mar 2014 22:23:14 -0300 Subject: [PATCH 4/7] Fix container setting closure --- library/Respect/Config/Container.php | 6 +++++- tests/library/Respect/Config/ContainerTest.php | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/library/Respect/Config/Container.php b/library/Respect/Config/Container.php index bb4be3a..5d41e62 100644 --- a/library/Respect/Config/Container.php +++ b/library/Respect/Config/Container.php @@ -174,8 +174,12 @@ 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; + } $this->parseItem($key, $value); + } } public function __get($name) diff --git a/tests/library/Respect/Config/ContainerTest.php b/tests/library/Respect/Config/ContainerTest.php index 26b7caa..d69c04d 100644 --- a/tests/library/Respect/Config/ContainerTest.php +++ b/tests/library/Respect/Config/ContainerTest.php @@ -251,6 +251,16 @@ public function testGetItemLazyLoad() $this->assertEquals('ok', $c->getItem('foo', false)); } + public function testClosureWithLoadedFile() + { + $ini = <<panda = function() { return 'ok'; }; + $this->assertEquals('ok', $c->getItem('panda', false)); + } + public function testLazyLoadinessOnMultipleConfigLevels() { $GLOBALS['_SHIT_'] = false; From 75f47d41b1535a2085c122f120b80a670d8a14c1 Mon Sep 17 00:00:00 2001 From: Jack Makiyama Date: Wed, 7 Jan 2015 02:42:28 -0200 Subject: [PATCH 5/7] Update copyright date --- LICENSE | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index 05b7b55..4b241ad 100644 --- a/LICENSE +++ b/LICENSE @@ -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, diff --git a/README.md b/README.md index 8265906..09b5bca 100644 --- a/README.md +++ b/README.md @@ -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, From 16b13f67b742cbe09267a82cafcb235b133d748f Mon Sep 17 00:00:00 2001 From: Jack Makiyama Date: Wed, 7 Jan 2015 03:11:11 -0200 Subject: [PATCH 6/7] Fix PSR2 code standard --- library/Respect/Config/Container.php | 142 +++++++++++++++--------- library/Respect/Config/Instantiator.php | 76 ++++++++----- 2 files changed, 136 insertions(+), 82 deletions(-) diff --git a/library/Respect/Config/Container.php b/library/Respect/Config/Container.php index 5d41e62..4048fd0 100644 --- a/library/Respect/Config/Container.php +++ b/library/Respect/Config/Container.php @@ -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); } @@ -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')) { @@ -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; } @@ -76,34 +80,42 @@ 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); } @@ -111,8 +123,9 @@ public function getItem($name, $raw=false) 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); } @@ -120,10 +133,17 @@ public function loadString($configurator) 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' + ) ) ); } @@ -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); } @@ -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; } @@ -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; } @@ -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); } @@ -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) @@ -274,40 +306,45 @@ 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) @@ -315,9 +352,10 @@ 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 ); } @@ -336,6 +374,4 @@ protected function lazyLoad($name) return $callback(); } - } - diff --git a/library/Respect/Config/Instantiator.php b/library/Respect/Config/Instantiator.php index 455eb12..bc0e82d 100644 --- a/library/Respect/Config/Instantiator.php +++ b/library/Respect/Config/Instantiator.php @@ -45,40 +45,49 @@ public function getClassName() return $this->className; } - public function getInstance($forceNew=false) + public function getInstance($forceNew = false) { - if ($this->mode == static::MODE_FACTORY) + if ($this->mode == static::MODE_FACTORY) { $this->instance = null; + } - if ($this->instance && !$forceNew) + if ($this->instance && !$forceNew) { return $this->instance; + } $className = $this->className; $staticMethods = count($this->staticMethodCalls); foreach ($this->staticMethodCalls as $methodCalls) { - $this->performMethodCalls($className, $methodCalls, + $this->performMethodCalls( + $className, + $methodCalls, function($result) use ($className, &$instance, $staticMethods) { - if ($result instanceof $className || ($staticMethods == 1 && is_object($result))) + if ($result instanceof $className || ($staticMethods == 1 && is_object($result))) { $instance = $result; + } } ); } $constructor = $this->reflection->getConstructor(); $hasConstructor = ($constructor) ? $constructor->isPublic() : false ; - if (empty($instance)) - if (empty($this->constructor) || !$hasConstructor) + if (empty($instance)) { + if (empty($this->constructor) || !$hasConstructor) { $instance = new $className; - else + } else { $instance = $this->reflection->newInstanceArgs( $this->cleanupParams($this->constructor) ); + } + } - foreach ($this->propertySetters as $property => $value) + foreach ($this->propertySetters as $property => $value) { $instance->{$property} = $this->lazyLoad($value); + } - foreach ($this->methodCalls as $methodCalls) + foreach ($this->methodCalls as $methodCalls) { $this->performMethodCalls($instance, $methodCalls); + } return $this->instance = $instance; } @@ -97,27 +106,30 @@ public function setParam($name, $value) { $value = $this->processValue($value); - if ($this->matchStaticMethod($name)) + if ($this->matchStaticMethod($name)) { $this->staticMethodCalls[] = array($name, $value); - elseif ($this->matchConstructorParam($name)) + } elseif ($this->matchConstructorParam($name)) { $this->constructor[$name] = $value; - elseif ($this->matchFullConstructor($name, $value)) + } elseif ($this->matchFullConstructor($name, $value)) { $this->constructor = $value; - elseif ($this->matchMethod($name)) + } elseif ($this->matchMethod($name)) { $this->methodCalls[] = array($name, $value); - else + } else { $this->propertySetters[$name] = $value; + } $this->params[$name] = $value; } protected function cleanupParams(array $params) { - while (null === end($params)) + while (null === end($params)) { unset($params[key($params)]); + } - foreach ($params as &$p) + foreach ($params as &$p) { $p = $this->lazyLoad($p); + } return $params; } @@ -132,21 +144,25 @@ protected function findConstructorParams(ReflectionClass $class) $params = array(); $constructor = $class->getConstructor(); - if (!$constructor) + if (!$constructor) { return array(); + } - foreach ($constructor->getParameters() as $param) + foreach ($constructor->getParameters() as $param) { $params[$param->getName()] = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null; + } return $params; } protected function processValue($value) { - if (is_array($value)) - foreach ($value as $valueKey => $subValue) + if (is_array($value)) { + foreach ($value as $valueKey => $subValue) { $value[$valueKey] = $this->processValue($subValue); + } + } return $value; } @@ -173,22 +189,24 @@ protected function matchStaticMethod($name) && $this->reflection->getMethod($name)->isStatic(); } - protected function performMethodCalls($class, array $methodCalls, $resultCallback=null) + protected function performMethodCalls($class, array $methodCalls, $resultCallback = null) { list($methodName, $calls) = $methodCalls; - $resultCallback = $resultCallback ?: function(){}; + $resultCallback = $resultCallback ?: function() { + + }; - foreach ($calls as $arguments) - if (is_array($arguments)) + foreach ($calls as $arguments) { + if (is_array($arguments)) { $resultCallback(call_user_func_array( array($class, $methodName), $this->cleanUpParams($arguments) )); - elseif (!is_null($arguments)) + } elseif (!is_null($arguments)) { $resultCallback(call_user_func(array($class, $methodName), $this->lazyLoad($arguments))); - else + } else { $resultCallback(call_user_func(array($class, $methodName))); + } + } } - } - From 15882cdefd04b8c3e94681397a58e95e269132ad Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Wed, 7 Jan 2015 10:54:43 -0200 Subject: [PATCH 7/7] Update continuous integration settings --- .scrutinizer.yml | 16 ++++++++++++++++ .travis.yml | 28 +++++++++++++++++++--------- README.md | 4 +++- 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 .scrutinizer.yml diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..aeca599 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,16 @@ +filter: + excluded_paths: + - tests/* + +checks: + php: + code_rating: true + +tools: + external_code_coverage: true + php_analyzer: true + php_changetracking: true + php_cpd: true + php_mess_detector: true + php_pdepend: true + sensiolabs_security_checker: true diff --git a/.travis.yml b/.travis.yml index 40fc56a..900a1e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,29 @@ -language: php +sudo: + false + +language: + php php: - 5.3 - 5.4 -env: FOUNDATION_NO_WAIT=1 +cache: + directories: + - vendor before_script: - - "make composer-install-dev" - - "phpenv rehash" + - composer install --dev --no-interaction --prefer-source script: - - "make testdox" + - vendor/bin/phpunit --configuration tests/phpunit.xml --colors --coverage-clover=coverage.clover tests/ + +after_script: + - wget https://scrutinizer-ci.com/ocular.phar + - php ocular.phar code-coverage:upload --format=php-clover coverage.clover -branches: - only: - - master - - develop +notifications: + irc: + channels: + - "irc.freenode.org#php-respect" + use_notice: true diff --git a/README.md b/README.md index 09b5bca..c4ae7f7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Respect\Config +Respect\Config ============== [![Build Status](https://travis-ci.org/Respect/Config.png?branch=develop)](https://travis-ci.org/Respect/Config) [![Latest Stable Version](https://poser.pugx.org/respect/config/v/stable.png)](https://packagist.org/packages/respect/config) [![Total Downloads](https://poser.pugx.org/respect/config/downloads.png)](https://packagist.org/packages/respect/config) [![Latest Unstable Version](https://poser.pugx.org/respect/config/v/unstable.png)](https://packagist.org/packages/respect/config) [![License](https://poser.pugx.org/respect/config/license.png)](https://packagist.org/packages/respect/config) @@ -15,6 +15,8 @@ Installation Packages available on [PEAR](http://respect.li/pear) and [Composer](http://packagist.org/packages/Respect/Config). Autoloading is [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) compatible. +Works on PHP 5.3 and 5.4 only. + Autoloading -----------