Completed
Push — master ( 284085...99e334 )
by Fumio
02:01
created

Registrar   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 83.93%

Importance

Changes 0
Metric Value
dl 0
loc 141
ccs 47
cts 56
cp 0.8393
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
C register() 0 36 7
A loadFiles() 0 15 3
A loadConfigurationFiles() 0 8 2
A getConfigurationFiles() 0 13 3
A boot() 0 6 2
A registerPackage() 0 19 4
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 4
    public function register(Application $app, array $addons)
18
    {
19
        // prepare helper functions
20 4
        foreach ($addons as $addon) {
21 2
            $this->loadFiles($addon, $addon->config('addon.files', []));
22
        }
23
24 4
        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.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 2
                    $app['router']->aliasMiddleware($name, $middleware);
49
                }
50
            }
51
        }
52 4
    }
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
        }
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
        }
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
            }
106
        }
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
        }
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
        }
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
        }
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
        }
148 2
    }
149
}
150