This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
598 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause | ||
* @copyright Copyright (c) 2016 Zend Technologies Ltd (http://www.zend.com) | ||
*/ | ||
|
||
namespace Zend\ComponentInstaller\ConfigDiscovery; | ||
|
||
class ConfigAggregator extends AbstractDiscovery | ||
{ | ||
/** | ||
* Configuration file to look for. | ||
* | ||
* @var string | ||
*/ | ||
protected $configFile = 'config/config.php'; | ||
|
||
/** | ||
* Expected pattern to match if the configuration file exists. | ||
* | ||
* Pattern is set in constructor to ensure PCRE quoting is correct. | ||
* | ||
* @var string | ||
*/ | ||
protected $expected = ''; | ||
|
||
public function __construct($projectDirectory = '') | ||
{ | ||
$this->expected = sprintf( | ||
'/new (?:%s?%s)?ConfigAggregator\(\s*(?:array\(|\[)/s', | ||
preg_quote('\\'), | ||
preg_quote('Zend\ConfigAggregator\\') | ||
); | ||
|
||
parent::__construct($projectDirectory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause | ||
* @copyright Copyright (c) 2016 Zend Technologies Ltd (http://www.zend.com) | ||
*/ | ||
|
||
namespace Zend\ComponentInstaller\Injector; | ||
|
||
use Composer\IO\IOInterface; | ||
|
||
trait ConditionalDiscoveryTrait | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* Prepends the package with a `\\` in order to ensure it is fully | ||
* qualified, preventing issues in config files that are namespaced. | ||
*/ | ||
public function inject($package, $type, IOInterface $io) | ||
{ | ||
if (! $this->validConfigAggregatorConfig()) { | ||
return; | ||
} | ||
|
||
parent::inject('\\' . $package, $type, $io); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* Prepends the package with a `\\` in order to ensure it is fully | ||
* qualified, preventing issues in config files that are namespaced. | ||
*/ | ||
public function remove($package, IOInterface $io) | ||
{ | ||
if (! $this->validConfigAggregatorConfig()) { | ||
return; | ||
} | ||
|
||
parent::remove('\\' . $package, $io); | ||
} | ||
|
||
/** | ||
* Does the config file hold valid ConfigAggregator configuration? | ||
* | ||
* @return bool | ||
*/ | ||
private function validConfigAggregatorConfig() | ||
{ | ||
$discoveryClass = $this->discoveryClass; | ||
$discovery = new $discoveryClass($this->getProjectRoot()); | ||
return $discovery->locate(); | ||
} | ||
|
||
/** | ||
* Calculate the project root from the config file | ||
* | ||
* @return string | ||
*/ | ||
private function getProjectRoot() | ||
{ | ||
if (static::DEFAULT_CONFIG_FILE === $this->configFile) { | ||
return ''; | ||
} | ||
return str_replace('/' . static::DEFAULT_CONFIG_FILE, '', $this->configFile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause | ||
* @copyright Copyright (c) 2016 Zend Technologies Ltd (http://www.zend.com) | ||
*/ | ||
|
||
namespace Zend\ComponentInstaller\Injector; | ||
|
||
use Zend\ComponentInstaller\ConfigDiscovery\ConfigAggregator as ConfigAggregatorDiscovery; | ||
|
||
class ConfigAggregatorInjector extends AbstractInjector | ||
{ | ||
use ConditionalDiscoveryTrait; | ||
|
||
const DEFAULT_CONFIG_FILE = 'config/config.php'; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $allowedTypes = [ | ||
self::TYPE_CONFIG_PROVIDER, | ||
]; | ||
|
||
/** | ||
* Configuration file to update. | ||
* | ||
* @var string | ||
*/ | ||
protected $configFile = self::DEFAULT_CONFIG_FILE; | ||
|
||
/** | ||
* Discovery class, for testing if this injector is valid for the given | ||
* configuration. | ||
* | ||
* @var string | ||
*/ | ||
protected $discoveryClass = ConfigAggregatorDiscovery::class; | ||
|
||
/** | ||
* Patterns and replacements to use when registering a code item. | ||
* | ||
* Pattern is set in constructor due to PCRE quoting issues. | ||
* | ||
* @var string[] | ||
*/ | ||
protected $injectionPatterns = [ | ||
self::TYPE_CONFIG_PROVIDER => [ | ||
'pattern' => '', | ||
'replacement' => "\$1\n\$2%s::class,\n\$2", | ||
], | ||
]; | ||
|
||
/** | ||
* Pattern to use to determine if the code item is registered. | ||
* | ||
* Set in constructor due to PCRE quoting issues. | ||
* | ||
* @var string | ||
*/ | ||
protected $isRegisteredPattern = ''; | ||
|
||
/** | ||
* Patterns and replacements to use when removing a code item. | ||
* | ||
* @var string[] | ||
*/ | ||
protected $removalPatterns = [ | ||
'pattern' => '/^\s+%s::class,\s*$/m', | ||
'replacement' => '', | ||
]; | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* Sets $isRegisteredPattern and pattern for $injectionPatterns to ensure | ||
* proper PCRE quoting. | ||
*/ | ||
public function __construct($projectRoot = '') | ||
{ | ||
$this->isRegisteredPattern = '/new (?:' | ||
. preg_quote('\\') | ||
. '?' | ||
. preg_quote('Zend\ConfigAggregator\\') | ||
. ')?ConfigAggregator\(\s*(?:array\(|\[).*\s+%s::class/s'; | ||
|
||
$this->injectionPatterns[self::TYPE_CONFIG_PROVIDER]['pattern'] = sprintf( | ||
"/(new (?:%s?%s)?ConfigAggregator\(\s*(?:array\(|\[)\s*)(?:\r|\n|\r\n)(\s*)/", | ||
preg_quote('\\'), | ||
preg_quote('Zend\ConfigAggregator\\') | ||
); | ||
|
||
parent::__construct($projectRoot); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause | ||
* @copyright Copyright (c) 2016 Zend Technologies Ltd (http://www.zend.com) | ||
*/ | ||
|
||
namespace ZendTest\ComponentInstaller\ConfigDiscovery; | ||
|
||
use org\bovigo\vfs\vfsStream; | ||
use org\bovigo\vfs\vfsStreamDirectory; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\ComponentInstaller\ConfigDiscovery\ConfigAggregator; | ||
|
||
class ConfigAggregatorTest extends TestCase | ||
{ | ||
private $configDir; | ||
|
||
private $locator; | ||
|
||
public function setUp() | ||
{ | ||
$this->configDir = vfsStream::setup('project'); | ||
$this->locator = new ConfigAggregator( | ||
vfsStream::url('project') | ||
); | ||
} | ||
|
||
public function testAbsenceOfFileReturnsFalseOnLocate() | ||
{ | ||
$this->assertFalse($this->locator->locate()); | ||
} | ||
|
||
public function testLocateReturnsFalseWhenFileDoesNotHaveExpectedContents() | ||
{ | ||
vfsStream::newFile('config/config.php') | ||
->at($this->configDir) | ||
->setContent('<' . "?php\nreturn [];"); | ||
$this->assertFalse($this->locator->locate()); | ||
} | ||
|
||
public function validExpressiveConfigContents() | ||
{ | ||
// @codingStandardsIgnoreStart | ||
return [ | ||
'fqcn-short-array' => ['<' . "?php\n\$aggregator = new Zend\ConfigAggregator\ConfigAggregator([\n]);"], | ||
'globally-qualified-short-array' => ['<' . "?php\n\$aggregator = new \Zend\ConfigAggregator\ConfigAggregator([\n]);"], | ||
'imported-short-array' => ['<' . "?php\n\$aggregator = new ConfigAggregator([\n]);"], | ||
'fqcn-long-array' => ['<' . "?php\n\$aggregator = new Zend\ConfigAggregator\ConfigAggregator(array(\n));"], | ||
'globally-qualified-long-array' => ['<' . "?php\n\$aggregator = new \Zend\ConfigAggregator\ConfigAggregator(array(\n));"], | ||
'imported-long-array' => ['<' . "?php\n\$aggregator = new ConfigAggregator(array(\n));"], | ||
]; | ||
// @codingStandardsIgnoreEnd | ||
} | ||
|
||
/** | ||
* @dataProvider validExpressiveConfigContents | ||
*/ | ||
public function testLocateReturnsTrueWhenFileExistsAndHasExpectedContent($contents) | ||
{ | ||
vfsStream::newFile('config/config.php') | ||
->at($this->configDir) | ||
->setContent($contents); | ||
|
||
$this->assertTrue($this->locator->locate()); | ||
} | ||
} |
Oops, something went wrong.