Issues (18)

src/Settings/Compiler.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Settings;
15
16
use Illuminate\Contracts\View\Factory as ViewFactory;
17
use Illuminate\Filesystem\Filesystem;
18
use Illuminate\Support\Facades\File;
19
use Riimu\Kit\PHPEncoder\PHPEncoder;
20
21
/**
22
 * @internal
23
 */
24
final class Compiler
25
{
26
    /**
27
     * Array of opening and closing tags for raw echos.
28
     *
29
     * @var string[]
30
     */
31
    private static $rawTags = ['{!!', '!!}'];
32
33
    /**
34
     * @var \Illuminate\Contracts\View\Factory
35
     */
36
    private $viewFactory;
37
38
    /**
39
     * @var \Illuminate\Filesystem\Filesystem
40
     */
41
    private $files;
42
43
    /**
44
     * @var \Riimu\Kit\PHPEncoder\PHPEncoder
45
     */
46
    private $encoder;
47
48
    /**
49
     * Compiler constructor.
50
     *
51
     * @param \Illuminate\Contracts\View\Factory $viewFactory
52
     * @param \Illuminate\Filesystem\Filesystem $files
53
     * @param \Riimu\Kit\PHPEncoder\PHPEncoder $encoder
54
     *
55
     * @return void
56
     */
57 13
    public function __construct(ViewFactory $viewFactory, Filesystem $files, PHPEncoder $encoder)
58
    {
59 13
        $this->viewFactory = $viewFactory;
60 13
        $this->files = $files;
61 13
        $this->encoder = $encoder;
62 13
    }
63
64
    /**
65
     * Compiles the provided settings into the provided path.
66
     *
67
     * @param \Algolia\ScoutExtended\Settings\Settings $settings
68
     * @param string $path
69
     *
70
     * @return void
71
     */
72 6
    public function compile(Settings $settings, string $path): void
73
    {
74 6
        $viewVariables = self::getViewVariables();
75
76 6
        $viewParams = [];
77 6
        $all = $settings->all();
78
79 6
        foreach ($viewVariables as $viewVariable) {
80 6
            if (array_key_exists($viewVariable, $all)) {
81 6
                $viewParams[$viewVariable] = $this->encoder->encode($all[$viewVariable], ['array.base' => 4]);
82
            }
83
        }
84
85 6
        $indexChangedSettings = [];
86 6
        foreach ($settings->changed() as $setting => $value) {
87 6
            if (! array_key_exists($setting, $viewParams)) {
88 6
                $indexChangedSettings[$setting] = $value;
89
            }
90
        }
91
92 6
        $viewParams['__indexChangedSettings'] = $this->encoder->encode($indexChangedSettings, ['array.base' => 0]);
93
94 6
        if (empty($indexChangedSettings)) {
95 4
            $viewParams['__indexChangedSettings'] = ']';
96
        } else {
97 2
            $viewParams['__indexChangedSettings'] = preg_replace('/^.+\n/', '', $viewParams['__indexChangedSettings']);
98
        }
99
100 6
        $this->files->put($path, '<?php
101
102 6
'.$this->viewFactory->make('algolia::config', $viewParams)->render());
0 ignored issues
show
Are you sure $this->viewFactory->make... $viewParams)->render() of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
'./** @scrutinizer ignore-type */ $this->viewFactory->make('algolia::config', $viewParams)->render());
Loading history...
103 6
    }
104
105
    /**
106
     * Returns the view variables.
107
     *
108
     * @return array
109
     */
110 12
    public static function getViewVariables(): array
111
    {
112 12
        $contents = File::get(__DIR__.'/../../resources/views/config.blade.php');
113 12
        $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', self::$rawTags[0], self::$rawTags[1]);
114 12
        preg_match_all($pattern, $contents, $matches);
115
116 12
        array_pop($matches[2]);
117
118
        return array_map(function ($match) {
119 12
            return ltrim(explode(' ', $match)[0], '$');
120 12
        }, $matches[2]);
121
    }
122
}
123