1 | <?php |
||
2 | |||
3 | namespace Frontend\Core\Engine; |
||
4 | |||
5 | use Exception; |
||
0 ignored issues
–
show
|
|||
6 | |||
7 | /** |
||
8 | * This class will take care of functionality pertaining themes. |
||
9 | */ |
||
10 | class Theme |
||
11 | { |
||
12 | /** |
||
13 | * The current active theme's name |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | private static $theme; |
||
18 | |||
19 | /** |
||
20 | * Get the file path based on the theme. |
||
21 | * If it does not exist in the theme it will return $file. |
||
22 | * |
||
23 | * @param string $filePath Path to the file. |
||
24 | * |
||
25 | * @throws Exception |
||
26 | * |
||
27 | * @return string Path to the (theme) file. |
||
28 | */ |
||
29 | 26 | public static function getPath(string $filePath): string |
|
30 | { |
||
31 | 26 | $filePath = self::getFilePathForCurrentTheme(self::getTheme(), $filePath); |
|
32 | |||
33 | // check if the file exists |
||
34 | 26 | if (!is_file(PATH_WWW . str_replace(PATH_WWW, '', $filePath))) { |
|
35 | throw new Exception('The file (' . $filePath . ') does not exist.'); |
||
36 | } |
||
37 | |||
38 | 26 | return $filePath; |
|
39 | } |
||
40 | |||
41 | 26 | private static function getFilePathForCurrentTheme(string $theme, string $filePath): string |
|
42 | { |
||
43 | // just return the file if the theme is already in the file path |
||
44 | 26 | if (mb_strpos($filePath, 'src/Frontend/Themes/' . $theme) !== false) { |
|
45 | return $filePath; |
||
46 | } |
||
47 | |||
48 | // add theme location |
||
49 | 26 | $themeTemplate = str_replace('src/Frontend/', 'src/Frontend/Themes/' . $theme . '/', $filePath); |
|
50 | |||
51 | // check if this template exists |
||
52 | 26 | if (is_file(PATH_WWW . str_replace(PATH_WWW, '', $themeTemplate))) { |
|
53 | 26 | return $themeTemplate; |
|
54 | } |
||
55 | |||
56 | 26 | return $filePath; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Gets the active theme name |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | 26 | public static function getTheme(): string |
|
65 | { |
||
66 | // theme name has not yet been saved, fetch and save it |
||
67 | 26 | if (!self::$theme) { |
|
68 | 26 | self::$theme = Model::get('fork.settings')->get('Core', 'theme', null); |
|
69 | } |
||
70 | |||
71 | // return theme name |
||
72 | 26 | return self::$theme; |
|
73 | } |
||
74 | } |
||
75 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: