Completed
Push — master ( 4ee28c...27c466 )
by Marcel
34:36 queued 25:34
created

src/Checks/PhpExtensionsAreInstalled.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace BeyondCode\SelfDiagnosis\Checks;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
9
class PhpExtensionsAreInstalled implements Check
10
{
11
12
    const EXT = 'ext-';
13
14
    /** @var Filesystem */
15
    private $filesystem;
16
17
    public function __construct(Filesystem $filesystem)
18
    {
19
        $this->filesystem = $filesystem;
20
    }
21
22
    /** @var Collection */
23
    private $extensions;
24
25
    /**
26
     * The name of the check.
27
     *
28
     * @param array $config
29
     * @return string
30
     */
31
    public function name(array $config): string
32
    {
33
        return trans('self-diagnosis::checks.php_extensions_are_installed.name');
34
    }
35
36
    /**
37
     * The error message to display in case the check does not pass.
38
     *
39
     * @param array $config
40
     * @return string
41
     */
42
    public function message(array $config): string
43
    {
44
        return trans('self-diagnosis::checks.php_extensions_are_installed.message', [
45
            'extensions' => $this->extensions->implode(PHP_EOL),
46
        ]);
47
    }
48
49
    /**
50
     * Perform the actual verification of this check.
51
     *
52
     * @param array $config
53
     * @return bool
54
     */
55
    public function check(array $config): bool
56
    {
57
        $this->extensions = Collection::make(Arr::get($config, 'extensions', []));
58
        if (Arr::get($config, 'include_composer_extensions', false)) {
59
            $this->extensions = $this->extensions->merge($this->getExtensionsRequiredInComposerFile());
60
            $this->extensions = $this->extensions->unique();
61
        }
62
        $this->extensions = $this->extensions->reject(function ($ext) {
63
            return extension_loaded($ext);
64
        });
65
66
        return $this->extensions->isEmpty();
67
    }
68
69
    /**
70
     * @return array
71
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
72
     */
73
    public function getExtensionsRequiredInComposerFile()
74
    {
75
        $installedPackages = json_decode($this->filesystem->get(base_path('vendor/composer/installed.json')), true);
76
77
        $extensions = [];
78
        foreach ($installedPackages as $installedPackage) {
79
            $filtered = Arr::where(array_keys(Arr::get($installedPackage, 'require', [])), function ($value, $key) {
80
                return starts_with($value, self::EXT);
0 ignored issues
show
Deprecated Code introduced by
The function starts_with() has been deprecated with message: Str::startsWith() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
81
            });
82
            foreach ($filtered as $extension) {
83
                $extensions[] = str_replace_first(self::EXT, '', $extension);
0 ignored issues
show
Deprecated Code introduced by
The function str_replace_first() has been deprecated with message: Str::replaceFirst() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
84
            }
85
        }
86
        return array_unique($extensions);
87
    }
88
89
}
90