Completed
Push — master ( d916f4...696ec4 )
by Nekrasov
02:00
created

BladeProvider::getCompiler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Arrilot\BitrixBlade;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\View\Factory;
7
8
class BladeProvider
9
{
10
    /**
11
     * Path to a folder view common view can be stored.
12
     *
13
     * @var string
14
     */
15
    protected static $baseViewPath;
16
17
    /**
18
     * Local path to blade cache storage.
19
     *
20
     * @var string
21
     */
22
    protected static $cachePath;
23
24
    /**
25
     * View factory.
26
     *
27
     * @var Factory
28
     */
29
    protected static $viewFactory;
30
31
    /**
32
     * Service container factory.
33
     *
34
     * @var Container
35
     */
36
    protected static $container;
37
38
    /**
39
     * Register blade engine in Bitrix.
40
     *
41
     * @param string $baseViewPath
42
     * @param string $cachePath
43
     */
44
    public static function register($baseViewPath = 'local/views', $cachePath = 'bitrix/cache/blade')
0 ignored issues
show
Coding Style introduced by
register uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
45
    {
46
        static::$baseViewPath = $_SERVER['DOCUMENT_ROOT'].'/'.$baseViewPath;
47
        static::$cachePath = $_SERVER['DOCUMENT_ROOT'].'/'.$cachePath;
48
49
        static::instantiateServiceContainer();
50
        static::instantiateViewFactory();
51
        static::registerBitrixDirectives();
52
53
        global $arCustomTemplateEngines;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
54
        $arCustomTemplateEngines['blade'] = [
55
            'templateExt' => ['blade'],
56
            'function'    => 'renderBladeTemplate',
57
        ];
58
    }
59
60
    /**
61
     * Get view factory.
62
     *
63
     * @return Factory
64
     */
65
    public static function getViewFactory()
66
    {
67
        return static::$viewFactory;
68
    }
69
70
    /**
71
     * @return BladeCompiler
72
     */
73
    public function getCompiler()
74
    {
75
        return static::$container['blade.compiler'];
76
    }
77
78
    /**
79
     * Update paths where blade tries to find additional views.
80
     *
81
     * @param string $templateDir
82
     */
83
    public static function updateViewPaths($templateDir)
0 ignored issues
show
Coding Style introduced by
updateViewPaths uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
84
    {
85
        $newPaths = [
86
            $_SERVER['DOCUMENT_ROOT'].$templateDir,
87
            static::$baseViewPath,
88
        ];
89
90
        $finder = Container::getInstance()->make('view.finder');
91
        $finder->setPaths($newPaths);
92
    }
93
94
    /**
95
     * Instantiate service container if it's not instantiated yet.
96
     */
97
    protected static function instantiateServiceContainer()
98
    {
99
        $container = Container::getInstance();
100
101
        if (!$container) {
102
            $container = new Container();
103
            Container::setInstance($container);
104
        }
105
106
        static::$container = $container;
107
    }
108
109
    /**
110
     * Instantiate view factory.
111
     */
112
    protected static function instantiateViewFactory()
113
    {
114
        static::createDirIfNotExist(static::$baseViewPath);
115
        static::createDirIfNotExist(static::$cachePath);
116
117
        $viewPaths = [
118
            static::$baseViewPath,
119
        ];
120
        $cache = static::$cachePath;
121
122
        $blade = new Blade($viewPaths, $cache, static::$container);
123
124
        static::$viewFactory = $blade->view();
125
        static::$viewFactory->addExtension('blade', 'blade');
126
    }
127
128
    /**
129
     * Create dir if it does not exist.
130
     *
131
     * @param string $path
132
     */
133
    protected static function createDirIfNotExist($path)
134
    {
135
        if (!file_exists($path)) {
136
            $mask = umask(0);
137
            mkdir($path, 0777, true);
138
            umask($mask);
139
        }
140
    }
141
142
    /**
143
     * Register bitrix directives.
144
     */
145
    protected static function registerBitrixDirectives()
146
    {
147
        $compiler = static::getCompiler();
148
        $compiler->directive('bxComponent', function ($expression) {
149
            $expression = rtrim($expression, ')');
150
            $expression = ltrim($expression, '(');
151
152
            return '<?php $APPLICATION->IncludeComponent('.$expression.'); ?>';
153
        });
154
    }
155
}
156