| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Integration\Timber; |
| 13 |
|
| 14 |
use WindPressDeps\Symfony\Component\Finder\Finder; |
| 15 |
use Timber\LocationManager; |
| 16 |
use Timber\Timber; |
| 17 |
use WindPress\WindPress\Core\Scanner\FileScanner; |
| 18 |
/** |
| 19 |
* @author Joshua Gugun Siagian <suabahasa@gmail.com> |
| 20 |
*/ |
| 21 |
class Compile |
| 22 |
{ |
| 23 |
/** |
| 24 |
* @param array $metadata |
| 25 |
*/ |
| 26 |
public function __invoke($metadata): array |
| 27 |
{ |
| 28 |
if (!class_exists(Timber::class)) { |
| 29 |
return []; |
| 30 |
} |
| 31 |
return $this->get_contents($metadata); |
| 32 |
} |
| 33 |
public function get_contents($metadata): array |
| 34 |
{ |
| 35 |
return FileScanner::scan_files('timber', static function (): \Generator { |
| 36 |
$paths = LocationManager::get_locations(); |
| 37 |
$paths = array_unique(array_filter(array_merge(...array_values($paths)), static fn($path) => is_string($path) && $path !== '/' && is_dir($path))); |
| 38 |
if ($paths === []) { |
| 39 |
return; |
| 40 |
} |
| 41 |
$finder = new Finder(); |
| 42 |
$finder->in($paths)->files()->name('*.twig'); |
| 43 |
do_action('a!windpress/integration/timber/compile:get_contents.finder', $finder); |
| 44 |
foreach ($finder as $file) { |
| 45 |
yield ['path' => $file->getPathname(), 'name' => $file->getRelativePathname()]; |
| 46 |
} |
| 47 |
}, $metadata['next_batch'] ?? \false); |
| 48 |
} |
| 49 |
} |
| 50 |
|