@@ -20,6 +20,26 @@ public function __construct(
2020 $ this ->name = $ this ->origin ->dirname ;
2121 }
2222
23+ /** Creates a source from a given origin folder */
24+ public static function in (string |Folder $ origin ): self {
25+ static $ implementations = [
26+ 'content.md ' => new Content (...),
27+ 'journey.md ' => new Journey (...),
28+ 'cover.md ' => new Cover (...),
29+ ];
30+
31+ foreach ($ implementations as $ source => $ new ) {
32+ $ file = new File ($ origin , $ source );
33+ if ($ file ->exists ()) return $ new ($ origin instanceof Folder ? $ origin : new Folder ($ origin ), $ file );
34+ }
35+
36+ throw new IllegalArgumentException (sprintf (
37+ 'Cannot locate any of [%s] in %s ' ,
38+ implode (', ' , array_keys ($ implementations )),
39+ $ origin
40+ ));
41+ }
42+
2343 /** Returns this source's name */
2444 public function name (): string { return $ this ->name ; }
2545
@@ -29,26 +49,22 @@ public function parent(): ?string { return strstr($this->name(), '/', true) ?: n
2949 /** Sets a parent for this source */
3050 public function nestedIn (string $ parent ): self { $ this ->name = $ parent .'/ ' .$ this ->name ; return $ this ; }
3151
52+ /** Returns this source's origin */
53+ public function origin (): Folder { return $ this ->origin ; }
54+
3255 /** Yields all the media files in this source */
3356 protected function mediaIn (Files $ files ): iterable {
34- static $ processed = '/^(thumb|preview|full|video|screen)-/ ' ;
35-
3657 $ images = [];
3758 foreach ($ this ->entry ['images ' ] ?? [] as $ image ) {
3859 $ images [$ image ['name ' ]]= $ image ;
3960 }
4061
41- foreach ($ this ->origin ->entries () as $ path ) {
42- $ name = $ path ->name ();
43- if ($ path ->isFile () && !preg_match ($ processed , $ name ) && ($ processing = $ files ->processing ($ name ))) {
44- $ file = $ path ->asFile ();
45- $ name = $ file ->filename ;
46-
47- if (!isset ($ images [$ name ]) || $ file ->lastModified () > $ images [$ name ]['modified ' ]) {
48- yield new UploadMedia ($ this ->entry ['slug ' ], $ file , $ processing );
49- }
50- unset($ images [$ name ]);
62+ foreach ($ files ->in ($ this ->origin ) as $ file => $ processing ) {
63+ $ name = $ file ->filename ;
64+ if (!isset ($ images [$ name ]) || $ file ->lastModified () > $ images [$ name ]['modified ' ]) {
65+ yield new UploadMedia ($ this ->entry ['slug ' ], $ file , $ processing );
5166 }
67+ unset($ images [$ name ]);
5268 }
5369
5470 foreach ($ images as $ rest ) {
0 commit comments