Passed
Push — stable ( c117f7...5e5803 )
by Nuno
02:33
created

Container::path()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 1
nop 1
crap 2
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 path to the application "app" directory.
57
     *
58
     * @param  string  $path Optionally, a path to append to the app path
59
     * @return string
60
     */
61 57
    public function path($path = '')
62
    {
63 57
        return $this->basePath('app'.($path ? DIRECTORY_SEPARATOR.$path : $path));
64
    }
65
66
    /**
67
     * Get the base path of the Laravel installation.
68
     *
69
     * @param  string $path
70
     *
71
     * @return string
72
     */
73 57
    public function basePath($path = '')
74
    {
75 57
        return BASE_PATH.($path ? DIRECTORY_SEPARATOR.$path : $path);
76
    }
77
78
    /**
79
     * Get the path to the application configuration files.
80
     *
81
     * @param  string $path
82
     *
83
     * @return string
84
     */
85 57
    public function configPath($path = '')
86
    {
87 57
        return BASE_PATH.DIRECTORY_SEPARATOR.'config'.($path ? DIRECTORY_SEPARATOR.$path : $path);
88
    }
89
90
    /**
91
     * Get the path to the database directory.
92
     *
93
     * @param  string $path
94
     *
95
     * @return string
96
     */
97 57
    public function databasePath($path = '')
98
    {
99 57
        return (BASE_PATH.DIRECTORY_SEPARATOR.'database').($path ? DIRECTORY_SEPARATOR.$path : $path);
100
    }
101
102
    /**
103
     * Get the path to the language files.
104
     *
105
     * @return string
106
     */
107 57
    public function langPath()
108
    {
109 57
        return $this->resourcePath('lang');
110
    }
111
112
    /**
113
     * Get the path to the resources directory.
114
     *
115
     * @param  string $path
116
     *
117
     * @return string
118
     */
119 57
    public function resourcePath($path = '')
120
    {
121 57
        return BASE_PATH.DIRECTORY_SEPARATOR.'resources'.($path ? DIRECTORY_SEPARATOR.$path : $path);
122
    }
123
124
    /**
125
     * Get the path to the storage directory.
126
     *
127
     * @return string
128
     */
129 57
    public function storagePath()
130
    {
131 57
        return BASE_PATH.DIRECTORY_SEPARATOR.'storage';
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 57
    public function environment()
138
    {
139 57
        return config('app.production') ? 'production' : 'development';
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function runningInConsole()
146
    {
147 1
        return true;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 57
    public function getNamespace()
154
    {
155 57
        if (! is_null($this->namespace)) {
156 2
            return $this->namespace;
157
        }
158 57
        $composer = json_decode(file_get_contents(base_path('composer.json')), true);
159 57
        foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) {
160 57
            foreach ((array) $path as $pathChoice) {
161 57
                if (realpath(app_path()) == realpath(base_path().'/'.$pathChoice)) {
162 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...
163
                }
164
            }
165
        }
166
        throw new RuntimeException('Unable to detect application namespace.');
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172 1
    public function isDownForMaintenance()
173
    {
174 1
        return false;
175
    }
176
177
    /**
178
     * Get the path to the environment file directory.
179
     *
180
     * @return string
181
     */
182 2
    public function environmentPath()
183
    {
184 2
        return $this->environmentPath ?: $this->basePath();
185
    }
186
187
    /**
188
     * Set the directory for the environment file.
189
     *
190
     * @param  string $path
191
     * @return $this
192
     */
193 1
    public function useEnvironmentPath($path)
194
    {
195 1
        $this->environmentPath = $path;
196
197 1
        return $this;
198
    }
199
200
    /**
201
     * Set the environment file to be loaded during bootstrapping.
202
     *
203
     * @param  string $file
204
     * @return $this
205
     */
206 1
    public function loadEnvironmentFrom($file)
207
    {
208 1
        $this->environmentFile = $file;
209
210 1
        return $this;
211
    }
212
213
    /**
214
     * Get the environment file the application is using.
215
     *
216
     * @return string
217
     */
218 2
    public function environmentFile()
219
    {
220 2
        return $this->environmentFile ?: '.env';
221
    }
222
223
    /**
224
     * Get the fully qualified path to the environment file.
225
     *
226
     * @return string
227
     */
228 1
    public function environmentFilePath()
229
    {
230 1
        return $this->environmentPath().'/'.$this->environmentFile();
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236 1
    public function configurationIsCached()
237
    {
238 1
        return false;
239
    }
240
241
    /**
242
     * Throw an Console Exception with the given data unless the given condition is true.
243
     *
244
     * @param  int $code
245
     * @param  string $message
246
     * @param  array $headers
247
     * @return void
248
     *
249
     * @throws \Symfony\Component\Console\Exception\CommandNotFoundException
250
     * @throws \LaravelZero\Framework\Contracts\Exceptions\ConsoleException
251
     */
252 1
    public function abort($code, $message = '', array $headers = [])
253
    {
254 1
        if ($code == 404) {
255 1
            throw new CommandNotFoundException($message);
256
        }
257
258 1
        throw new ConsoleException($code, $message, $headers);
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264 1
    public function registerConfiguredProviders()
265
    {
266 1
        throw new NotImplementedException;
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272 1
    public function register($provider, $options = [], $force = false)
273
    {
274 1
        throw new NotImplementedException;
275
    }
276
277
    /**
278
     * {@inheritdoc}
279
     */
280 1
    public function registerDeferredProvider($provider, $service = null)
281
    {
282 1
        throw new NotImplementedException;
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     */
288 1
    public function boot()
289
    {
290 1
        throw new NotImplementedException;
291
    }
292
293
    /**
294
     * {@inheritdoc}
295
     */
296 1
    public function booting($callback)
297
    {
298 1
        throw new NotImplementedException;
299
    }
300
301
    /**
302
     * {@inheritdoc}
303
     */
304 1
    public function booted($callback)
305
    {
306 1
        throw new NotImplementedException;
307
    }
308
309
    /**
310
     * {@inheritdoc}
311
     */
312 1
    public function getCachedServicesPath()
313
    {
314 1
        throw new NotImplementedException;
315
    }
316
317
    /**
318
     * {@inheritdoc}
319
     */
320
    public function getCachedPackagesPath()
321
    {
322
        throw new NotImplementedException;
323
    }
324
325
    /**
326
     * Define a callback to be used to configure Monolog.
327
     *
328
     * @param  callable $callback
329
     *
330
     * @return $this
331
     */
332 1
    public function configureMonologUsing(callable $callback)
333
    {
334 1
        $this->monologConfigurator = $callback;
335
336 1
        return $this;
337
    }
338
339
    /**
340
     * Determine if the application has a custom Monolog configurator.
341
     *
342
     * @return bool
343
     */
344 1
    public function hasMonologConfigurator()
345
    {
346 1
        return ! is_null($this->monologConfigurator);
347
    }
348
349
    /**
350
     * Get the custom Monolog configurator for the application.
351
     *
352
     * @return callable
353
     */
354 1
    public function getMonologConfigurator()
355
    {
356 1
        return $this->monologConfigurator;
357
    }
358
}
359