Completed
Push — master ( 1ca3ad...78b3b4 )
by Fumio
02:09
created

Registrar::loadConfigurationFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 5
cp 0.8
crap 2.032
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Console\Application as Artisan;
7
use Symfony\Component\Finder\Finder;
8
9
class Registrar
10
{
11
    /**
12
     * register files.
13
     *
14
     * @param \Illuminate\Contracts\Foundation\Application $app
15
     * @param array $addons
16
     */
17 3
    public function register(Application $app, array $addons)
18
    {
19
        // prepare helper functions
20 3
        foreach ($addons as $addon) {
21 2
    	    $this->loadFiles($addon, $addon->config('addon.files', []));
22
    	}
23
24 3
        foreach ($addons as $addon) {
25
	        // load config
26 2
	        $this->loadConfigurationFiles($addon);
27
28
	        // regist service providers
29 2
	        $providers = $addon->config('addon.providers', []);
30 2
	        foreach ($providers as $provider) {
31 1
	            $app->register($provider);
32
	        }
33
34
	        // register commands
35 2
            $commands = $addon->config('addon.console.commands', []);
36 2
            if (!is_array($commands)) $commands = [$commands];
37 2
	        Artisan::starting(function ($artisan) use ($commands) {
38
	            $artisan->resolveCommands($commands);
39 2
	        });
40
41
	        // register middleware
42 2
	        $middlewares = $addon->config('addon.http.middleware', $addon->config('addon.http.middlewares', []));
43 2
            if (!is_array($middlewares)) $middlewares = [$middlewares];
44 2
	        foreach ($middlewares as $middleware) {
45
	            $app[HttpKernel::class]->pushMiddleware($middleware);
46
	        }
47
48
	        // register route middleware
49 2
	        $middlewares = $addon->config('addon.route_middleware', $addon->config('addon.http.route_middlewares', []));
50 2
            if (!is_array($middlewares)) $middlewares = [$middlewares];
51 2
	        foreach ($middlewares as $name => $middleware) {
52 2
	            $app['router']->middleware($name, $middleware);
53
	        }
54
        }
55 3
    }
56
57
    /**
58
     * load addon initial script files.
59
     *
60
     * @param \Jumilla\Addomnipot\Laravel\Addon $addon
61
     * @param array $files
62
     */
63 2
    protected function loadFiles(Addon $addon, array $files)
64
    {
65 2
        foreach ($files as $filename) {
66 1
            $path = $addon->path($filename);
67
68 1
            if (!file_exists($path)) {
69
                $message = "Warning: PHP Script '$path' is nothing.";
70
                info($message);
71
                echo $message;
72
                continue;
73
            }
74
75 1
            require_once $path;
76
        }
77 2
    }
78
79
    /**
80
     * Load the configuration items from all of the files.
81
     *
82
     * @param \Jumilla\Addomnipot\Laravel\Addon $addon
83
     */
84 2
    protected function loadConfigurationFiles(Addon $addon)
85
    {
86 2
    	$directoryPath = $addon->path($addon->config('addon.paths.config', 'config'));
87
88 2
        foreach ($this->getConfigurationFiles($directoryPath) as $group => $path) {
89
            $addon->config->set($group, require $path);
0 ignored issues
show
Bug introduced by
The property config cannot be accessed from this context as it is declared protected in class Jumilla\Addomnipot\Laravel\Addon.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
90
        }
91 2
    }
92
93
    /**
94
     * Get all of the configuration files for the directory.
95
     *
96
     * @param string $directoryPath
97
     *
98
     * @return array
99
     */
100 2
    protected function getConfigurationFiles($directoryPath)
101
    {
102 2
        $files = [];
103
104 2
        if (is_dir($directoryPath)) {
105 1
            foreach (Finder::create()->files()->in($directoryPath) as $file) {
106
                $group = basename($file->getRealPath(), '.php');
107
                $files[$group] = $file->getRealPath();
108
            }
109
        }
110
111 2
        return $files;
112
    }
113
114
    /**
115
     * boot addon.
116
     *
117
     * @param \Illuminate\Contracts\Foundation\Application $app
118
     * @param array $addons
119
     */
120 3
    public function boot(Application $app, array $addons)
121
    {
122 3
        foreach ($addons as $addon) {
123 2
	        $this->registerPackage($app, $addon);
124
	    }
125 3
    }
126
127
    /**
128
     * Register the package's component namespaces.
129
     *
130
     * @param \Illuminate\Contracts\Foundation\Application $app
131
     * @param \Jumilla\Addomnipot\Laravel\Addon $addon
132
     */
133 2
    protected function registerPackage(Application $app, Addon $addon)
134
    {
135 2
        $namespace = $addon->name();
136
137 2
        $lang = $addon->path($addon->config('addon.paths.lang', 'lang'));
138 2
        if (is_dir($lang)) {
139 1
            $app['translator']->addNamespace($namespace, $lang);
140
        }
141
142 2
        $view = $addon->path($addon->config('addon.paths.views', 'views'));
143 2
        if (is_dir($view)) {
144 1
            $app['view']->addNamespace($namespace, $view);
145
        }
146
147 2
        $spec = $addon->path($addon->config('addon.paths.specs', 'specs'));
148 2
        if (is_dir($spec)) {
149 1
            $app['specs']->addNamespace($namespace, $spec);
150
        }
151 2
    }
152
}
153