Completed
Push — master ( 0e0f05...a73ca1 )
by Mahmoud
03:35
created

PortButler::getClassNamespaceFromFile()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 31
rs 5.3846
cc 8
eloc 22
nc 8
nop 1
1
<?php
2
3
namespace App\Port\Butler\Portals;
4
5
use App\Port\Butler\Exceptions\WrongConfigurationsException;
6
use Illuminate\Support\Facades\Config;
7
use Illuminate\Support\Facades\File;
8
9
/**
10
 * Class PortButler.
11
 *
12
 * NOTE: You can access this Class functions with the facade [ModuleConfig].
13
 *
14
 * @author Mahmoud Zalt <[email protected]>
15
 */
16
class PortButler
17
{
18
19
    /**
20
     * Get the containers namespace value from the containers config file
21
     *
22
     * @return  string
23
     */
24
    public function getContainersNamespace()
25
    {
26
        return Config::get('hello.containers.namespace');
27
    }
28
29
    /**
30
     * @return  mixed
31
     */
32
    public function getLoginWebPageName()
33
    {
34
        $loginPage = Config::get('hello.containers.login-page-name');
35
36
        if (is_null($loginPage)) {
37
            throw new WrongConfigurationsException();
38
        }
39
40
        return $loginPage;
41
    }
42
43
    /**
44
     * Get the containers names
45
     *
46
     * @return  array
47
     */
48
    public function getContainersNames()
49
    {
50
        $containersNames = [];
51
52
        foreach ($this->getContainersPaths() as $containersPath) {
53
            $containersNames[] = basename($containersPath);
54
        }
55
56
        return $containersNames;
57
    }
58
59
    /**
60
     * get containers directories paths
61
     *
62
     * @return  mixed
63
     */
64
    public function getContainersPaths()
65
    {
66
        $containersPaths = File::directories(app_path('Containers'));
67
68
        // TODO: preparing some code to implement the Ships concept to Porto
69
70
//        // Search for Ships "_SHIP" of Containers
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
//        foreach ($containersPaths as $containersPath) {
72
//
73
//            if (preg_match('/_SHIP/',$containersPath)){
74
//
75
//                // append the Ship directories to the Containers directory, since it will contain containers
76
//                $shipContainers = File::directories($containersPath);
77
//                $containersPaths = array_merge($containersPaths, $shipContainers);
78
//            }
79
//        }
80
81
        return $containersPaths;
82
    }
83
    /**
84
     * build the main service provider class namespace
85
     *
86
     * @param $containersNamespace
87
     * @param $containerName
88
     *
89
     * @return  string
90
     */
91
    public function buildMainServiceProvider($containersNamespace, $containerName)
92
    {
93
        if ($containerName != 'Port') {
94
            return $containersNamespace . "\\Containers\\" . $containerName . "\\Providers\\" . $containerName . "ServiceProvider";
95
        }
96
97
        return "App" . "\\Port" . "\\Provider\\Providers\\" . $containerName . "ServiceProvider";
98
    }
99
100
101
    /**
102
     * build and return an object
103
     *
104
     * @param $filePathName
105
     *
106
     * @return  mixed
107
     */
108
    public function getClassObjectFromFile($filePathName)
109
    {
110
        $classString = $this->getClassFullNameFromFile($filePathName);
111
112
        $object = new $classString;
113
114
        return $object;
115
    }
116
117
118
    /**
119
     * return the name and the namespace of a class from its file path
120
     * result example: (string) "I\Am\The\Namespace\Of\This\Class"
121
     *
122
     * @param $filePathName
123
     *
124
     * @return  string
125
     */
126
    public function getClassFullNameFromFile($filePathName)
127
    {
128
        return $this->getClassNamespaceFromFile($filePathName) . '\\' . $this->getClassNameFromFile($filePathName);
129
    }
130
131
    /**
132
     * get the class namespace using token
133
     *
134
     * @param $filePathName
135
     *
136
     * @return  null|string
137
     */
138
    protected function getClassNamespaceFromFile($filePathName)
139
    {
140
        $src = file_get_contents($filePathName);
141
142
        $tokens = token_get_all($src);
143
        $count = count($tokens);
144
        $i = 0;
145
        $namespace = '';
146
        $namespace_ok = false;
147
        while ($i < $count) {
148
            $token = $tokens[$i];
149
            if (is_array($token) && $token[0] === T_NAMESPACE) {
150
                // Found namespace declaration
151
                while (++$i < $count) {
152
                    if ($tokens[$i] === ';') {
153
                        $namespace_ok = true;
154
                        $namespace = trim($namespace);
155
                        break;
156
                    }
157
                    $namespace .= is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
158
                }
159
                break;
160
            }
161
            $i++;
162
        }
163
        if (!$namespace_ok) {
164
            return null;
165
        } else {
166
            return $namespace;
167
        }
168
    }
169
170
    /**
171
     * get the class name form using token
172
     *
173
     * @param $filePathName
174
     *
175
     * @return  mixed
176
     */
177
    protected function getClassNameFromFile($filePathName)
178
    {
179
        $php_code = file_get_contents($filePathName);
180
181
        $classes = array();
182
        $tokens = token_get_all($php_code);
183
        $count = count($tokens);
184
        for ($i = 2; $i < $count; $i++) {
185
            if ($tokens[$i - 2][0] == T_CLASS
186
                && $tokens[$i - 1][0] == T_WHITESPACE
187
                && $tokens[$i][0] == T_STRING
188
            ) {
189
190
                $class_name = $tokens[$i][1];
191
                $classes[] = $class_name;
192
            }
193
        }
194
195
        return $classes[0];
196
    }
197
198
199
}
200