Completed
Push — master ( dc322e...438d0e )
by Mahmoud
03:27
created

ProvidersLoaderTrait::loadProviders()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 9
nc 5
nop 1
1
<?php
2
3
namespace App\Ship\Engine\Loaders;
4
5
use App;
6
use App\Ship\Engine\Butlers\Facades\ShipButler;
7
use App\Ship\Engine\Butlers\Facades\LoaderButler;
8
use File;
9
10
/**
11
 * Class ProvidersLoaderTrait.
12
 *
13
 * @author  Mahmoud Zalt <[email protected]>
14
 */
15
trait ProvidersLoaderTrait
16
{
17
18
    /**
19
     * @param $containerName
20
     */
21
    public function loadProvidersFromContainers($containerName)
22
    {
23
        $containerProvidersDirectory = base_path('app/Containers/' . $containerName . '/Providers');
24
25
        $this->loadProviders($containerProvidersDirectory);
26
    }
27
28
    /**
29
     * loadProvidersFromShip
30
     */
31
    public function loadProvidersFromShip()
32
    {
33
        foreach ($this->serviceProviders as $providerClass) {
0 ignored issues
show
Bug introduced by
The property serviceProviders does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
            $this->loadProvider($providerClass);
35
        }
36
    }
37
38
    /**
39
     * @param $directory
40
     */
41
    private function loadProviders($directory)
42
    {
43
        if (File::isDirectory($directory)) {
44
45
            $files = File::allFiles($directory);
46
47
            $mainServiceProviderNameStartWith = 'Main';
48
49
            foreach ($files as $file) {
50
51
                if (File::isFile($file)) {
52
53
                    // Check if this is the Main Service Provider
54
                    if (ShipButler::stringStartsWith($file->getFilename(), $mainServiceProviderNameStartWith)) {
55
56
                        $serviceProviderClass = LoaderButler::getClassFullNameFromFile($file->getPathname());
57
58
                        $this->loadProvider($serviceProviderClass);
59
60
                    }
61
                }
62
            }
63
        }
64
    }
65
66
    /*
67
     * loadProvider
68
     */
69
    private function loadProvider($provider)
70
    {
71
        App::register($provider);
72
    }
73
74
    /**
75
     * loadContainersInternalProviders
76
     */
77
    public function loadContainersInternalProviders()
78
    {
79
        foreach ($this->containerServiceProviders as $provider) {
0 ignored issues
show
Bug introduced by
The property containerServiceProviders does not seem to exist. Did you mean serviceProviders?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
80
            $this->loadProvider($provider);
81
        }
82
    }
83
84
}
85