@@ -31,7 +31,7 @@ class ProjectConfiguration implements Arrayable
3131 private $ configuration ;
3232
3333 /**
34- * The path to the configuration file.
34+ * The path to the Ymir project configuration file.
3535 *
3636 * @var string
3737 */
@@ -47,11 +47,11 @@ class ProjectConfiguration implements Arrayable
4747 /**
4848 * Constructor.
4949 */
50- public function __construct (string $ configurationFilePath , Filesystem $ filesystem )
50+ public function __construct (Filesystem $ filesystem , string $ configurationFilePath = '' )
5151 {
52- $ this ->configurationFilePath = $ configurationFilePath ;
5352 $ this ->filesystem = $ filesystem ;
54- $ this ->configuration = $ this ->load ($ configurationFilePath );
53+
54+ $ this ->loadConfiguration ($ configurationFilePath );
5555 }
5656
5757 /**
@@ -115,8 +115,12 @@ public function createNew(Collection $project, array $environments, string $type
115115 */
116116 public function delete ()
117117 {
118+ if ($ this ->exists ()) {
119+ $ this ->filesystem ->remove ($ this ->configurationFilePath );
120+ }
121+
118122 $ this ->configuration = [];
119- $ this ->filesystem -> remove ( $ this -> configurationFilePath ) ;
123+ $ this ->configurationFilePath = '' ;
120124 }
121125
122126 /**
@@ -130,11 +134,11 @@ public function deleteEnvironment(string $environment)
130134 }
131135
132136 /**
133- * Checks if the project configuration file exists.
137+ * Checks if the Ymir project configuration file exists.
134138 */
135139 public function exists (): bool
136140 {
137- return $ this ->filesystem ->exists ($ this ->configurationFilePath );
141+ return ! empty ( $ this -> configurationFilePath ) && $ this ->filesystem ->exists ($ this ->configurationFilePath );
138142 }
139143
140144 /**
@@ -143,7 +147,7 @@ public function exists(): bool
143147 public function getEnvironment (string $ environment ): array
144148 {
145149 if (!$ this ->hasEnvironment ($ environment )) {
146- throw new InvalidArgumentException (sprintf ('Environment "%s" not found in ymir.yml file ' , $ environment ));
150+ throw new InvalidArgumentException (sprintf ('Environment "%s" not found in Ymir project configuration file ' , $ environment ));
147151 }
148152
149153 return (array ) $ this ->configuration ['environments ' ][$ environment ];
@@ -163,7 +167,7 @@ public function getEnvironments(): array
163167 public function getProjectId (): int
164168 {
165169 if (empty ($ this ->configuration ['id ' ])) {
166- throw new RuntimeException ('No "id" found in ymir.yml file ' );
170+ throw new RuntimeException ('No "id" found in Ymir project configuration file ' );
167171 }
168172
169173 return (int ) $ this ->configuration ['id ' ];
@@ -175,7 +179,7 @@ public function getProjectId(): int
175179 public function getProjectName (): string
176180 {
177181 if (empty ($ this ->configuration ['name ' ])) {
178- throw new RuntimeException ('No "name" found in ymir.yml file ' );
182+ throw new RuntimeException ('No "name" found in Ymir project configuration file ' );
179183 }
180184
181185 return (string ) $ this ->configuration ['name ' ];
@@ -187,7 +191,7 @@ public function getProjectName(): string
187191 public function getProjectType (): string
188192 {
189193 if (empty ($ this ->configuration ['type ' ])) {
190- throw new RuntimeException ('No "type" found in ymir.yml file ' );
194+ throw new RuntimeException ('No "type" found in Ymir project configuration file ' );
191195 }
192196
193197 return (string ) $ this ->configuration ['type ' ];
@@ -201,6 +205,25 @@ public function hasEnvironment(string $environment): bool
201205 return array_key_exists ($ environment , $ this ->configuration ['environments ' ]);
202206 }
203207
208+ /**
209+ * Load the given Ymir project configuration file.
210+ */
211+ public function loadConfiguration (string $ configurationFilePath )
212+ {
213+ $ configuration = [];
214+
215+ if ($ this ->filesystem ->exists ($ configurationFilePath )) {
216+ $ configuration = Yaml::parse ((string ) file_get_contents ($ configurationFilePath ));
217+ }
218+
219+ if (!empty ($ configuration ) && !is_array ($ configuration )) {
220+ throw new RuntimeException ('Error parsing Ymir project configuration file ' );
221+ }
222+
223+ $ this ->configuration = $ configuration ;
224+ $ this ->configurationFilePath = $ configurationFilePath ;
225+ }
226+
204227 /**
205228 * {@inheritdoc}
206229 */
@@ -215,41 +238,27 @@ public function toArray()
215238 public function validate ()
216239 {
217240 if (!$ this ->exists ()) {
218- throw new RuntimeException (sprintf ('No Ymir project found in the current directory . You can initialize one with the "%s" command. ' , InitializeProjectCommand::ALIAS ));
241+ throw new RuntimeException (sprintf ('No Ymir project configuration file found . You can create one by initializing a project with the "%s" command. ' , InitializeProjectCommand::ALIAS ));
219242 } elseif (empty ($ this ->configuration ['id ' ])) {
220- throw new RuntimeException ('The ymir.yml file must have an "id" ' );
243+ throw new RuntimeException ('The Ymir project configuration file must have an "id" ' );
221244 } elseif (empty ($ this ->configuration ['environments ' ])) {
222- throw new RuntimeException ('The ymir.yml file must have at least one environment ' );
245+ throw new RuntimeException ('The Ymir project configuration file must have at least one environment ' );
223246 } elseif (empty ($ this ->configuration ['type ' ])) {
224- throw new RuntimeException ('The ymir.yml file must have a "type" ' );
247+ throw new RuntimeException ('The Ymir project configuration file must have a "type" ' );
225248 } elseif (!in_array ($ this ->configuration ['type ' ], ['bedrock ' , 'wordpress ' ])) {
226249 throw new RuntimeException ('The allowed project "type" are "bedrock" or "wordpress" ' );
227250 }
228251 }
229252
230- /**
231- * Load the options from the configuration file.
232- */
233- private function load (string $ configurationFilePath ): array
234- {
235- $ configuration = [];
236-
237- if ($ this ->filesystem ->exists ($ configurationFilePath )) {
238- $ configuration = Yaml::parse ((string ) file_get_contents ($ configurationFilePath ));
239- }
240-
241- if (!empty ($ configuration ) && !is_array ($ configuration )) {
242- throw new RuntimeException ('Error parsing ymir.yml file ' );
243- }
244-
245- return $ configuration ;
246- }
247-
248253 /**
249254 * Save the configuration options to the configuration file.
250255 */
251256 private function save ()
252257 {
258+ if (empty ($ this ->configurationFilePath )) {
259+ throw new RuntimeException ('No Ymir project configuration file path set ' );
260+ }
261+
253262 $ this ->filesystem ->dumpFile ($ this ->configurationFilePath , str_replace ('!!float 8 ' , '8.0 ' , Yaml::dump ($ this ->configuration , 20 , 2 , Yaml::DUMP_NULL_AS_TILDE )));
254263 }
255264}
0 commit comments