Passed
Push — stable ( bf53fe...29e605 )
by Nuno
06:22
created

Container::environmentPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelZero\Framework;
4
5
use RuntimeException;
6
use Illuminate\Container\Container as BaseContainer;
7
use LaravelZero\Framework\Exceptions\ConsoleException;
8
use LaravelZero\Framework\Exceptions\NotImplementedException;
9
use Symfony\Component\Console\Exception\CommandNotFoundException;
10
use Illuminate\Contracts\Foundation\Application as LaravelApplication;
11
12
/**
13
 * This is the Laravel Zero Framework container class.
14
 *
15
 * @author Nuno Maduro <[email protected]>
16
 */
17
class Container extends BaseContainer implements LaravelApplication
18
{
19
    /**
20
     * A custom callback used to configure Monolog.
21
     *
22
     * @var callable|null
23
     */
24
    protected $monologConfigurator;
25
26
    /**
27
     * The application namespace.
28
     *
29
     * @var string
30
     */
31
    protected $namespace;
32
33
    /**
34
     * The custom environment path defined by the developer.
35
     *
36
     * @var string
37
     */
38
    protected $environmentPath;
39
40
    /**
41
     * The environment file to load during bootstrapping.
42
     *
43
     * @var string
44
     */
45
    protected $environmentFile = '.env';
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 2
    public function version()
51
    {
52 2
        return config('app.version');
53
    }
54
55
    /**
56
     * Get the base path of the Laravel installation.
57
     *
58
     * @param  string $path
59
     *
60
     * @return string
61
     */
62 57
    public function basePath($path = '')
63
    {
64 57
        return BASE_PATH.($path ? DIRECTORY_SEPARATOR.$path : $path);
65
    }
66
67
    /**
68
     * Get the path to the application configuration files.
69
     *
70
     * @param  string $path
71
     *
72
     * @return string
73
     */
74 57
    public function configPath($path = '')
75
    {
76 57
        return BASE_PATH.DIRECTORY_SEPARATOR.'config'.($path ? DIRECTORY_SEPARATOR.$path : $path);
77
    }
78
79
    /**
80
     * Get the path to the database directory.
81
     *
82
     * @param  string $path
83
     *
84
     * @return string
85
     */
86 57
    public function databasePath($path = '')
87
    {
88 57
        return (BASE_PATH.DIRECTORY_SEPARATOR.'database').($path ? DIRECTORY_SEPARATOR.$path : $path);
89
    }
90
91
    /**
92
     * Get the path to the language files.
93
     *
94
     * @return string
95
     */
96 1
    public function langPath()
97
    {
98 1
        return $this->resourcePath('lang');
99
    }
100
101
    /**
102
     * Get the path to the resources directory.
103
     *
104
     * @param  string $path
105
     *
106
     * @return string
107
     */
108 3
    public function resourcePath($path = '')
109
    {
110 3
        return BASE_PATH.DIRECTORY_SEPARATOR.'resources'.($path ? DIRECTORY_SEPARATOR.$path : $path);
111
    }
112
113
    /**
114
     * Get the path to the storage directory.
115
     *
116
     * @return string
117
     */
118 57
    public function storagePath()
119
    {
120 57
        return BASE_PATH.DIRECTORY_SEPARATOR.'storage';
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 57
    public function environment()
127
    {
128 57
        return config('app.production') ? 'production' : 'development';
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 1
    public function runningInConsole()
135
    {
136 1
        return true;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 57
    public function getNamespace()
143
    {
144 57
        if (! is_null($this->namespace)) {
145 2
            return $this->namespace;
146
        }
147 57
        $composer = json_decode(file_get_contents(base_path('composer.json')), true);
148 57
        foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) {
149 57
            foreach ((array) $path as $pathChoice) {
150 57
                if (realpath(app_path()) == realpath(base_path().'/'.$pathChoice)) {
151 57
                    return $this->namespace = $namespace;
0 ignored issues
show
Documentation Bug introduced by
It seems like $namespace can also be of type integer. However, the property $namespace is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
152
                }
153
            }
154
        }
155
        throw new RuntimeException('Unable to detect application namespace.');
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 1
    public function isDownForMaintenance()
162
    {
163 1
        return false;
164
    }
165
166
    /**
167
     * Get the path to the environment file directory.
168
     *
169
     * @return string
170
     */
171 2
    public function environmentPath()
172
    {
173 2
        return $this->environmentPath ?: $this->basePath();
174
    }
175
176
    /**
177
     * Set the directory for the environment file.
178
     *
179
     * @param  string $path
180
     * @return $this
181
     */
182 1
    public function useEnvironmentPath($path)
183
    {
184 1
        $this->environmentPath = $path;
185
186 1
        return $this;
187
    }
188
189
    /**
190
     * Set the environment file to be loaded during bootstrapping.
191
     *
192
     * @param  string $file
193
     * @return $this
194
     */
195 1
    public function loadEnvironmentFrom($file)
196
    {
197 1
        $this->environmentFile = $file;
198
199 1
        return $this;
200
    }
201
202
    /**
203
     * Get the environment file the application is using.
204
     *
205
     * @return string
206
     */
207 2
    public function environmentFile()
208
    {
209 2
        return $this->environmentFile ?: '.env';
210
    }
211
212
    /**
213
     * Get the fully qualified path to the environment file.
214
     *
215
     * @return string
216
     */
217 1
    public function environmentFilePath()
218
    {
219 1
        return $this->environmentPath().'/'.$this->environmentFile();
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225 1
    public function configurationIsCached()
226
    {
227 1
        return false;
228
    }
229
230
    /**
231
     * Throw an Console Exception with the given data unless the given condition is true.
232
     *
233
     * @param  int $code
234
     * @param  string $message
235
     * @param  array $headers
236
     * @return void
237
     *
238
     * @throws \Symfony\Component\Console\Exception\CommandNotFoundException
239
     * @throws \LaravelZero\Framework\Contracts\Exceptions\ConsoleException
240
     */
241 1
    public function abort($code, $message = '', array $headers = [])
242
    {
243 1
        if ($code == 404) {
244 1
            throw new CommandNotFoundException($message);
245
        }
246
247 1
        throw new ConsoleException($code, $message, $headers);
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253 1
    public function registerConfiguredProviders()
254
    {
255 1
        throw new NotImplementedException;
256
    }
257
258
    /**
259
     * {@inheritdoc}
260
     */
261 1
    public function register($provider, $options = [], $force = false)
262
    {
263 1
        throw new NotImplementedException;
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269 1
    public function registerDeferredProvider($provider, $service = null)
270
    {
271 1
        throw new NotImplementedException;
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277 1
    public function boot()
278
    {
279 1
        throw new NotImplementedException;
280
    }
281
282
    /**
283
     * {@inheritdoc}
284
     */
285 1
    public function booting($callback)
286
    {
287 1
        throw new NotImplementedException;
288
    }
289
290
    /**
291
     * {@inheritdoc}
292
     */
293 1
    public function booted($callback)
294
    {
295 1
        throw new NotImplementedException;
296
    }
297
298
    /**
299
     * {@inheritdoc}
300
     */
301 1
    public function getCachedServicesPath()
302
    {
303 1
        throw new NotImplementedException;
304
    }
305
306
    /**
307
     * {@inheritdoc}
308
     */
309
    public function getCachedPackagesPath()
310
    {
311
        throw new NotImplementedException;
312
    }
313
314
    /**
315
     * Define a callback to be used to configure Monolog.
316
     *
317
     * @param  callable $callback
318
     *
319
     * @return $this
320
     */
321 1
    public function configureMonologUsing(callable $callback)
322
    {
323 1
        $this->monologConfigurator = $callback;
324
325 1
        return $this;
326
    }
327
328
    /**
329
     * Determine if the application has a custom Monolog configurator.
330
     *
331
     * @return bool
332
     */
333 1
    public function hasMonologConfigurator()
334
    {
335 1
        return ! is_null($this->monologConfigurator);
336
    }
337
338
    /**
339
     * Get the custom Monolog configurator for the application.
340
     *
341
     * @return callable
342
     */
343 1
    public function getMonologConfigurator()
344
    {
345 1
        return $this->monologConfigurator;
346
    }
347
}
348