added sorting and render service classes

plugged in sorting class to gather the info necessary for the render
class to convert markown files to html and move them to the correct
location in the public diretory
This commit is contained in:
ro 2024-03-14 12:39:43 -06:00
parent 4f7bbcdf86
commit 950ca6f7ea
No known key found for this signature in database
GPG key ID: 29B551CDBD4D3B50
6 changed files with 286 additions and 0 deletions

View file

@ -11,7 +11,10 @@ use App\Services\ContentService;
use App\Services\PaginateService; use App\Services\PaginateService;
use App\Services\ThemeService; use App\Services\ThemeService;
use App\Services\DocService; use App\Services\DocService;
use App\Services\StringService;
use App\Services\FileUploadService; use App\Services\FileUploadService;
use App\Services\RenderService;
use App\Services\SortingService;
class FipamoServiceProvider extends ServiceProvider class FipamoServiceProvider extends ServiceProvider
{ {
@ -52,6 +55,18 @@ class FipamoServiceProvider extends ServiceProvider
$this->app->bind(FileUploadService::class, function ($app) { $this->app->bind(FileUploadService::class, function ($app) {
return new FileUploadService(); return new FileUploadService();
}); });
$this->app->bind(RenderService::class, function ($app) {
return new RenderService();
});
$this->app->bind(SortingService::class, function ($app) {
return new SortingService(
new SettingsService(new DocService()),
new ContentService(),
new StringService(),
);
});
} }
/** /**

View file

@ -8,6 +8,7 @@ use App\Services\ContentService;
use App\Services\PaginateService; use App\Services\PaginateService;
use App\Services\StringService; use App\Services\StringService;
use App\Services\DocService; use App\Services\DocService;
use App\Services\SortingService;
use Carbon\Carbon; use Carbon\Carbon;
class PageRepository implements PageRepositoryInterface class PageRepository implements PageRepositoryInterface
@ -18,6 +19,7 @@ class PageRepository implements PageRepositoryInterface
protected $pages; protected $pages;
protected $strings; protected $strings;
protected $docs; protected $docs;
protected $sort;
public function __construct( public function __construct(
ContentService $contentService, ContentService $contentService,
@ -25,12 +27,14 @@ class PageRepository implements PageRepositoryInterface
PaginateService $paginateService, PaginateService $paginateService,
StringService $stringService, StringService $stringService,
DocService $docService, DocService $docService,
SortingService $sortingService,
) { ) {
$this->content = $contentService; $this->content = $contentService;
$this->settings = $settingsService; $this->settings = $settingsService;
$this->paginate = $paginateService; $this->paginate = $paginateService;
$this->strings = $stringService; $this->strings = $stringService;
$this->docs = $docService; $this->docs = $docService;
$this->sort = $sortingService;
$this->pages = $this->content->loadAllPages(); $this->pages = $this->content->loadAllPages();
} }

View file

@ -0,0 +1,10 @@
<?php
namespace App\Services;
class RenderService
{
public function __construct()
{
}
}

View file

@ -31,6 +31,12 @@ class SettingsService
return $this->settings['global']; return $this->settings['global'];
} }
public function getMenu()
{
$this->settings = $this->loadSettings();
return $this->settings['menu'];
}
public function getFolks() public function getFolks()
{ {
return $this->folks; return $this->folks;

View file

@ -0,0 +1,229 @@
<?php
namespace App\Services;
use function _\find;
use function _\filter;
class SortingService
{
private $p_tags = [];
private $p_archive = [];
private $settings;
private $contents;
private $strings;
public function __construct(
SettingsService $settingsService,
ContentService $contentService,
StringService $stringService
) {
$this->settings = $settingsService;
$this->contents = $contentService;
$this->strings = $stringService;
}
public function tags()
{
$pages = $this->contents->loadAllPages();
foreach ($pages as $page) {
$temp = [];
if (isset($page['tags'])) {
$temp = explode(',', $page['tags']);
foreach ($temp as $tag) {
$label = trim($tag);
if (!find($this->p_tags, ['tag_name' => $label])) {
array_push($this->p_tags, [
'tag_name' => $label,
'slug' => $this->strings::safeString($label),
'pages' => $this->tagPages($label, $pages),
]);
}
}
}
}
return $this->p_tags;
}
private function tagPages($tag, $pages)
{
$tagged = [];
foreach ($pages as $page) {
if (isset($page['tags'])) {
if (strpos($page['tags'], $tag) !== false) {
array_push($tagged, [
'title' => $page['title'],
'slug' => $page['slug'],
'path' => $page['path'],
'feature' => $page['feature'],
]);
}
}
}
return $tagged;
}
public function archive()
{
$pages = $this->contents->loadAllPages();
$years = [];
$archive = [];
foreach ($pages as $page) {
// $year = date("Y", date($page["rawCreated"]));
$date = explode('/', $page['path']);
// echo $page["title"] . " : " . $year . "\n";
if (!find($years, ['year' => trim($date[0])])) {
$findPages = filter($pages, ['createdYear' => trim($date[0])]);
// var_dump($findPages);
array_push(
$years,
[
'year' => trim($date[0]),
'count' => count($findPages),
]
);
}
}
foreach ($years as $year) {
$sorted = [];
$filtered = filter($pages, ['createdYear' => $year['year']]);
foreach ($filtered as $obj) {
$month = date('m', date($obj['rawCreated']));
if (!find($sorted, ['month' => $month])) {
$perMonth = filter(
$pages,
[
'path' => $year['year'] . '/' . $month,
'deleted' => false,
'published' => true,
'layout' => 'page',
]
);
array_push($sorted, [
'month' => $month,
'full_month' => date('F', date($obj['rawCreated'])),
'count' => count($perMonth),
'pages' => $perMonth,
]);
}
}
array_push($this->p_archive, [
'year' => $year['year'],
'year_data' => $sorted,
]);
}
return $this->p_archive;
}
public function page($page)
{
$global = $this->settings->getGlobal();
$pageOptions = [];
$pageInfo = [
'keywords' => isset($global['keywords'])
? $global['keywords']
: 'fipamo, blog, jamstack, php, markdown, js',
'description' => $global['descriptions'],
'image' => $global['base_url'] . $global['background'],
'baseURL' => $global['base_url'],
];
$tags = [];
if (isset($page['tags'])) {
$taglist = explode(',', $page['tags']);
foreach ($taglist as $tag) {
$label = trim($tag);
array_push($tags, [
'label' => $label . ' ',
'slug' => $this->strings->safeString($label),
]);
}
}
$meta = [
'who' => $page['author'],
'when' => $page['created'],
'tags' => $tags,
];
// if page feature isn't empty, find image from list and set it as background image
// if it is empty, just use global background
if ($page['feature'] != '' || $page['feature'] != null) {
$media = explode(',', $page['feature']);
$set = false;
foreach ($media as $file) {
$item = trim($file);
$ext = pathinfo($item, PATHINFO_EXTENSION);
if ($ext != 'mp4' && !$set) {
$pageInfo['image'] = $pageInfo['baseURL'] . $item;
$set = true;
}
}
}
if ($page['layout'] == 'index') {
$recent = [];
$featured = [];
$limit = 4;
$pages = $this->contents->loadAllPages();
foreach ($pages as $item) {
if (
!$item['deleted'] &&
$item['published'] &&
$item['menu'] != 'true'
) {
if (count($recent) < $limit) {
array_push($recent, [
'path' => $item['path'],
'slug' => $item['slug'],
'title' => $item['title'],
'feature' => $item['feature'],
]);
}
if ($item['featured'] == true) {
if (count($featured) < $limit) {
array_push($featured, [
'path' => $item['path'],
'slug' => $item['slug'],
'title' => $item['title'],
'feature' => $item['feature'],
]);
}
}
}
}
$pageOptions = [
'title' => $page['title'],
'background' => $page['feature'],
'content' => $page['html'], // $cleaned,
'meta' => $meta,
'recent' => $recent,
'featured' => $featured,
'info' => $pageInfo,
'menu' => $this->settings->getMenu(),
'dynamicRender' => $global['dynamicRender'],
'media' => $page['media'],
'files' => $page['docs'],
];
} else {
$pageOptions = [
'title' => $page['title'],
'background' => $page['feature'],
'content' => $page['html'], // $cleaned,
'meta' => $meta,
'info' => $pageInfo,
'menu' => $this->settings->getMenu(),
'dynamicRender' => $global['dynamicRender'],
'media' => $page['media'],
'files' => $page['docs'],
];
}
var_dump($pageOptions);
//return $pageOptions;
}
}

View file

@ -26,4 +26,26 @@ class StringService
mt_rand(0, 65535) mt_rand(0, 65535)
); );
} }
public static function safeString($string)
{
return strtolower(
trim(
preg_replace(
'~[^0-9a-z]+~i',
'_',
html_entity_decode(
preg_replace(
'~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i',
'$1',
htmlentities($string, ENT_QUOTES, 'UTF-8')
),
ENT_QUOTES,
'UTF-8'
)
),
'-'
)
);
}
} }