1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory; |
6
|
|
|
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\CreatorInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
12
|
|
|
|
13
|
|
|
class FileFactory |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var NamespaceHelper |
17
|
|
|
*/ |
18
|
|
|
protected $namespaceHelper; |
19
|
|
|
/** |
20
|
|
|
* @var Config |
21
|
|
|
*/ |
22
|
|
|
protected $config; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $projectRootNamespace; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $projectRootDirectory; |
33
|
|
|
|
34
|
|
|
public function __construct(NamespaceHelper $namespaceHelper, Config $config) |
35
|
|
|
{ |
36
|
|
|
$this->namespaceHelper = $namespaceHelper; |
37
|
|
|
$this->projectRootNamespace = $this->namespaceHelper->getProjectRootNamespaceFromComposerJson(); |
38
|
|
|
$this->projectRootDirectory = $config::getProjectRootDirectory(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setProjectRootNamespace(string $projectRootNamespace): self |
42
|
|
|
{ |
43
|
|
|
$this->projectRootNamespace = $projectRootNamespace; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new file object from an existing file path |
50
|
|
|
* |
51
|
|
|
* @param string $path |
52
|
|
|
* |
53
|
|
|
* @return File |
54
|
|
|
* @throws DoctrineStaticMetaException |
55
|
|
|
*/ |
56
|
|
|
public function createFromExistingPath(string $path): File |
57
|
|
|
{ |
58
|
|
|
$file = new File($path); |
59
|
|
|
if (false === $file->exists()) { |
60
|
|
|
throw new DoctrineStaticMetaException('File does not exist at ' . $path); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $file; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create a new file object that should not already exist at the specified path |
68
|
|
|
* |
69
|
|
|
* @param string $path |
70
|
|
|
* |
71
|
|
|
* @return File |
72
|
|
|
* @throws DoctrineStaticMetaException |
73
|
|
|
*/ |
74
|
|
|
public function createFromNonExistantPath(string $path): File |
75
|
|
|
{ |
76
|
|
|
$file = new File($path); |
77
|
|
|
if (true === $file->exists()) { |
78
|
|
|
throw new DoctrineStaticMetaException('File exists at ' . $path); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $file; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create a new file object from a fully qualified name, may or may not exist |
86
|
|
|
* |
87
|
|
|
* @param string $fqn |
88
|
|
|
* @param string $srcOrTestSubFolder |
89
|
|
|
* |
90
|
|
|
* @return File |
91
|
|
|
* @throws DoctrineStaticMetaException |
92
|
|
|
*/ |
93
|
|
|
public function createFromFqn(string $fqn, $srcOrTestSubFolder = CreatorInterface::SRC_FOLDER): File |
94
|
|
|
{ |
95
|
|
|
list($className, , $subDirectories) = $this->namespaceHelper->parseFullyQualifiedName( |
96
|
|
|
$fqn, |
97
|
|
|
$srcOrTestSubFolder, |
98
|
|
|
$this->projectRootNamespace |
99
|
|
|
); |
100
|
|
|
$path = $this->projectRootDirectory |
101
|
|
|
. '/' . implode('/', $subDirectories) . '/' . $className . '.php'; |
102
|
|
|
|
103
|
|
|
return new File($path); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $projectRootDirectory |
108
|
|
|
* |
109
|
|
|
* @return FileFactory |
110
|
|
|
*/ |
111
|
|
|
public function setProjectRootDirectory(string $projectRootDirectory): FileFactory |
112
|
|
|
{ |
113
|
|
|
$this->projectRootDirectory = $projectRootDirectory; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|