From a404ed0789f409fe96b532489f7af42f58da27ea Mon Sep 17 00:00:00 2001 From: HerrHase Date: Sun, 14 Nov 2021 16:11:54 +0100 Subject: [PATCH] adding #15 --- .env.example | 1 + README.md | 34 +-------- app/Controllers/FeedController.php | 53 +++++++++++++ app/Controllers/PageController.php | 8 +- app/Controllers/PostController.php | 36 +++++++++ app/Repositories/Manager.php | 51 ------------- app/Repositories/PostRepository.php | 83 +++++++++++++++++++++ composer.json | 4 +- composer.lock | 67 ++++++++++++++++- public/index.php | 13 +++- resources/views/layout.blade.php | 2 - resources/views/page/blog.blade.php | 53 +++++++++++++ resources/views/partials/date.blade.php | 5 ++ resources/views/partials/readtime.blade.php | 10 +++ resources/views/post/default.blade.php | 17 +++++ resources/views/rss.blade.php | 38 ++++++++++ 16 files changed, 381 insertions(+), 94 deletions(-) create mode 100644 app/Controllers/FeedController.php create mode 100644 app/Controllers/PostController.php delete mode 100644 app/Repositories/Manager.php create mode 100644 app/Repositories/PostRepository.php create mode 100644 resources/views/page/blog.blade.php create mode 100644 resources/views/partials/date.blade.php create mode 100644 resources/views/partials/readtime.blade.php create mode 100644 resources/views/post/default.blade.php create mode 100644 resources/views/rss.blade.php diff --git a/.env.example b/.env.example index c5f35f6..79b0650 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ APP_DEBUG=false +APP_LOCALE=en DIRECTUS_API_URL= DIRECTUS_API_TOKEN= \ No newline at end of file diff --git a/README.md b/README.md index 1dc9e28..1618cac 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Super Gear Directus 1.0.0-rc2 +# Super Gear Directus 1.0.0-rc3 Project to using a Directus Instance as CMS. Structure is inspired by Laravel, using [FlightPHP](https://github.com/mikecao/flight) for handle Request. @@ -25,36 +25,4 @@ DIRECTUS_API_TOKEN= ## Repositories For getting Data use **App\\Respositories\\RepositoryAbstract** to create Repository-Classes. -This is the default class to handle - -```PHP -class PageRepository extends RepositoryAbstract -{ - /** endpoint */ - protected $endpoint = 'pages'; - - /** - * find single page with a slug, - * page must be published - * - * @param string $slug - * @return array - */ - public function findOneBySlug($slug) - { - if (!$slug) { - $slug = [ '_null' => 'true' ]; - } - - return $this->queryBuilder - ->fields(['title', 'slug', 'content', 'view', 'meta', 'media_teaser.*', 'media_hero.*']) - ->aliases('view', 'template') - ->filter([ - 'status' => 'published', - 'slug' => $slug - ]) - ->findOne(); - } -} -``` diff --git a/app/Controllers/FeedController.php b/app/Controllers/FeedController.php new file mode 100644 index 0000000..6587bc3 --- /dev/null +++ b/app/Controllers/FeedController.php @@ -0,0 +1,53 @@ + [ + 'view' => 'rss' + ] + ]; + + /** + * get single page from slug + * + * + * @param string $slug + */ + public function indexAction() + { + $siteRepository = new SiteRepository(); + $site = $siteRepository->findOne(); + + $postRepository = new PostRepository(); + $posts = $postRepository->find($this->limit); + + // change type + header('Content-Type: text/xml'); + + $this->render($this->page, [ + 'site' => $site, + 'posts' => $posts + ]); + } +} diff --git a/app/Controllers/PageController.php b/app/Controllers/PageController.php index 6154445..c30fac7 100644 --- a/app/Controllers/PageController.php +++ b/app/Controllers/PageController.php @@ -3,7 +3,7 @@ namespace App\Controllers; use App\Controllers\DirectusControllerAbstract; -use App\Repositories\Manager; +use App\Repositories\PageRepository; /** * controller for page items from directus @@ -36,11 +36,11 @@ class PageController extends DirectusControllerAbstract */ public function getAction($slug = NULL) { - $repository = Manager::get('Page'); - $page = $repository->findOneBySlug($slug); + $pageRepository = new PageRepository(); + $page = $pageRepository->findOneBySlug($slug); if (count($page['data']) === 0) { - $this->app->redirect('/404', 301); + $this->app->redirect('/404'); } else { $this->render($page); } diff --git a/app/Controllers/PostController.php b/app/Controllers/PostController.php new file mode 100644 index 0000000..b1bc0cb --- /dev/null +++ b/app/Controllers/PostController.php @@ -0,0 +1,36 @@ +findOneBySlug($slug); + + if (count($post['data']) === 0) { + $this->app->redirect('/404'); + } else { + $this->render($post); + } + } +} diff --git a/app/Repositories/Manager.php b/app/Repositories/Manager.php deleted file mode 100644 index 9c792f7..0000000 --- a/app/Repositories/Manager.php +++ /dev/null @@ -1,51 +0,0 @@ -queryBuilder + ->fields([ + 'title', + 'slug', + 'lead', + 'content', + 'view', + 'date_created', + 'published_at', + 'media_teaser.id', + 'media_teaser.description' + ]) + ->aliases('template', 'view') + ->filter([ + 'status' => 'published', + 'published_at' => [ + '_nnull' => 'true' + ] + ]) + ->sort(['published_at']) + ->find(); + } + + /** + * find single page with a slug, + * page must be published + * + * @param string $slug + * @return array + */ + public function findOneBySlug($slug) + { + return $this->queryBuilder + ->fields([ + 'title', + 'slug', + 'lead', + 'content', + 'view', + 'meta', + 'date_created', + 'published_at', + 'media_teaser.id', + 'media_teaser.description', + 'media_hero.id', + 'media_hero.description', + ]) + ->aliases('template', 'view') + ->filter([ + 'status' => 'published', + 'published_at' => [ + '_nnull' => 'true' + ], + 'slug' => $slug + ]) + ->findOne(); + } +} diff --git a/composer.json b/composer.json index cbae854..43c36e2 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,9 @@ "vlucas/phpdotenv": "5.3.*", "jenssegers/blade": "1.4.*", "tentakelfabrik/pirectus": "^0.1.3", - "cocur/slugify": "^4.0" + "cocur/slugify": "^4.0", + "nesbot/carbon": "^2.54", + "mtownsend/read-time": "^2.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 13c9fc9..fb2b52b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5263bc5e77eb9849e010bbef6fa46c6f", + "content-hash": "a83db35a0da974bc28038b8ae4581142", "packages": [ { "name": "cocur/slugify", @@ -1253,6 +1253,71 @@ }, "time": "2021-04-05T01:36:59+00:00" }, + { + "name": "mtownsend/read-time", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/mtownsend5512/read-time.git", + "reference": "b43f178095383bda3cdf606c8a288bf8cacf537e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mtownsend5512/read-time/zipball/b43f178095383bda3cdf606c8a288bf8cacf537e", + "reference": "b43f178095383bda3cdf606c8a288bf8cacf537e", + "shasum": "" + }, + "require": { + "php": "^7.4|^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.3" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Mtownsend\\ReadTime\\Providers\\ReadTimeServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Mtownsend\\ReadTime\\": "src" + }, + "files": [ + "src/helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mark Townsend", + "email": "mtownsend5512@gmail.com", + "role": "Developer" + } + ], + "description": "A PHP package to show users how long it takes to read content.", + "keywords": [ + "Minutes", + "article", + "blog", + "content", + "medium", + "min", + "read", + "reading", + "time" + ], + "support": { + "issues": "https://github.com/mtownsend5512/read-time/issues", + "source": "https://github.com/mtownsend5512/read-time/tree/2.0.0" + }, + "time": "2021-01-11T13:46:58+00:00" + }, { "name": "nesbot/carbon", "version": "2.54.0", diff --git a/public/index.php b/public/index.php index b6499e5..2fc3d02 100644 --- a/public/index.php +++ b/public/index.php @@ -9,11 +9,20 @@ $flight->route('GET /@slug:[a-z0-9\-]+/$', function() use ($flight) { // getting position and build route $position = strlen($flight->request()->url) - 1; $route = substr_replace($flight->request()->url, '', $position, 1); - - $flight->redirect($route); + + $flight->redirect($route, 301); }); $flight->route('GET /404', array(new App\Controllers\PageController, 'notFoundAction')); +$flight->route('GET /feed', array(new App\Controllers\FeedController, 'indexAction')); + +$flight->route('GET /blog/@slug:[a-z0-9\-]+$', array(new App\Controllers\PostController, 'getAction')); $flight->route('GET /(@slug:[a-z0-9\-]+$)', array(new App\Controllers\PageController, 'getAction')); $flight->start(); + +try { + $flight->start(); +} catch (\Exception $exception) { + echo $exception->getMessage(); +} diff --git a/resources/views/layout.blade.php b/resources/views/layout.blade.php index 566e372..ecfd394 100644 --- a/resources/views/layout.blade.php +++ b/resources/views/layout.blade.php @@ -2,8 +2,6 @@ @inject('siteRepository', 'App\Repositories\SiteRepository') @inject('menuRepository', 'App\Repositories\MenuRepository') -@inject('markdownHelper', 'App\Helpers\MarkdownHelper') - @php $site = $siteRepository->findOne(); $menuItems = $menuRepository->findByName('main'); diff --git a/resources/views/page/blog.blade.php b/resources/views/page/blog.blade.php new file mode 100644 index 0000000..ce4aa63 --- /dev/null +++ b/resources/views/page/blog.blade.php @@ -0,0 +1,53 @@ +@extends('layout') + +{{-- pretend duplicate content --}} +@push('head') + +@endpush + +{{-- inject helper for content & repositories --}} +@inject('markdownHelper', 'App\Helpers\MarkdownHelper') +@inject('postRepository', 'App\Repositories\PostRepository') + +@php + $posts = $postRepository->find(); +@endphp + +@section('content') + +

+ {{ $page['data']['title'] }} +

+
+ {!! $markdownHelper->parse($page['data']['content']) !!} +
+ +@if (count($posts) > 0) + @foreach($posts['data'] as $post) + +
+

+ {{ $post['title'] }} +

+ @include('partials.date', ['post' => $post]) + @include('partials.readtime', ['post' => $post]) + @if (isset($post['media_teaser']['id'])) + + @endif +
+
+ {!! $markdownHelper->parse($post['lead']) !!} +
+ + @endforeach +@else +
+

+ Nothing! +

+
+@endif + +@endsection diff --git a/resources/views/partials/date.blade.php b/resources/views/partials/date.blade.php new file mode 100644 index 0000000..48d6a27 --- /dev/null +++ b/resources/views/partials/date.blade.php @@ -0,0 +1,5 @@ + diff --git a/resources/views/partials/readtime.blade.php b/resources/views/partials/readtime.blade.php new file mode 100644 index 0000000..97bb307 --- /dev/null +++ b/resources/views/partials/readtime.blade.php @@ -0,0 +1,10 @@ +@php + $content = $post['lead'].$post['content']; + $readtime = (new \Mtownsend\ReadTime\ReadTime($content))->timeOnly(true)->setTranslation([ + 'minute' => '' + ])->get(); +@endphp + +
+ Reading time {{ $readtime }} Minutes +
\ No newline at end of file diff --git a/resources/views/post/default.blade.php b/resources/views/post/default.blade.php new file mode 100644 index 0000000..bbdf1c4 --- /dev/null +++ b/resources/views/post/default.blade.php @@ -0,0 +1,17 @@ +@extends('layout') + +@inject('markdownHelper', 'App\Helpers\MarkdownHelper') + +@section('content') + +

+ {{ $page['data']['title'] }} +

+
+ {!! $markdownHelper->parse($page['data']['lead']) !!} +
+
+ {!! $markdownHelper->parse($page['data']['content']) !!} +
+ +@endsection diff --git a/resources/views/rss.blade.php b/resources/views/rss.blade.php new file mode 100644 index 0000000..a2bd292 --- /dev/null +++ b/resources/views/rss.blade.php @@ -0,0 +1,38 @@ +@inject('markdownHelper', 'App\Helpers\MarkdownHelper') + +@php +if (isset($_SERVER['HTTPS'])) { + $http = 'https'; +} else { + $http = 'http'; +} + +$base_url = $http.'://'.$_SERVER['SERVER_NAME']; + +@endphp + + + + + {{ $site['data']['title'] }} + + {{ $base_url }}/blog + {{ $site['data']['description'] }} + {{ date(DATE_RSS) }} + {{ $_ENV['APP_LOCALE'] }}-{{ strtoupper($_ENV['APP_LOCALE']) }} + @foreach($posts['data'] as $post) + + {{ $post['title'] }} + {{ $base_url.'/blog/'.$post['slug'] }} + {{ date(DATE_RSS, strtotime($post['published_at'])) }} + + parse($post['lead']) !!} + {!! $markdownHelper->parse($post['content']) !!} + ]]> + + {{ $post['slug'] }} + + @endforeach + +