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