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