mirror of
https://github.com/doctrine/doctrine-website.git
synced 2026-03-23 22:32:11 +01:00
127 lines
3.1 KiB
PHP
127 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Doctrine\Website\Builder;
|
|
|
|
use Symfony\Component\Finder\Finder;
|
|
use Symfony\Component\Yaml\Yaml;
|
|
use function end;
|
|
use function explode;
|
|
use function file_get_contents;
|
|
use function in_array;
|
|
use function preg_match;
|
|
use function preg_replace;
|
|
use function str_replace;
|
|
use function strrpos;
|
|
use function substr;
|
|
|
|
class SourceFileRepository
|
|
{
|
|
/** @var string */
|
|
private $rootDir;
|
|
|
|
public function __construct(string $rootDir)
|
|
{
|
|
$this->rootDir = $rootDir;
|
|
}
|
|
|
|
/**
|
|
* @return SourceFile[]
|
|
*/
|
|
public function getFiles(string $buildDir, string $inPath = 'source') : array
|
|
{
|
|
$finder = new Finder();
|
|
|
|
$finder
|
|
->in($this->rootDir . '/' . $inPath)
|
|
->files();
|
|
|
|
$files = [];
|
|
|
|
foreach ($finder as $splFileInfo) {
|
|
$path = $splFileInfo->getRealPath();
|
|
|
|
$contents = file_get_contents($path);
|
|
|
|
$extension = $this->getExtension($path);
|
|
|
|
$writePath = $this->getWritePath($buildDir, $path, $extension);
|
|
|
|
$parameters = $this->getFileParameters($buildDir, $writePath, $contents);
|
|
|
|
$writePath = $buildDir . $parameters['url'];
|
|
|
|
$contents = $this->stripFileParameters($contents);
|
|
|
|
$files[] = new SourceFile(
|
|
$extension,
|
|
$writePath,
|
|
$contents,
|
|
$parameters
|
|
);
|
|
}
|
|
|
|
return $files;
|
|
}
|
|
|
|
private function getWritePath(string $buildDir, string $path, string $extension) : string
|
|
{
|
|
$writePath = $buildDir . str_replace($this->rootDir . '/source', '', $path);
|
|
|
|
if (in_array($extension, ['md', 'rst'], true)) {
|
|
$writePath = substr($writePath, 0, strrpos($writePath, '.')) . '.html';
|
|
}
|
|
|
|
return $writePath;
|
|
}
|
|
|
|
private function getExtension(string $path) : string
|
|
{
|
|
$e = explode('.', $path);
|
|
|
|
return end($e);
|
|
}
|
|
|
|
private function stripFileParameters(string $contents) : string
|
|
{
|
|
return preg_replace('/^\s*(?:---[\s]*[\r\n]+)(.*?)(?:---[\s]*[\r\n]+)(.*?)$/s', '$2', $contents);
|
|
}
|
|
|
|
/**
|
|
* @return mixed[]
|
|
*/
|
|
private function getFileParameters(string $buildDir, string $writePath, string $string) : array
|
|
{
|
|
$parameters = [];
|
|
|
|
if (preg_match('/^\s*(?:---[\s]*[\r\n]+)(.*?)(?:---[\s]*[\r\n]+)(.*?)$/s', $string, $matches)) {
|
|
if (preg_match('/^(\s*[-]+\s*|\s*)$/', $matches[1]) === 0) {
|
|
$parameters = Yaml::parse($matches[1], 1);
|
|
}
|
|
}
|
|
|
|
if (! isset($parameters['layout'])) {
|
|
$parameters['layout'] = 'default';
|
|
}
|
|
|
|
$parameters['url'] = $this->getUrl($buildDir, $writePath, $parameters);
|
|
|
|
return $parameters;
|
|
}
|
|
|
|
/**
|
|
* @param mixed[] $parameters
|
|
*/
|
|
private function getUrl(string $buildDir, string $writePath, array $parameters) : string
|
|
{
|
|
$permalink = $parameters['permalink'] ?? '';
|
|
|
|
if ($permalink !== '' && $permalink !== 'none') {
|
|
return $permalink;
|
|
}
|
|
|
|
return str_replace($buildDir, '', $writePath);
|
|
}
|
|
}
|