-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split file handling of ArrayDefinitionSource to new PHPFileDefinition…
…Source
- Loading branch information
Showing
7 changed files
with
133 additions
and
94 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,76 @@ | ||
<?php | ||
/** | ||
* PHP-DI | ||
* | ||
* @link http://php-di.org/ | ||
* @copyright Matthieu Napoli (http://mnapoli.fr/) | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file) | ||
*/ | ||
|
||
namespace DI\Definition\Source; | ||
|
||
use DI\Definition\Exception\DefinitionException; | ||
use DI\Definition\MergeableDefinition; | ||
|
||
/** | ||
* Reads DI definitions from a file returning a PHP array. | ||
* | ||
* @author Matthieu Napoli <matthieu@mnapoli.fr> | ||
*/ | ||
class PHPFileDefinitionSource extends ArrayDefinitionSource | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
private $initialized = false; | ||
|
||
/** | ||
* File containing definitions, or null if the definitions are given as a PHP array. | ||
* @var string|null | ||
*/ | ||
private $file; | ||
|
||
/** | ||
* @param string $file File in which the definitions are returned as an array. | ||
*/ | ||
public function __construct($file) | ||
{ | ||
// Lazy-loading to improve performances | ||
$this->file = $file; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefinition($name, MergeableDefinition $parentDefinition = null) | ||
{ | ||
$this->initialize(); | ||
|
||
return parent::getDefinition($name, $parentDefinition); | ||
} | ||
|
||
/** | ||
* Lazy-loading of the definitions. | ||
* @throws DefinitionException | ||
*/ | ||
private function initialize() | ||
{ | ||
if ($this->initialized === true) { | ||
return; | ||
} | ||
|
||
if (! is_readable($this->file)) { | ||
throw new DefinitionException("File {$this->file} doesn't exist or is not readable"); | ||
} | ||
|
||
$definitions = require $this->file; | ||
|
||
if (! is_array($definitions)) { | ||
throw new DefinitionException("File {$this->file} should return an array of definitions"); | ||
} | ||
|
||
$this->addDefinitions($definitions); | ||
|
||
$this->initialized = true; | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
tests/UnitTests/DI/Definition/Source/PHPFileDefinitionSourceTest.php
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,38 @@ | ||
<?php | ||
/** | ||
* PHP-DI | ||
* | ||
* @link http://php-di.org/ | ||
* @copyright Matthieu Napoli (http://mnapoli.fr/) | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file) | ||
*/ | ||
|
||
namespace UnitTests\DI\Definition\Source; | ||
|
||
use DI\Definition\ClassDefinition; | ||
use DI\Definition\Source\PHPFileDefinitionSource; | ||
|
||
/** | ||
* Test class for PHPFileDefinitionSource | ||
*/ | ||
class PHPFileDefinitionSourceTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @covers \DI\Definition\Source\PHPFileDefinitionSource | ||
*/ | ||
public function testLoadFromFile() | ||
{ | ||
$source = new PHPFileDefinitionSource(__DIR__ . '/Fixtures/definitions.php'); | ||
|
||
$definition = $source->getDefinition('foo'); | ||
$this->assertNotNull($definition); | ||
$this->assertEquals('bar', $definition->getValue()); | ||
$this->assertInternalType('string', $definition->getValue()); | ||
|
||
/** @var $definition ClassDefinition */ | ||
$definition = $source->getDefinition('bim'); | ||
$this->assertInstanceOf('DI\Definition\ClassDefinition', $definition); | ||
$this->assertEquals('bim', $definition->getName()); | ||
$this->assertEquals('bim', $definition->getClassName()); | ||
} | ||
} |