PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.8.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.8.2
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Dependencies / DI / Definition / Source / DefinitionFile.php

DefinitionFile.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.8.2, at includes/Dependencies/DI/Definition/Source/DefinitionFile.php

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