1 | <?php |
||
23 | class Finder |
||
24 | { |
||
25 | /** @var string */ |
||
26 | private $sourceDirectory = ''; |
||
27 | |||
28 | /** @var string[] */ |
||
29 | private $exampleDirectories = []; |
||
30 | |||
31 | /** |
||
32 | * Attempts to find the example contents for the given descriptor. |
||
33 | */ |
||
34 | 5 | public function find(ExampleDescriptor $descriptor): string |
|
35 | { |
||
36 | 5 | $filename = $descriptor->getFilePath(); |
|
37 | |||
38 | 5 | $file = $this->getExampleFileContents($filename); |
|
39 | 5 | if (! $file) { |
|
40 | 1 | return "** File not found : {$filename} **"; |
|
41 | } |
||
42 | |||
43 | 4 | return implode('', array_slice($file, $descriptor->getStartingLine() - 1, $descriptor->getLineCount())); |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * Registers the project's root directory where an 'examples' folder can be expected. |
||
48 | */ |
||
49 | 1 | public function setSourceDirectory(string $directory = ''): void |
|
53 | |||
54 | /** |
||
55 | * Returns the project's root directory where an 'examples' folder can be expected. |
||
56 | */ |
||
57 | 1 | public function getSourceDirectory(): string |
|
61 | |||
62 | /** |
||
63 | * Registers a series of directories that may contain examples. |
||
64 | * |
||
65 | * @param string[] $directories |
||
66 | */ |
||
67 | 1 | public function setExampleDirectories(array $directories = []): void |
|
71 | |||
72 | /** |
||
73 | * Returns a series of directories that may contain examples. |
||
74 | * |
||
75 | * @return string[] |
||
76 | */ |
||
77 | 1 | public function getExampleDirectories(): array |
|
81 | |||
82 | /** |
||
83 | * Attempts to find the requested example file and returns its contents or null if no file was found. |
||
84 | * |
||
85 | * This method will try several methods in search of the given example file, the first one it encounters is |
||
86 | * returned: |
||
87 | * |
||
88 | * 1. Iterates through all examples folders for the given filename |
||
89 | * 2. Checks the source folder for the given filename |
||
90 | * 3. Checks the 'examples' folder in the current working directory for examples |
||
91 | * 4. Checks the path relative to the current working directory for the given filename |
||
92 | */ |
||
93 | 5 | private function getExampleFileContents(string $filename): ?array |
|
117 | |||
118 | /** |
||
119 | * Get example filepath based on the example directory inside your project. |
||
120 | */ |
||
121 | 1 | private function getExamplePathFromExampleDirectory(string $file): string |
|
125 | |||
126 | /** |
||
127 | * Returns a path to the example file in the given directory.. |
||
128 | */ |
||
129 | 1 | private function constructExamplePath(string $directory, string $file): string |
|
133 | |||
134 | /** |
||
135 | * Get example filepath based on sourcecode. |
||
136 | */ |
||
137 | 1 | private function getExamplePathFromSource(string $file): string |
|
146 | } |
||
147 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: