Failed Conditions
Push — develop ( b8268a...b3fdf9 )
by Maxime
19:19
created

UserBootstrapper::bootstrapUserCode()   C

Complexity

Conditions 8
Paths 24

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 33
Code Lines 17

Code Coverage

Tests 21
CRAP Score 8

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 33
ccs 21
cts 21
cp 1
rs 5.3846
cc 8
eloc 17
nc 24
nop 0
crap 8
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 */
12
13
namespace Rocketeer\Services\Bootstrapper\Modules;
14
15
use Illuminate\Support\Str;
16
use Rocketeer\Services\Builders\TaskCompositionException;
17
18
/**
19
 * Loads the user's .rocketeer folder and registers
20
 * his/her custom code.
21
 */
22
class UserBootstrapper extends AbstractBootstrapperModule
23
{
24
    /**
25
     * Bootstrap the user's code.
26
     */
27 440
    public function bootstrapUserFiles()
28
    {
29 440
        if (!$this->bootstrapApp()) {
30 440
            $this->bootstrapStandaloneFiles();
31 440
        }
32
33 440
        $this->bootstrapUserCode();
34 440
    }
35
36
    /**
37
     * Load any configured plugins.
38
     */
39 440
    public function bootstrapPlugins()
40
    {
41 440
        $plugins = (array) $this->config->get('plugins.loaded');
42 440
        $plugins = array_filter($plugins, 'class_exists');
43
44
        $this->events->onTag('plugins', function () use ($plugins) {
45 440
            foreach ($plugins as $plugin) {
46 1
                $this->container->addServiceProvider($plugin);
47 440
            }
48 440
        });
49 440
    }
50
51
    /**
52
     * @return string
53
     */
54 440
    public function getUserNamespace()
55 1
    {
56 440
        $name = $this->config->get('application_name');
57 440
        $name = Str::studly($name);
58
59 440
        return $name;
60
    }
61
62
    /**
63
     * Bootstrap a PSR4 folder in the user's directory.
64
     *
65
     * @return bool
66
     */
67 440
    protected function bootstrapApp()
68
    {
69 440
        $namespace = $this->getUserNamespace();
70
71
        // Load service provider
72 440
        $serviceProvider = $namespace.'\\'.$namespace.'ServiceProvider';
73 440
        $hasServiceProvider = class_exists($serviceProvider);
74 440
        if ($hasServiceProvider) {
75
            $plugins = (array) $this->config->get('plugins.loaded');
76
            $plugins[] = $serviceProvider;
77
            $this->config->set('plugins.loaded', $plugins);
78
        }
79
80 440
        return $hasServiceProvider;
81
    }
82
83
    /**
84
     * Bootstrap the user's standalone files.
85
     */
86 440
    protected function bootstrapStandaloneFiles()
87
    {
88 440
        $files = $this->files->listFiles($this->paths->getUserlandPath(), true);
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method getUserlandPath does not exist on object<Rocketeer\Services\Environment\Pathfinder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
89
90
        // Build queue, placing tasks first, events after
91 440
        $queue = [];
92 440
        foreach ($files as $file) {
93 2
            $path = $this->files->getAdapter()->applyPathPrefix($file['path']);
94 2
            if (strpos($path, 'tasks') !== false) {
95 1
                array_unshift($queue, $path);
96 1
            } else {
97 2
                $queue[] = $path;
98
            }
99 440
        }
100
101
        // Include files
102 440
        foreach ($queue as $path) {
103 2
            include $path;
104 440
        }
105 440
    }
106
107
    /**
108
     * Register the user's tasks, events and roles.
109
     */
110 440
    public function bootstrapUserCode()
111
    {
112
        // Clean previously registered events
113 440
        $this->tasks->clearRegisteredEvents();
114 440
        $this->bootstrapPlugins();
115
116
        // Get the registered events
117 440
        $hooks = (array) $this->config->getContextually('hooks');
118 440
        $tasks = isset($hooks['tasks']) ? (array) $hooks['tasks'] : [];
119 440
        $roles = isset($hooks['roles']) ? (array) $hooks['roles'] : [];
120 440
        $events = isset($hooks['events']) ? (array) $hooks['events'] : [];
121
122
        // Bind tasks and commands
123 440
        foreach ($tasks as $name => $task) {
124
            try {
125 70
                $this->tasks->task($name, $task);
126 70
            } catch (TaskCompositionException $exception) {
127 1
                $this->tasks->command($name, $task);
128
            }
129 440
        }
130
131
        // Bind events
132 440
        $this->events->onTag('hooks', function () use ($events) {
133 440
            foreach ($events as $event => $tasks) {
134 440
                foreach ($tasks as $task => $listeners) {
135 70
                    $this->tasks->addTaskListeners($task, $event, $listeners, 0);
136 440
                }
137 440
            }
138 440
        });
139
140
        // Assign roles
141 440
        $this->roles->assignTasksRoles($roles);
142 440
    }
143
144
    /**
145
     * @return string[]
146
     */
147 440
    public function getProvided()
148
    {
149
        return [
150 440
            'bootstrapUserFiles',
151 440
            'bootstrapPlugins',
152 440
            'bootstrapUserCode',
153 440
            'getUserNamespace',
154 440
        ];
155
    }
156
}
157