Browse Source

adding #15

develop
HerrHase 3 years ago
parent
commit
a404ed0789
16 changed files with 381 additions and 94 deletions
  1. +1
    -0
      .env.example
  2. +1
    -33
      README.md
  3. +53
    -0
      app/Controllers/FeedController.php
  4. +4
    -4
      app/Controllers/PageController.php
  5. +36
    -0
      app/Controllers/PostController.php
  6. +0
    -51
      app/Repositories/Manager.php
  7. +83
    -0
      app/Repositories/PostRepository.php
  8. +3
    -1
      composer.json
  9. +66
    -1
      composer.lock
  10. +11
    -2
      public/index.php
  11. +0
    -2
      resources/views/layout.blade.php
  12. +53
    -0
      resources/views/page/blog.blade.php
  13. +5
    -0
      resources/views/partials/date.blade.php
  14. +10
    -0
      resources/views/partials/readtime.blade.php
  15. +17
    -0
      resources/views/post/default.blade.php
  16. +38
    -0
      resources/views/rss.blade.php

+ 1
- 0
.env.example View File

@ -1,4 +1,5 @@
APP_DEBUG=false APP_DEBUG=false
APP_LOCALE=en
DIRECTUS_API_URL= DIRECTUS_API_URL=
DIRECTUS_API_TOKEN= DIRECTUS_API_TOKEN=

+ 1
- 33
README.md View File

@ -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) Project to using a Directus Instance as CMS. Structure is inspired by Laravel, using [FlightPHP](https://github.com/mikecao/flight)
for handle Request. for handle Request.
@ -25,36 +25,4 @@ DIRECTUS_API_TOKEN=
## Repositories ## Repositories
For getting Data use **App\\Respositories\\RepositoryAbstract** to create Repository-Classes. 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();
}
}
```

+ 53
- 0
app/Controllers/FeedController.php View File

@ -0,0 +1,53 @@
<?php
namespace App\Controllers;
use App\Controllers\DirectusControllerAbstract;
use App\Repositories\SiteRepository;
use App\Repositories\PostRepository;
/**
* controller for render feed of posts
*
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.tentakelfabrik.de/Tentakelfabrik/super-gear-directus
*
*/
class FeedController extends DirectusControllerAbstract
{
private $limit = 20;
/**
*
*/
protected $page = [
'data' => [
'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
]);
}
}

+ 4
- 4
app/Controllers/PageController.php View File

@ -3,7 +3,7 @@
namespace App\Controllers; namespace App\Controllers;
use App\Controllers\DirectusControllerAbstract; use App\Controllers\DirectusControllerAbstract;
use App\Repositories\Manager;
use App\Repositories\PageRepository;
/** /**
* controller for page items from directus * controller for page items from directus
@ -36,11 +36,11 @@ class PageController extends DirectusControllerAbstract
*/ */
public function getAction($slug = NULL) 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) { if (count($page['data']) === 0) {
$this->app->redirect('/404', 301);
$this->app->redirect('/404');
} else { } else {
$this->render($page); $this->render($page);
} }


+ 36
- 0
app/Controllers/PostController.php View File

@ -0,0 +1,36 @@
<?php
namespace App\Controllers;
use App\Controllers\DirectusControllerAbstract;
use App\Repositories\PostRepository;
/**
* controller for page items from directus
*
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.tentakelfabrik.de/Tentakelfabrik/super-gear-directus
*
*/
class PostController extends DirectusControllerAbstract
{
/**
* get single page from slug
*
*
* @param string $slug
*/
public function getAction($slug)
{
$repository = new PostRepository();
$post = $repository->findOneBySlug($slug);
if (count($post['data']) === 0) {
$this->app->redirect('/404');
} else {
$this->render($post);
}
}
}

+ 0
- 51
app/Repositories/Manager.php View File

@ -1,51 +0,0 @@
<?php
namespace App\Repositories;
use Exception;
/**
* Manager Class to create Repository Objects that
* are located in App\Repositories\
*
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.tentakelfabrik.de/Tentakelfabrik/super-gear-directus
*
*/
class Manager
{
/**
* naming of Repository
* @var string
*/
const NAMESPACE = 'App\Repositories\\';
/**
* naming of Repository
* @var string
*/
const REPOSITORY_SUFFIX = 'Repository';
/**
* getting repository object
*
* @param string $repositoryClass
* @return AbstractRepository
*/
public static function get($repositoryName)
{
$repositoryClass = self::NAMESPACE.$repositoryName.self::REPOSITORY_SUFFIX;
if (!class_exists($repositoryClass)) {
throw new Exception('Repository Class '.$repositoryClass.' not exists!');
}
// create respository object
$repository = new $repositoryClass();
return $repository;
}
}

+ 83
- 0
app/Repositories/PostRepository.php View File

@ -0,0 +1,83 @@
<?php
namespace App\Repositories;
use App\Repositories\RepositoryAbstract;
/**
* request pages items from directus
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.tentakelfabrik.de/Tentakelfabrik/super-gear-directus
*
*/
class PostRepository extends RepositoryAbstract
{
/** endpoint */
protected $endpoint = 'posts';
/**
*
*/
public function find($limit = 20)
{
return $this->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();
}
}

+ 3
- 1
composer.json View File

@ -17,7 +17,9 @@
"vlucas/phpdotenv": "5.3.*", "vlucas/phpdotenv": "5.3.*",
"jenssegers/blade": "1.4.*", "jenssegers/blade": "1.4.*",
"tentakelfabrik/pirectus": "^0.1.3", "tentakelfabrik/pirectus": "^0.1.3",
"cocur/slugify": "^4.0"
"cocur/slugify": "^4.0",
"nesbot/carbon": "^2.54",
"mtownsend/read-time": "^2.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {


+ 66
- 1
composer.lock View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "5263bc5e77eb9849e010bbef6fa46c6f",
"content-hash": "a83db35a0da974bc28038b8ae4581142",
"packages": [ "packages": [
{ {
"name": "cocur/slugify", "name": "cocur/slugify",
@ -1253,6 +1253,71 @@
}, },
"time": "2021-04-05T01:36:59+00:00" "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", "name": "nesbot/carbon",
"version": "2.54.0", "version": "2.54.0",


+ 11
- 2
public/index.php View File

@ -9,11 +9,20 @@ $flight->route('GET /@slug:[a-z0-9\-]+/$', function() use ($flight) {
// getting position and build route // getting position and build route
$position = strlen($flight->request()->url) - 1; $position = strlen($flight->request()->url) - 1;
$route = substr_replace($flight->request()->url, '', $position, 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 /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->route('GET /(@slug:[a-z0-9\-]+$)', array(new App\Controllers\PageController, 'getAction'));
$flight->start(); $flight->start();
try {
$flight->start();
} catch (\Exception $exception) {
echo $exception->getMessage();
}

+ 0
- 2
resources/views/layout.blade.php View File

@ -2,8 +2,6 @@
@inject('siteRepository', 'App\Repositories\SiteRepository') @inject('siteRepository', 'App\Repositories\SiteRepository')
@inject('menuRepository', 'App\Repositories\MenuRepository') @inject('menuRepository', 'App\Repositories\MenuRepository')
@inject('markdownHelper', 'App\Helpers\MarkdownHelper')
@php @php
$site = $siteRepository->findOne(); $site = $siteRepository->findOne();
$menuItems = $menuRepository->findByName('main'); $menuItems = $menuRepository->findByName('main');


+ 53
- 0
resources/views/page/blog.blade.php View File

@ -0,0 +1,53 @@
@extends('layout')
{{-- pretend duplicate content --}}
@push('head')
<meta name="robots" content="noindex,follow" />
@endpush
{{-- inject helper for content & repositories --}}
@inject('markdownHelper', 'App\Helpers\MarkdownHelper')
@inject('postRepository', 'App\Repositories\PostRepository')
@php
$posts = $postRepository->find();
@endphp
@section('content')
<h1>
{{ $page['data']['title'] }}
</h1>
<div class="content">
{!! $markdownHelper->parse($page['data']['content']) !!}
</div>
@if (count($posts) > 0)
@foreach($posts['data'] as $post)
<a class="post" href="/blog/{{ $post['slug'] }}">
<header class="post__header">
<h2 class="post__title">
{{ $post['title'] }}
</h2>
@include('partials.date', ['post' => $post])
@include('partials.readtime', ['post' => $post])
@if (isset($post['media_teaser']['id']))
<div class="post__teaser">
<img src="{{ assetsUrl($post['media_teaser']['id']) }}" alt="{{ $post['media_teaser']['description'] }}" />
</div>
@endif
</header>
<div class="content post__lead">
{!! $markdownHelper->parse($post['lead']) !!}
</div>
</div>
@endforeach
@else
<div class="post">
<p>
Nothing!
</p>
</div>
@endif
@endsection

+ 5
- 0
resources/views/partials/date.blade.php View File

@ -0,0 +1,5 @@
<div class="post__date">
<time datetime="{{ $post['published_at'] }}">
{{ Carbon\Carbon::parse($post['published_at'])->diffForHumans() }}
</time>
</div>

+ 10
- 0
resources/views/partials/readtime.blade.php View File

@ -0,0 +1,10 @@
@php
$content = $post['lead'].$post['content'];
$readtime = (new \Mtownsend\ReadTime\ReadTime($content))->timeOnly(true)->setTranslation([
'minute' => ''
])->get();
@endphp
<div class="post__readtime">
Reading time {{ $readtime }} Minutes
</div>

+ 17
- 0
resources/views/post/default.blade.php View File

@ -0,0 +1,17 @@
@extends('layout')
@inject('markdownHelper', 'App\Helpers\MarkdownHelper')
@section('content')
<h1>
{{ $page['data']['title'] }}
</h1>
<div class="content content--lead">
{!! $markdownHelper->parse($page['data']['lead']) !!}
</div>
<div class="content">
{!! $markdownHelper->parse($page['data']['content']) !!}
</div>
@endsection

+ 38
- 0
resources/views/rss.blade.php View File

@ -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
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ $site['data']['title'] }}</title>
<atom:link href="{{ $base_url }}/feed" rel="self" type="application/rss+xml" />
<link>{{ $base_url }}/blog</link>
<description>{{ $site['data']['description'] }}</description>
<lastBuildDate>{{ date(DATE_RSS) }}</lastBuildDate>
<language>{{ $_ENV['APP_LOCALE'] }}-{{ strtoupper($_ENV['APP_LOCALE']) }}</language>
@foreach($posts['data'] as $post)
<item>
<title>{{ $post['title'] }}</title>
<link>{{ $base_url.'/blog/'.$post['slug'] }}</link>
<pubDate>{{ date(DATE_RSS, strtotime($post['published_at'])) }}</pubDate>
<description>
<![CDATA[
{!! $markdownHelper->parse($post['lead']) !!}
{!! $markdownHelper->parse($post['content']) !!}
]]>
</description>
<guid isPermaLink="false">{{ $post['slug'] }}</guid>
</item>
@endforeach
</channel>
</rss>

Loading…
Cancel
Save