| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition\Source; |
| 6 |
|
| 7 |
/** |
| 8 |
* Reads WPDeveloper\BetterDocs\Dependencies\DI definitions from a file returning a PHP array. |
| 9 |
* |
| 10 |
* @author Matthieu Napoli <matthieu@mnapoli.fr> |
| 11 |
*/ |
| 12 |
class DefinitionFile extends DefinitionArray |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @var bool |
| 16 |
*/ |
| 17 |
private $initialized = false; |
| 18 |
|
| 19 |
/** |
| 20 |
* File containing definitions, or null if the definitions are given as a PHP array. |
| 21 |
* @var string|null |
| 22 |
*/ |
| 23 |
private $file; |
| 24 |
|
| 25 |
/** |
| 26 |
* @param string $file File in which the definitions are returned as an array. |
| 27 |
*/ |
| 28 |
public function __construct($file, Autowiring $autowiring = null) |
| 29 |
{ |
| 30 |
// Lazy-loading to improve performances |
| 31 |
$this->file = $file; |
| 32 |
|
| 33 |
parent::__construct([], $autowiring); |
| 34 |
} |
| 35 |
|
| 36 |
public function getDefinition(string $name) |
| 37 |
{ |
| 38 |
$this->initialize(); |
| 39 |
|
| 40 |
return parent::getDefinition($name); |
| 41 |
} |
| 42 |
|
| 43 |
public function getDefinitions() : array |
| 44 |
{ |
| 45 |
$this->initialize(); |
| 46 |
|
| 47 |
return parent::getDefinitions(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Lazy-loading of the definitions. |
| 52 |
*/ |
| 53 |
private function initialize() |
| 54 |
{ |
| 55 |
if ($this->initialized === true) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$definitions = require $this->file; |
| 60 |
|
| 61 |
if (! is_array($definitions)) { |
| 62 |
throw new \Exception("File {$this->file} should return an array of definitions"); |
| 63 |
} |
| 64 |
|
| 65 |
$this->addDefinitions($definitions); |
| 66 |
|
| 67 |
$this->initialized = true; |
| 68 |
} |
| 69 |
} |
| 70 |
|