| 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 |
/** |
| 18 |
* @author Joshua Gugun Siagian <suabahasa@gmail.com> |
| 19 |
*/ |
| 20 |
class Compile |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @param array $metadata |
| 24 |
*/ |
| 25 |
public function __invoke($metadata): array |
| 26 |
{ |
| 27 |
if (!class_exists(Timber::class)) { |
| 28 |
return []; |
| 29 |
} |
| 30 |
return $this->get_contents($metadata); |
| 31 |
} |
| 32 |
public function get_contents($metadata): array |
| 33 |
{ |
| 34 |
$contents = []; |
| 35 |
$paths = LocationManager::get_locations(); |
| 36 |
$paths = array_unique(array_filter(array_merge(...array_values($paths)), fn($path) => $path !== '/')); |
| 37 |
$finder = new Finder(); |
| 38 |
$finder->in($paths); |
| 39 |
$finder->files()->name('*.twig'); |
| 40 |
do_action('a!windpress/integration/timber/compile:get_contents.finder', $finder); |
| 41 |
foreach ($finder as $file) { |
| 42 |
$template_file = $file->getPathname(); |
| 43 |
if (!is_readable($template_file)) { |
| 44 |
continue; |
| 45 |
} |
| 46 |
$contents[] = ['name' => $file->getRelativePathname(), 'content' => $file->getContents()]; |
| 47 |
} |
| 48 |
return ['metadata' => ['next_batch' => \false, 'total_batches' => 1], 'contents' => $contents]; |
| 49 |
} |
| 50 |
} |
| 51 |
|