Componentes::components()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Netflie\Componentes;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\View\Factory as ViewFactoryContract;
7
use Illuminate\Filesystem\Filesystem;
8
use Netflie\Componentes\Provider\ServiceProvider;
9
10
class Componentes
11
{
12
    /**
13
     * @var \Illuminate\Container\Container
14
     */
15
    protected $app;
16
17
    public function __construct(Filesystem $files, string $viewPath)
18
    {
19
        $cachePath = $viewPath . '/cache';
20
21
        $this->app = Container::getInstance();
22
        $this->app->files = $files;
23
        $this->app->config = [
24
            'view.paths' => [$viewPath, $cachePath],
25
            'view.compiled' => $cachePath,
26
        ];
27
28
        (new ServiceProvider($this->app))->register();
29
    }
30
31
    /**
32
     * Creates a new Componentes.
33
     *
34
     * @return static
35
     */
36
    public static function create(string $viewPath)
37
    {
38
        return new static(new Filesystem(), $viewPath);
39
    }
40
41
    /**
42
     * Register a class-based component alias directive.
43
     *
44
     * @param  string  $class
45
     * @param  string|null  $alias
46
     * @param  string  $prefix
47
     * @return void
48
     */
49
    public function component($class, $alias = null, $prefix = '')
50
    {
51
        $this->app['blade.compiler']->component($class, $alias, $prefix);
52
    }
53
54
    /**
55
     * Register an array of class-based components.
56
     *
57
     * @param  array  $components
58
     * @param  string  $prefix
59
     * @return void
60
     */
61
    public function components(array $components, $prefix = '')
62
    {
63
        $this->app['blade.compiler']->components($components, $prefix);
64
    }
65
66
    public function render(string $content)
67
    {
68
        $viewFactory = $this->app->make(ViewFactoryContract::class);
69
70
        $viewName = sha1($content);
71
        $viewPath = $this->app->config['view.compiled'] . '/' . $viewName . '.blade.php';
72
73
        $this->app->files->replace($viewPath, $content);
74
75
        $view = $viewFactory->make($viewName);
76
77
        return $view->render();
78
    }
79
}