Skip to content

Commit

Permalink
Assume if the ControllerName.php exists it matches /name/ routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
svandragt committed Oct 1, 2020
1 parent c5b96c1 commit ad62b75
Show file tree
Hide file tree
Showing 22 changed files with 39 additions and 75 deletions.
30 changes: 0 additions & 30 deletions src/application/App.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/application/ControllerArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ControllerArchive extends Controller
*/
public function records()
{
$content_dir = Configuration::CONTENT_FOLDER . '/posts';
$content_dir = Configuration::CONTENT_FOLDER . '/post';
$Files = new Files($content_dir, $this->ext);
$this->records = $Files->files();
}
Expand Down
2 changes: 1 addition & 1 deletion src/application/ControllerFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ControllerFeed extends Controller
public function records()
{
$limit = Configuration::POSTS_HOMEPAGE;
$content_dir = Configuration::CONTENT_FOLDER . '/posts';
$content_dir = Configuration::CONTENT_FOLDER . '/post';
$Records = new Files($content_dir, $this->ext);
$this->records = $Records->limit($limit + 5);
}
Expand Down
2 changes: 1 addition & 1 deletion src/application/ControllerHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ControllerHome extends Controller
public function records()
{
$limit = Configuration::POSTS_HOMEPAGE;
$content_dir = Configuration::CONTENT_FOLDER . '/posts';
$content_dir = Configuration::CONTENT_FOLDER . '/post';
$Files = new Files($content_dir, $this->ext);
$this->records = $Files->limit($limit + 5);
}
Expand Down
5 changes: 2 additions & 3 deletions src/application/ControllerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

// single page
Expand All @@ -15,7 +14,7 @@ class ControllerPage extends Controller
*/
public function records()
{
$content_dir = Configuration::CONTENT_FOLDER . '/pages/';
$content_dir = Configuration::CONTENT_FOLDER . '/page/';
$path = $content_dir . implode('/', $this->args) . '.' . $this->ext;
$this->records = [ $path ];
}
Expand All @@ -37,7 +36,7 @@ public function view()

$this->View = new Html($this->Model->contents, array(
'layout' => 'layout.php',
'controller' => 'pages',
'controller' => 'page',
'model' => 'page',
));
}
Expand Down
4 changes: 2 additions & 2 deletions src/application/ControllerPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ControllerPost extends Controller
*/
public function records()
{
$content_dir = Configuration::CONTENT_FOLDER . '/posts/';
$content_dir = Configuration::CONTENT_FOLDER . '/post/';
$path = $content_dir . implode('/', $this->args) . '.' . $this->ext;
$this->records = [ $path ];
}
Expand All @@ -37,7 +37,7 @@ public function view()

$this->View = new Html($this->Model->contents, array(
'layout' => 'layout.php',
'controller' => 'posts',
'controller' => 'post',
'model' => 'post',
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
require '../vendor/autoload.php';
require '../Configuration.php';

$App = new Blog\App();
$App = new Cuttlefish\App();
$App->run();
4 changes: 2 additions & 2 deletions src/system/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public function __construct()
$this->Security = new Security();
}

public function run(array $routes)
public function run()
{
if ($this->Cache->is_cached) {
return;
}

// Process request if not statically cached.
$this->Cache->start();
new Router($routes);
new Router();
$this->Cache->end();
}
}
8 changes: 4 additions & 4 deletions src/system/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function generateSite(): string
$content = Configuration::CONTENT_FOLDER;
$ext = Configuration::CONTENT_EXT;
$Curl = new Curl();
$Files = new Files($content, $ext );
$Files = new Files($content, $ext);

$cache_urls = array();

Expand All @@ -183,7 +183,7 @@ public function generateSite(): string

$urls = array(
'/',
'/feeds/posts',
'/feeds/post',
'/archive',
);
foreach ($urls as $path) {
Expand Down Expand Up @@ -219,7 +219,7 @@ public function clear()
global $App;
$dir = $this->getCacheFolder();
$output = sprintf('Removing all files in %s<br>', $dir);
$Files = new Files($dir );
$Files = new Files($dir);
$output .= $Files->removeAll();
$dirs = Filesystem::subdirs(realpath($dir . '/.'), false);
foreach ($dirs as $dir) {
Expand Down Expand Up @@ -251,7 +251,7 @@ protected function getCacheFolder()
protected function copyThemeFiles($file_types)
{

include_once( 'helpers.php' );
include_once('helpers.php');
$output = 'Copying files from theme: <br><br>';

foreach ($file_types as $file_type) {
Expand Down
2 changes: 1 addition & 1 deletion src/system/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public function model()
*/
public function view()
{
include_once( 'helpers.php' );
include_once('helpers.php');
}
}
1 change: 0 additions & 1 deletion src/system/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class Defaults
{

public const SITE_MOTTO = 'Something clever for the internets';
public const SITE_TITLE = 'Your site';
public const THEME = 'basic';
Expand Down
6 changes: 1 addition & 5 deletions src/system/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ protected function isNewInstall(): bool

protected function createSystemFolders(): void
{
$cfg_content_folder = Configuration::CONTENT_FOLDER;

$folders = array(
Configuration::LOGS_FOLDER,
Configuration::CACHE_FOLDER,
$cfg_content_folder . '/pages',
$cfg_content_folder . '/posts',
$cfg_content_folder . '/errors',
Configuration::THEMES_FOLDER,
Configuration::CONTENT_FOLDER
);
$ok = null;
foreach ($folders as $folder) {
Expand Down
6 changes: 3 additions & 3 deletions src/system/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Files
public function __construct($dir_or_path, $ext = null)
{
if (empty($dir_or_path)) {
$this->files = null;
return;
$this->files = null;
return;
}

$files = $this->collect(realpath($dir_or_path), $ext);
Expand All @@ -37,7 +37,7 @@ protected function collect(string $dir = ".", $filter = null): array
// get files
if ($handle = opendir($dir)) {
while (false !== ( $file = readdir($handle) )) {
if ( $file !== "." && $file !== "..") {
if ($file !== "." && $file !== "..") {
$file_path = $dir . DIRECTORY_SEPARATOR . $file;
if (is_dir($file_path)) {
$dir_files = $this->collect($file_path, $filter);
Expand Down
5 changes: 3 additions & 2 deletions src/system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ public function section(string $content_section, $section_key): StdClass
}


public function validate($Content) {
public function validate($Content)
{
foreach ($this->required_fields as $section => $fields) {
if (!property_exists($Content, $section)) {
if (!property_exists($Content, $section)) {
throw new Exception("Required section '$section' missing in content.");
}
foreach ($fields as $field) {
Expand Down
21 changes: 10 additions & 11 deletions src/system/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ class Router
{
protected $Controller;

public function __construct($routes)
public function __construct()
{

// Route to controller
$args = explode("/", $this->pathInfo());
if (isset($routes[$args[1]])) {
$controller_class = $routes[$args[1]];
}
$controller_class = 'Cuttlefish\Blog\Controller' . ucfirst($args[1]);

$controller_arguments = array_slice($args, 2);
if (class_exists($controller_class, true)) {
$this->Controller = new $controller_class($this, $controller_arguments);
Expand Down Expand Up @@ -87,11 +85,12 @@ protected function redirect($Url, string $log_message): void
exit($log_message);
}

protected function matchRoute($pattern, $routes) {
$keys = array_flip($routes);
$matches = preg_grep($pattern,$keys);
if ($matches) {
return array_shift($matches);
}
protected function matchRoute($pattern, $routes)
{
$keys = array_flip($routes);
$matches = preg_grep($pattern, $keys);
if ($matches) {
return array_shift($matches);
}
}
}
2 changes: 1 addition & 1 deletion src/system/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __get($name)

public function render(): void
{
$path = BASE_FILEPATH . trim(theme_dir(),'/') . DIRECTORY_SEPARATOR . $this->file;
$path = BASE_FILEPATH . trim(theme_dir(), '/') . DIRECTORY_SEPARATOR . $this->file;
require $path;
}
}
2 changes: 1 addition & 1 deletion src/system/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function convertFileToURL($file_object): self
], '', $url);
}

Log::debug(__FUNCTION__ . " relative_url: $relative_url");
Log::debug(__FUNCTION__ . " relative_url: $relative_url");
$this->setUrl($relative_url);

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/system/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
function theme_dir($filename = '')
{
$theme_folder = Configuration::THEMES_FOLDER . DIRECTORY_SEPARATOR . Configuration::THEME . DIRECTORY_SEPARATOR;
$theme_folder = Configuration::THEMES_FOLDER . DIRECTORY_SEPARATOR . Configuration::THEME . DIRECTORY_SEPARATOR;

return BASE_PATH . str_replace("\\", "/", $theme_folder) . $filename;
return BASE_PATH . str_replace("\\", "/", $theme_folder) . $filename;
}

// put functions specific to your application here. You can call these from the view templates.
Expand Down
2 changes: 1 addition & 1 deletion src/themes/basic/errors.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

// Error pages use page layout
include __DIR__ . DIRECTORY_SEPARATOR . "pages.php";
include __DIR__ . DIRECTORY_SEPARATOR;
4 changes: 2 additions & 2 deletions src/themes/basic/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ function href($internal_url)
function pages()
{
$output = '';
$pages_path = Configuration::CONTENT_FOLDER .'/pages';
$pages_path = Configuration::CONTENT_FOLDER . '/page';

$Files = new Cuttlefish\Files($pages_path, Configuration::CONTENT_EXT);

foreach ($Files->files() as $path) {
$filename = pathinfo($path, PATHINFO_FILENAME);
$title = ucwords(str_replace("-", " ", $filename));
$output .= sprintf("<li><a href='%s'>%s</a></li>", href("/pages/$filename"), $title);
$output .= sprintf("<li><a href='%s'>%s</a></li>", href("/page/$filename"), $title);
}

return $output;
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit ad62b75

Please sign in to comment.