|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use League\Flysystem\Adapter\Local; |
|
7
|
|
|
use League\Flysystem\Filesystem; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
|
|
10
|
|
|
class Composer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Filesystem |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $filesystem; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $namespace; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $path; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(Filesystem $filesystem = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->filesystem = $filesystem ? $filesystem : new Filesystem(new Local(getcwd())); |
|
30
|
|
|
|
|
31
|
|
|
$this->load(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function load() |
|
35
|
|
|
{ |
|
36
|
|
|
$composer = json_decode($this->filesystem->get('composer.json')->read(), true); |
|
37
|
|
|
if(!isset($composer['autoload']['psr-4']) || empty($composer['autoload']['psr-4'])) throw new RuntimeException('Unable to detect namespace. Ensure you have PSR4 autoloading setup in your composer.json'); |
|
38
|
|
|
|
|
39
|
|
|
$namespace = array_keys($composer['autoload']['psr-4'])[0]; |
|
40
|
|
|
$path = $composer['autoload']['psr-4'][$namespace]; |
|
41
|
|
|
|
|
42
|
|
|
$this->namespace = Utils::removeTrailingSlashes($namespace); |
|
43
|
|
|
$this->path = Utils::removeTrailingSlashes($path); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getRootNamespace() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->namespace; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getRootPath() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->path; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getClassNamespace($name) |
|
57
|
|
|
{ |
|
58
|
|
|
$name = Utils::switchToNamespaceSlashes($name); |
|
59
|
|
|
|
|
60
|
|
|
if(Str::startsWith($name, $this->getRootNamespace())) |
|
61
|
|
|
{ |
|
62
|
|
|
return $name; |
|
63
|
|
|
} |
|
64
|
|
|
return $this->getRootNamespace() . '\\' . $name; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getClassPath($name) |
|
68
|
|
|
{ |
|
69
|
|
|
$name = $this->getClassNamespace($name); |
|
70
|
|
|
$name = Str::substr($name, strlen($this->getRootNamespace()) + 1); |
|
71
|
|
|
|
|
72
|
|
|
return $this->getRootPath() . DIRECTORY_SEPARATOR . Utils::switchToDirectorySlashes($name) . '.php'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getTestPath($name) |
|
76
|
|
|
{ |
|
77
|
|
|
$name = $this->getClassNamespace($name); |
|
78
|
|
|
$name = Str::substr($name, strlen($this->getRootNamespace()) + 1); |
|
79
|
|
|
|
|
80
|
|
|
$switchToDirectorySlashes = Utils::switchToDirectorySlashes($name); |
|
81
|
|
|
|
|
82
|
|
|
return 'tests' . DIRECTORY_SEPARATOR . str_replace(DIRECTORY_SEPARATOR, "", $switchToDirectorySlashes) . 'Test.php'; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|