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