Skip to content

Commit

Permalink
Merge pull request #79 from 8fold/improve-testability
Browse files Browse the repository at this point in the history
Improve testability
  • Loading branch information
joshbruce authored Nov 10, 2021
2 parents 2494600 + 5d04e92 commit 656c1a1
Show file tree
Hide file tree
Showing 39 changed files with 886 additions and 582 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
<php>
<ini name="display_errors" value="On"/>
<ini name="display_startup_errors" value="On"/>
<server name="APP_ENV" value="test" force="true"/>
<server name="APP_URL" value="http://jb-site.test"/>
</php>

</phpunit>
5 changes: 4 additions & 1 deletion site-dynamic-php/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

JoshBruce\Site\SiteDynamic\Emitter::emit(
response:JoshBruce\Site\HttpResponse::from(
request: JoshBruce\Site\HttpRequest::fromGlobals()
request: JoshBruce\Site\HttpRequest::with(
JoshBruce\Site\ServerGlobals::init(),
JoshBruce\Site\FileSystem::init()
)
)
);
exit;
35 changes: 23 additions & 12 deletions src/Content/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Eightfold\Markdown\Markdown as MarkdownConverter;

use JoshBruce\Site\File;
use JoshBruce\Site\FileSystemInterface;

use JoshBruce\Site\PageComponents\Data;
use JoshBruce\Site\PageComponents\DateBlock;
Expand All @@ -24,9 +25,9 @@ class Markdown

private string $body = '';

public static function for(File $file): Markdown
public static function for(File $file, FileSystemInterface $in): Markdown
{
return new Markdown($file);
return new Markdown($file, $in);
}

public static function markdownConverter(): MarkdownConverter
Expand All @@ -49,8 +50,10 @@ public static function markdownConverter(): MarkdownConverter
);
}

private function __construct(private File $file)
{
private function __construct(
private File $file,
private FileSystemInterface $fileSystem
) {
}

public function html(): string
Expand All @@ -77,10 +80,13 @@ public function html(): string
$b = '';
$template = $templateMap[$templateKey];
if ($templateKey === 'loglist') {
$b = $template::create($this->file);
$b = $template::create($this->file(), $this->fileSystem());

} else {
$b = $template::create($this->frontMatter());
$b = $template::create(
$this->frontMatter(),
$this->fileSystem()
);

}

Expand Down Expand Up @@ -118,11 +124,11 @@ public function pageTitle(): string
$titles = [];
$titles[] = $this->frontMatter()->title();

$file = clone $this->file;
$file = clone $this->file();
while ($file->canGoUp()) {
$file = $file->up();

$m = Markdown::for($file);
$m = Markdown::for($file, $this->fileSystem());

$titles[] = $m->frontMatter()->title();
}
Expand All @@ -131,16 +137,21 @@ public function pageTitle(): string
return implode(' | ', $titles);
}

public function canonicalURl(): string
public function file(): File
{
return $this->file->canonicalUrl();
return $this->file;
}

private function fileContent(): string
{
if (strlen($this->fileContent) === 0 and $this->file->found()) {
$this->fileContent = $this->file->contents();
if (strlen($this->fileContent) === 0 and $this->file()->found()) {
$this->fileContent = $this->file()->contents();
}
return $this->fileContent;
}

private function fileSystem(): FileSystemInterface
{
return $this->fileSystem;
}
}
11 changes: 6 additions & 5 deletions src/Documents/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@
use Eightfold\XMLBuilder\Document;
use Eightfold\XMLBuilder\Element;

use JoshBruce\Site\FileSystem;
use JoshBruce\Site\FileSystemInterface;
use JoshBruce\Site\File;

use JoshBruce\Site\Content\Markdown;

class Sitemap
{
public static function create(): string
public static function create(FileSystemInterface $fileSystem): string
{
$finder = FileSystem::finder()->name('content.md')->sortByName()
$finder = $fileSystem->publishedContentFinder()->sortByName()
->notContains('redirect:')
->notContains('noindex:');

$markdown = [];
foreach ($finder as $file) {
$markdown[] = Markdown::for(
File::at($file->getPathname())
File::at($file->getPathname(), $fileSystem),
$fileSystem
);
}

$urls = [];
foreach ($markdown as $m) {
$urls[] = Element::url(
Element::loc($m->canonicalUrl())
Element::loc($m->file()->canonicalUrl())
);
}

Expand Down
74 changes: 45 additions & 29 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@

use DirectoryIterator;

use JoshBruce\Site\FileSystem;
use JoshBruce\Site\FileSystemInterface;

class File
{
private string $contentFileName = '/content.md';

public static function at(string $localPath): File
private string $contents = '';

private string $mimetype = '';

public static function at(string $localPath, FileSystemInterface $in): File
{
return new File($localPath);
return new File($localPath, $in);
}

private function __construct(private string $localPath)
{
private function __construct(
private string $localPath,
private FileSystemInterface $fileSystem
) {
}

public function isNotMarkdown(): bool
Expand Down Expand Up @@ -60,7 +66,7 @@ public function path(bool $full = true): string
}
// TODO: test and verify used - returning empty string not an option.
return str_replace(
$this->contentRoot(),
$this->fileSystem()->publicRoot(),
'',
$this->localPath
);
Expand All @@ -76,39 +82,48 @@ public function up(): File
$parts = explode('/', $this->localPath);
$parts = array_slice($parts, 0, -2); // remove file name and one folder.
$localPath = implode('/', $parts);
return File::at($localPath . $this->contentFileName);
return File::at(
$localPath . $this->contentFileName,
$this->fileSystem()
);
}

public function contents(): string
{
$contents = file_get_contents($this->path());
if ($contents === false) {
return '';
if (strlen($this->contents) === 0) {
$contents = file_get_contents($this->path());
if ($contents === false) {
return '';
}
$this->contents = $contents;
}
return $contents;
return $this->contents;
}

public function mimetype(): string
{
$type = mime_content_type($this->path());
if (is_bool($type) and $type === false) {
return '';
}
if (strlen($this->mimetype) === 0) {
$type = mime_content_type($this->path());
if (is_bool($type) and $type === false) {
return '';
}

if ($type === 'text/plain') {
$extensionMap = [
'md' => 'text/html',
'css' => 'text/css',
'js' => 'text/javascript',
'xml' => 'application/xml'
];
if ($type === 'text/plain') {
$extensionMap = [
'md' => 'text/html',
'css' => 'text/css',
'js' => 'text/javascript',
'xml' => 'application/xml'
];

$parts = explode('.', $this->path());
$extension = array_pop($parts);
$parts = explode('.', $this->path());
$extension = array_pop($parts);

$type = $extensionMap[$extension];
$type = $extensionMap[$extension];
}
$this->mimetype = $type;
}
return $type;
return $this->mimetype;
}

public function canonicalUrl(): string
Expand Down Expand Up @@ -138,14 +153,15 @@ public function children(string $filesNamed): array
$folderName = array_pop($parts);

$files[$folderName] = File::at(
$fullPathToFolder . '/' . $filesNamed
$fullPathToFolder . '/' . $filesNamed,
$this->fileSystem()
);
}
return $files;
}

private function contentRoot(): string
private function fileSystem(): FileSystemInterface
{
return FileSystem::publicRoot();
return $this->fileSystem;
}
}
75 changes: 49 additions & 26 deletions src/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,38 @@

use Symfony\Component\Finder\Finder;

class FileSystem
use JoshBruce\Site\FileSystemInterface;

class FileSystem implements FileSystemInterface
{
public static function publicRoot(): string
public static function init(): static
{
return new static(static::projectRoot());
}

public static function projectRoot(): string
{
$dir = __DIR__;
$parts = explode('/', $dir);
$parts = array_slice($parts, 0, -1);
return implode('/', $parts);
}

final private function __construct(protected string $projectRoot)
{
return FileSystem::contentRoot() . '/public';
}

public static function contentRoot(): string
public function hasRequiredFolders(): bool
{
$parts = explode('/', self::projectRoot());
return file_exists($this->contentRoot()) and
file_exists($this->publicRoot()) and
is_dir($this->contentRoot()) and
is_dir($this->publicRoot());
}

public function contentRoot(): string
{
$parts = explode('/', static::projectRoot());
$parts[] = 'content';
$base = implode('/', $parts);
if (str_ends_with($base, '/')) {
Expand All @@ -26,41 +48,42 @@ public static function contentRoot(): string
return $base;
}

public static function projectRoot(): string
public function publicRoot(): string
{
$dir = __DIR__;
$parts = explode('/', $dir);
$parts = array_slice($parts, 0, -1);
return implode('/', $parts);
return $this->contentRoot() . '/public';
}

public static function finder(): Finder
public function publishedContentFinder(): Finder
{
$finder = new Finder();
return $finder->ignoreVCS(false)
->ignoreUnreadableDirs()
->ignoreDotFiles(false)
->ignoreVCSIgnored(true)
->notName('.gitignore')
->files()
->filter(fn($f) => self::isPublished($f))
->in(self::publicRoot());
return $this->finder()->in($this->publicRoot())->name('content.md')
->filter(fn($f) => $this->isPublished($f));
}

private static function isPublished(SplFileInfo $finderFile): bool
private function relativePath(string $path): string
{
return ! self::isDraft($finderFile);
return str_replace($this->contentRoot(), '', $path);
}

private static function isDraft(SplFileInfo $finderFile): bool
private function isPublished(SplFileInfo $finderFile): bool
{
return ! $this->isDraft($finderFile);
}

private function isDraft(SplFileInfo $finderFile): bool
{
$filePath = (string) $finderFile;
$relativePath = self::relativePath($filePath);
$relativePath = $this->relativePath($filePath);
return str_contains($relativePath, '_');
}

private static function relativePath(string $path): string
private function finder(): Finder
{
return str_replace(self::contentRoot(), '', $path);
$finder = new Finder();
return $finder->ignoreVCS(false)
->ignoreUnreadableDirs()
->ignoreDotFiles(false)
->ignoreVCSIgnored(true)
->notName('.gitignore')
->files();
}
}
Loading

0 comments on commit 656c1a1

Please sign in to comment.