Completed
Push — master ( 0af372...46d965 )
by Fumio
03:04
created

Registrar::register()   C

Complexity

Conditions 7
Paths 26

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7.4428

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 26
nop 2
dl 0
loc 36
ccs 19
cts 24
cp 0.7917
crap 7.4428
rs 6.7272
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 3
        }
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 2
            }
33
34
            // register commands
35 2
            $commands = $addon->config('addon.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 named middleware
42 2
	        $middlewares = $addon->config('addon.middleware', $addon->config('addon.http.route_middlewares', []));
43 2
	        foreach ($middlewares as $name => $middleware) {
44
                if (is_array($middleware)) {
45
    	            $app['router']->middlewareGroup($name, $middleware);
46
                }
47
                else {
48
    	            $app['router']->aliasMiddleware($name, $middleware);
49
                }
50 2
	        }
51 3
        }
52 3
    }
53
54
    /**
55
     * load addon initial script files.
56
     *
57
     * @param \Jumilla\Addomnipot\Laravel\Addon $addon
58
     * @param array $files
59
     */
60 2
    protected function loadFiles(Addon $addon, array $files)
61
    {
62 2
        foreach ($files as $filename) {
63 1
            $path = $addon->path($filename);
64
65 1
            if (!file_exists($path)) {
66
                $message = "Warning: PHP Script '$path' is nothing.";
67
                info($message);
68
                echo $message;
69
                continue;
70
            }
71
72 1
            require_once $path;
73 2
        }
74 2
    }
75
76
    /**
77
     * Load the configuration items from all of the files.
78
     *
79
     * @param \Jumilla\Addomnipot\Laravel\Addon $addon
80
     */
81 2
    protected function loadConfigurationFiles(Addon $addon)
82
    {
83 2
        $directoryPath = $addon->path($addon->config('addon.paths.config', 'config'));
84
85 2
        foreach ($this->getConfigurationFiles($directoryPath) as $group => $path) {
86
            $addon->setConfig($group, require $path);
87 2
        }
88 2
    }
89
90
    /**
91
     * Get all of the configuration files for the directory.
92
     *
93
     * @param string $directoryPath
94
     *
95
     * @return array
96
     */
97 2
    protected function getConfigurationFiles($directoryPath)
98
    {
99 2
        $files = [];
100
101 2
        if (is_dir($directoryPath)) {
102 1
            foreach (Finder::create()->files()->in($directoryPath) as $file) {
103
                $group = basename($file->getRealPath(), '.php');
104
                $files[$group] = $file->getRealPath();
105 1
            }
106 1
        }
107
108 2
        return $files;
109
    }
110
111
    /**
112
     * boot addon.
113
     *
114
     * @param \Illuminate\Contracts\Foundation\Application $app
115
     * @param array $addons
116
     */
117 3
    public function boot(Application $app, array $addons)
118
    {
119 3
        foreach ($addons as $addon) {
120 2
            $this->registerPackage($app, $addon);
121 3
        }
122 3
    }
123
124
    /**
125
     * Register the package's component namespaces.
126
     *
127
     * @param \Illuminate\Contracts\Foundation\Application $app
128
     * @param \Jumilla\Addomnipot\Laravel\Addon $addon
129
     */
130 2
    protected function registerPackage(Application $app, Addon $addon)
131
    {
132 2
        $namespace = $addon->name();
133
134 2
        $lang = $addon->path($addon->config('addon.paths.lang', 'lang'));
135 2
        if (is_dir($lang)) {
136 1
            $app['translator']->addNamespace($namespace, $lang);
137 1
        }
138
139 2
        $view = $addon->path($addon->config('addon.paths.views', 'views'));
140 2
        if (is_dir($view)) {
141 1
            $app['view']->addNamespace($namespace, $view);
142 1
        }
143
144 2
        $spec = $addon->path($addon->config('addon.paths.specs', 'specs'));
145 2
        if (is_dir($spec)) {
146 1
            $app['specs']->addNamespace($namespace, $spec);
147 1
        }
148 2
    }
149
}
150