Skip to content

Commit

Permalink
Reintegrate blog
Browse files Browse the repository at this point in the history
  • Loading branch information
svandragt committed Aug 23, 2020
1 parent fb1691b commit 83bf54b
Show file tree
Hide file tree
Showing 16 changed files with 537 additions and 19 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"autoload": {
"psr-4": {
"Cuttlefish\\": "src"
"Cuttlefish\\": "src",
"Cuttlefish\\Blog\\": "src/blog"
}
},
"config": {
Expand All @@ -35,7 +36,7 @@
],
"prep": [
"phpcbf",
"psalm --alter --issues=all",
"psalm --alter",
"@lint"
],
"lint": [
Expand All @@ -44,4 +45,4 @@
"phpcs"
]
}
}
}
36 changes: 23 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions src/blog/ControllerAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Cuttlefish\Blog;

use Cuttlefish\Controller;
use Cuttlefish\Html;
use Cuttlefish\App;

class ControllerAdmin extends Controller
{
public array $allowed_methods = array(
'index' => 'Overview',
'clearCache' => 'Clear cache',
'generateSite' => 'Generate static site',
'logout' => 'Logout',
);

// admin section does not use content files
protected string $contents;

protected function isAllowedMethod($action): bool
{
return array_key_exists($action, $this->allowed_methods);
}

protected function showTasks(): string
{
$output = '<ul>';
$am = $this->allowed_methods;
array_shift($am);
foreach ($am as $key => $value) :
$Url = new Cuttlefish\Url("/admin/$key");
$output .= sprintf('<li><a href="%s">%s</a></li>', $Url->url_absolute, $value);
endforeach;

$output .= '</ul>';

return $output;
}

protected function showLogin(): string
{
return App::getInstance()->Security->login();
}

protected function clearCache()
{
$App = App::getInstance();
$App->Security->maybeLoginRedirect();

return $App->Cache->clear();
}

protected function generateSite(): void
{
$App = App::getInstance();
$App->Security->maybeLoginRedirect();
echo $App->Cache->generateSite();
}


/**
* @return void
*/
public function init()
{
App::getInstance()->Cache->abort();

$action = ( isset($this->args[0]) ) ? $this->args[0] : 'index';
if ($this->isAllowedMethod($action)) {
$this->contents = $this->$action();
} else {
exit("Method $action is not allowed");
}

parent::init();
}

/**
* @return void
*/
public function view()
{
parent::view();

$this->View = new Html($this->contents, array(
'layout' => 'layout.php',
'controller' => 'admin',
'model' => 'page',
));
}

public function index(): string
{
if (App::getInstance()->Security->isLoggedIn()) {
return $this->showTasks();
}

return $this->showLogin();
}

public function logout()
{
$App = App::getInstance();
$App->Security->maybeLoginRedirect();

return $App->Security->logout();
}
}
42 changes: 42 additions & 0 deletions src/blog/ControllerArchive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Cuttlefish\Blog;

use Configuration;
use Cuttlefish\Controller;
use Cuttlefish\Files;
use Cuttlefish\Html;

class ControllerArchive extends Controller
{
/**
* @return void
*/
public function records()
{
$content_dir = Configuration::CONTENT_FOLDER . '/posts';
$Files = new Files(array( 'url' => $content_dir ), $this->ext);
$this->records = $Files->files();
}

/**
* @return void
*/
public function model()
{
$this->Model = new ModelPost($this->records);
}

/**
* @return void
*/
public function view()
{
parent::view();
$this->View = new Html($this->Model->contents, array(
'layout' => 'layout.php',
'controller' => 'archive',
'model' => 'post',
));
}
}
45 changes: 45 additions & 0 deletions src/blog/ControllerError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Cuttlefish\Blog;

use Configuration;
use Cuttlefish\Controller;
use Cuttlefish\Filesystem;
use Cuttlefish\Html;

class ControllerError extends Controller
{
// single errors page

/**
* @return void
*/
public function records()
{
$content_dir = Configuration::CONTENT_FOLDER . '/errors/';
$url = $content_dir . implode("/", $this->args) . '.' . $this->ext;
$this->records = [ Filesystem::convertUrlToPath($url) ];
}

/**
* @return void
*/
public function model()
{
$this->Model = new ModelPage($this->records);
}

/**
* @return void
*/
public function view()
{
parent::view();

$this->View = new Html($this->Model->contents, array(
'layout' => 'layout.php',
'controller' => 'errors',
'model' => 'page',
));
}
}
41 changes: 41 additions & 0 deletions src/blog/ControllerFeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Cuttlefish\Blog;

use Configuration;
use Cuttlefish\Controller;
use Cuttlefish\Feed;
use Cuttlefish\Files;

class ControllerFeed extends Controller
{
// single feed
/**
* @return void
*/
public function records()
{
$limit = Configuration::POSTS_HOMEPAGE;
$content_dir = Configuration::CONTENT_FOLDER . '/posts';
$Records = new Files(array( 'url' => $content_dir ), $this->ext);
$this->records = $Records->limit($limit + 5);
}

/**
* @return void
*/
public function model()
{
$Model = new ModelPost($this->records);
$this->Model = $Model->limit(10);
}

/**
* @return void
*/
public function view()
{
parent::view();
$this->View = new Feed($this->Model->contents);
}
}
Loading

0 comments on commit 83bf54b

Please sign in to comment.