Skip to content

Commit

Permalink
Merge pull request #17 from librarianphp/inject-contentType
Browse files Browse the repository at this point in the history
Injecting content type in ContentServiceProvider
  • Loading branch information
erikaheidi authored Jun 1, 2023
2 parents 391fceb + dbcb42d commit 90af0c9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
14 changes: 9 additions & 5 deletions src/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ class Content extends Parsed
/** @var string Content Slug */
public string $slug;

/** @var string Route for this Content */
/** @var string Route */
public string $route;

/** @var ContentType Content Type */
public ContentType $contentType;

/** @var string Link to this content */
public string $link;

Expand All @@ -29,13 +32,14 @@ class Content extends Parsed

/**
* Sets content type / route
* @param string $route
* @param ContentType $contentType
*/
public function setRoute($route)
public function setContentType(ContentType $contentType): void
{
$this->route = $route;
$this->contentType = $contentType;
$this->route = $contentType->slug;
}

/**
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ContentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function next(): void
++$this->current_position;
}

public function key(): mixed
public function key(): int
{
return $this->current_position;
}
Expand Down
20 changes: 15 additions & 5 deletions src/Provider/ContentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function fetch(string $route, bool $parse_markdown = true): ?Content

try {
$content->load($filename);
$content->setRoute($request->getRoute());
$content->setContentType($this->getContentType($request->getRoute()));

$content->parse($this->parser, $parse_markdown);
} catch (ContentNotFoundException $e) {
Expand All @@ -86,6 +86,7 @@ public function fetch(string $route, bool $parse_markdown = true): ?Content
* @param bool $parse_markdown
* @param string $orderBy
* @return ContentCollection
* @throws ContentNotFoundException
*/
public function fetchAll(int $start = 0, int $limit = 20, bool $parse_markdown = false, string $orderBy = 'desc'): ContentCollection
{
Expand All @@ -99,7 +100,7 @@ public function fetchAll(int $start = 0, int $limit = 20, bool $parse_markdown =
try {
$content->load($filename);
$content->parse($this->parser, $parse_markdown);
$content->setRoute($contentType->slug);
$content->setContentType($contentType);
$list[] = $content;
} catch (ContentNotFoundException $e) {
continue;
Expand All @@ -121,6 +122,7 @@ public function fetchAll(int $start = 0, int $limit = 20, bool $parse_markdown =
/**
* @param int $per_page
* @return int
* @throws ContentNotFoundException
*/
public function fetchTotalPages(int $per_page = 20): int
{
Expand Down Expand Up @@ -153,6 +155,7 @@ public function fetchTagTotalPages(string $tag, int $per_page = 20): int

/**
* @return array|mixed
* @throws ContentNotFoundException
*/
public function fetchTagList(bool $cached = true): ?array
{
Expand Down Expand Up @@ -239,16 +242,23 @@ public function getContentTypes(): array
return $orderedContent;
}

public function fetchFrom(string $route, int $start = 0, int $limit = 20, bool $parse_markdown = false, string $orderBy = 'desc'): ?ContentCollection
/**
* @throws ContentNotFoundException
*/
public function getContentType(string $contentType): ContentType
{
return new ContentType($contentType, $this->data_path);
}
public function fetchFrom(ContentType $contentType, int $start = 0, int $limit = 20, bool $parse_markdown = false, string $orderBy = 'desc'): ?ContentCollection
{
$feed = [];

foreach (glob($this->data_path . '/' . $route . '/*.md') as $filename) {
foreach (glob($this->data_path . '/' . $contentType->slug . '/*.md') as $filename) {
$content = new Content();
try {
$content->load($filename);
$content->parse($this->parser, $parse_markdown);
$content->setRoute($route);
$content->setContentType($contentType);
$feed[] = $content;
} catch (ContentNotFoundException $e) {
continue;
Expand Down
6 changes: 4 additions & 2 deletions tests/Feature/ContentCollectionTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Librarian\ContentType;
use Minicli\App;
use Librarian\Provider\ContentServiceProvider;
use Librarian\ContentCollection;
Expand Down Expand Up @@ -53,8 +54,9 @@
it('orders content type posts based on front matter index (index)', function () {
$app = new App($this->config);
$app->addService('content', new ContentServiceProvider());

$posts = $app->content->fetchFrom('posts', 0, 10, false, 'index');
$cType = $app->content->getContentType('posts');
expect($cType)->toBeInstanceOf(ContentType::class);
$posts = $app->content->fetchFrom($cType, 0, 10, false, 'index');

expect($posts->current()->frontMatterGet('title'))->toEqual("Testing Markdown Front Matter");
});
Expand Down

0 comments on commit 90af0c9

Please sign in to comment.