Failed Conditions
Push — develop ( d778eb...6e6bc5 )
by Maxime
17:40 waited 57:29
created

UserBootstrapper::bootstrapRocketeerDependencies()   A

↳ Parent: UserBootstrapper

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 7
Code Lines 4

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
ccs 4
cts 6
cp 0.6667
cc 2
eloc 4
nc 2
nop 0
crap 2.1481
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 442
    public function bootstrapUserFiles()
28
    {
29 442
        if (!$this->bootstrapApp()) {
30 442
            $this->bootstrapStandaloneFiles();
31 442
        }
32
33 442
        $this->bootstrapUserCode();
34 442
    }
35
36
    /**
37
     * Bootstrap Rocketeer's dependencies.
38
     */
39 4
    public function bootstrapRocketeerDependencies()
40
    {
41 4
        $plugins = $this->paths->getRocketeerPath().DS.'vendor/autoload.php';
0 ignored issues
show
Documentation Bug introduced by Maxime Fabre
The method getRocketeerPath does not exist on object<Rocketeer\Service...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...
42 4
        if (file_exists($plugins)) {
43
            require_once $plugins;
44
        }
45 4
    }
46
47
    /**
48
     * Load any configured plugins.
49
     */
50 442
    public function bootstrapPlugins()
51
    {
52 442
        $plugins = (array) $this->config->get('plugins.loaded');
53 442
        $plugins = array_filter($plugins, 'class_exists');
54
55
        $this->events->onTag('plugins', function () use ($plugins) {
56 442
            foreach ($plugins as $plugin) {
57 1
                $this->container->addServiceProvider($plugin);
58 442
            }
59 442
        });
60 442
    }
61
62
    /**
63
     * @return string
64
     */
65 442
    public function getUserNamespace()
66
    {
67 442
        $name = $this->config->get('application_name');
68 442
        $name = Str::studly($name);
69
70 442
        return $name;
71
    }
72
73
    /**
74
     * Bootstrap a PSR4 folder in the user's directory.
75
     *
76
     * @return bool
77
     */
78 442
    protected function bootstrapApp()
79
    {
80 442
        $namespace = $this->getUserNamespace();
81
82
        // Load service provider
83 442
        $serviceProvider = $namespace.'\\'.$namespace.'ServiceProvider';
84 442
        $hasServiceProvider = class_exists($serviceProvider);
85 442
        if ($hasServiceProvider) {
86
            $plugins = (array) $this->config->get('plugins.loaded');
87
            $plugins[] = $serviceProvider;
88
            $this->config->set('plugins.loaded', $plugins);
89
        }
90
91 442
        return $hasServiceProvider;
92
    }
93
94
    /**
95
     * Bootstrap the user's standalone files.
96
     */
97 442
    protected function bootstrapStandaloneFiles()
98
    {
99 442
        $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\Service...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...
100
101
        // Build queue, placing tasks first, events after
102 442
        $queue = [];
103 442
        foreach ($files as $file) {
104 2
            $path = $this->files->getAdapter()->applyPathPrefix($file['path']);
105 2
            if (strpos($path, 'tasks') !== false) {
106 1
                array_unshift($queue, $path);
107 1
            } else {
108 2
                $queue[] = $path;
109
            }
110 442
        }
111
112
        // Include files
113 442
        foreach ($queue as $path) {
114 3
            include $path;
115 442
        }
116 442
    }
117
118
    /**
119
     * Register the user's tasks, events and roles.
120
     */
121 442
    public function bootstrapUserCode()
122
    {
123
        // Clean previously registered events
124 442
        $this->tasks->clearRegisteredEvents();
125 442
        $this->bootstrapPlugins();
126
127
        // Get the registered events
128 442
        $hooks = (array) $this->config->getContextually('hooks');
129 442
        $tasks = isset($hooks['tasks']) ? (array) $hooks['tasks'] : [];
130 442
        $roles = isset($hooks['roles']) ? (array) $hooks['roles'] : [];
131 442
        $events = isset($hooks['events']) ? (array) $hooks['events'] : [];
132
133
        // Bind tasks and commands
134 442
        foreach ($tasks as $name => $task) {
135
            try {
136 68
                $this->tasks->task($name, $task);
137 68
            } catch (TaskCompositionException $exception) {
138 1
                $this->tasks->command($name, $task);
139
            }
140 442
        }
141
142
        // Bind events
143 442
        $this->events->onTag('hooks', function () use ($events) {
144 442
            foreach ($events as $event => $tasks) {
145 442
                foreach ($tasks as $task => $listeners) {
146 68
                    $this->tasks->addTaskListeners($task, $event, $listeners, 0);
147 442
                }
148 442
            }
149 442
        });
150
151
        // Assign roles
152 442
        $this->roles->assignTasksRoles($roles);
153 442
    }
154
155
    /**
156
     * @return string[]
157
     */
158 442
    public function getProvided()
159
    {
160
        return [
161 442
            'bootstrapPlugins',
162 442
            'bootstrapRocketeerDependencies',
163 442
            'bootstrapUserCode',
164 442
            'bootstrapUserFiles',
165 442
            'getUserNamespace',
166 442
        ];
167
    }
168
}
169