1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW\Core; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class to define options for BFW Core |
7
|
|
|
*/ |
8
|
|
|
class Options extends \BFW\Options |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Search the root and vendor paths if they are not declared. |
12
|
|
|
* |
13
|
|
|
* @return $this |
14
|
|
|
*/ |
15
|
|
|
public function searchPaths() |
16
|
|
|
{ |
17
|
|
|
//Search root directory if is not declared |
18
|
|
|
if ($this->options['rootDir'] === null) { |
19
|
|
|
$this->options['rootDir'] = $this->searchRootDir(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
//Search the vendor directory if is not declared |
23
|
|
|
if ($this->options['vendorDir'] === null) { |
24
|
|
|
$this->options['vendorDir'] = $this->searchVendorDir(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Check root and vendor path declared or found. |
32
|
|
|
* |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function checkPaths() |
36
|
|
|
{ |
37
|
|
View Code Duplication |
if (empty($this->options['rootDir'])) { |
|
|
|
|
38
|
|
|
$this->options['rootDir'] .= '/'; |
39
|
|
|
} else { |
40
|
|
|
$rootDirPosLastChar = strlen($this->options['rootDir']) - 1; |
41
|
|
|
|
42
|
|
|
if ($this->options['rootDir'][$rootDirPosLastChar] !== '/') { |
43
|
|
|
$this->options['rootDir'] .= '/'; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
if (empty($this->options['vendorDir'])) { |
|
|
|
|
48
|
|
|
$this->options['vendorDir'] .= '/'; |
49
|
|
|
} else { |
50
|
|
|
$vendorDirPosLastChar = strlen($this->options['vendorDir']) - 1; |
51
|
|
|
|
52
|
|
|
if ($this->options['vendorDir'][$vendorDirPosLastChar] !== '/') { |
53
|
|
|
$this->options['vendorDir'] .= '/'; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Find the vendor directory from the path of this file |
62
|
|
|
* (In theory we are into) |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
protected function searchVendorDir() |
67
|
|
|
{ |
68
|
|
|
if (PHP_VERSION_ID >= 70000) { |
69
|
|
|
return dirname(__FILE__, 5).'/'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$rootDir = __FILE__; |
73
|
|
|
for ($i = 1; $i <= 5; $i++) { |
74
|
|
|
$rootDir = dirname($rootDir); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $rootDir.'/'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Find the root directory from the vendor directory |
82
|
|
|
* In theory of the vendor directory is on the root directory. |
83
|
|
|
* If not, the user can define path for vendor and root directory. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function searchRootDir() |
88
|
|
|
{ |
89
|
|
|
return dirname($this->searchVendorDir()).'/'; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.