Issues (281)

Branch: master

src/Frontend/Core/Engine/Theme.php (1 issue)

1
<?php
2
3
namespace Frontend\Core\Engine;
4
5
use Exception;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Frontend\Core\Engine\Exception. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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