Commit e60a3a29 authored by Webserver User's avatar Webserver User

rebuilding PageCreator

parent be239af7
......@@ -7,6 +7,9 @@ Perhaps the simplest and easiest PHP MVC framework in the world.
* strict separation between code and view
* supports composer, just load what you need
* page creator, create pages with controller and view fully automatically and iteratively
* routing & seo friendly urls
* high scalable
* for free
# next features
* MYSQL Adapter based on PDO
......
......@@ -16,8 +16,8 @@
"autoload": {
"psr-4": {
"HM\\":"models/",
"simple\\":"simplemvc/",
"simpleController\\":"controller/"
"simple\\":"simplemvc/",
"simpleController\\":"controllers/"
}
}
}
......@@ -11,16 +11,42 @@ class indexController {
}
public function index($page) {
public function indexAction($page) {
$page->title = 'simple-mvc';
$page->template('default');
$page->title = 'simple-mvc';
$page->template('default');
/*
$array = array("header" => array(),
'config' => array(),
"sitemap" => array());
$json = json_encode($array);
*/
#$manifest = (new HM\File( dirname(__DIR__) . '/simplemvc/' . 'manifest.json'))->getJson();
#$manifest = $manifestFile;
#var_dump($manifest->sitemap);
#$return = $manifest->create();
// create a new page with simple\Page -- blog/list/article/comments
$newPage = (new simple\Page('warehouses/specials'))->add();
#$this->out = $newPage;
// or set path
#$newPage->setPath('');
#$manifest = new simple\Manifest();
#$manifest->build();
#$this->view = $manifest->get();
// send $this to the view
$this->view = "Hi, i am a view object";
$this->user = "I am a user!";
$this->dir = new HM\Dir('/path_to/MyNewDirectory');
//$this->view = "Hi, i am a view object";
#$this->user = "I am a user!";
#$this->dir = new HM\Dir('/path_to/MyNewDirectory');
/* How to create a new page
* create a new page included controller and views
......@@ -28,13 +54,13 @@ class indexController {
* first step, define the page / url
*
*/
$url = "/blog/article";
#$url = "/blog/article/list";
/*
* second step, build a MVC object with the uri as parameter
* !Note: do not use the variable $page to create new pages, this will overwrite the current object
*/
$newPage = new simple\MVC($url);
#$newPage = new simple\MVC($url);
//new simple\Page($url);
/*
* third step, create page
* this will generated follow files:
......@@ -55,7 +81,43 @@ class indexController {
* Congratulations, thats all :-)
*
*/
}
public function moreOfIndexActionAction ($page) {
$page->title = 'simple-mvc';
$page->template('default');
}
// send $this to the view
$this->view = "Hi, i am a view object";
$this->user = "I am a user!";
$this->dir = new HM\Dir('/path_to/MyNewDirectory');
/* How to create a new page
* create a new page included controller and views
*
* first step, define the page / url
*
*/
$url = "/index/item";
/*
* second step, build a MVC object with the uri as parameter
* !Note: do not use the variable $page to create new pages, this will overwrite the current object
*/
$newPage = new simple\MVC($url);
/*
* third step, create page
* this will generated follow files:
*
* /controller/blog.controller.php
* /render/views/blog.view.php (maybe for the list of all articles)
* /render/views/blog/article.view.php (a single blog post)
*
*/
#$newPage->create();
}
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ class notFoundController {
}
public function notFound($page) {
public function notFoundAction($page) {
$page->title = 'simple-mvc';
......
......@@ -3,12 +3,18 @@ require __DIR__ . '/globals/header.php';
require __DIR__ . '/globals/settings.php';
require __DIR__ . '/vendor/autoload.php';
$page = new simple\MVC($_SERVER["SCRIPT_URL"]);
if ($base_url = dirname($_SERVER["SCRIPT_NAME"]) !== '/') {
$app_route = str_replace($base_url, '', $_SERVER["REQUEST_URI"]);
} else {
$app_route = $_SERVER["REQUEST_URI"];
}
$page = new simple\MVC($app_route);
# show info about the $page object
#var_dump($page);
if ($page->view->path === false) : header('Location: '.'/notFound'); endif;
#if ($page->view->path === false) : header('Location: '.'/notFound'); endif;
// build view controller and call the page action
$controller = $page->controller->namespace . $page->controller->name;
......@@ -22,3 +28,5 @@ $view->$action($page);
require($page->template);
......@@ -50,14 +50,54 @@ namespace HM;
* Build object
*
* @param -
* @desc check if dir exists
* @desc write in a file (overwrite)
*/
public function write($content)
{
if (file_put_contents($this->path, $content)):
if (file_put_contents($this->path, $content, LOCK_EX)):
return true;
endif;
return false;
}
/**
* Build object
*
* @param -
* @desc write in a file (appends)
*/
public function append($content)
{
if (file_put_contents($this->path, $content, FILE_APPEND | LOCK_EX)):
return true;
endif;
return false;
}
/*
* getContent
*
* params -
* return $content (string)
*
*/
public function getContent(){
return file_get_contents($this->path);
}
/*
* getContent
*
* @description
* get content from files in the src directory
*
* @param
* path (string)
* filename (string)
*
* @return
* content (string)
*
*/
public function getJson($assoc = false) {
return json_decode($this->getContent($this->path), $assoc);
}
}
\ No newline at end of file
<?
namespace simple;
use HM;
class Controller {
/**
* Generate page with controller and view
*
* @description
* https://gitlab.com/mascoda/simple-mvc/wikis/home
*
*/
public $name;
public $filename;
public $filepath;
public $actions;
/*
* Constructor
*
* @description
* build the complete page with controller and view
*
* @param
* page (string)
* template (string) [optional]
*
* @return
* -
*
*/
function __construct($path)
{
$this->setName($path);
$this->setFilename();
$this->setFilepath();
}
/*
* @description
* set name of controller
*/
private function setName($path) {
$path = strtolower($path);
if (strpos('/', $path) !== false) {
$this->name = $path;
} else {
$name = explode("/" ,$path);
$name = array_shift($name);
$this->name = $name;
}
}
/*
* @description
* returned the filename
*/
public function setFilename() {
$this->filename = $this->name . '.controller.php';
}
/*
* @description
* returned the filepath
*/
public function setFilepath() {
$this->filepath = dirname(__DIR__) . '/controllers/' . $this->name . '.controller.php';
}
/*
* @description
* returned the filepath
*/
public function exists() {
return $exists = realpath($this->filepath) ? TRUE : FALSE;
}
/*
* @description
* returned the Actions (array)
*/
public function getAction($viewname) {
return $viewname . 'Action';
}
/*
* @description
* returned the Actions (array)
*/
public function getNamespace() {
return 'simpleController\\';
}
/*
* @description
* create controller file
*/
public function createFile() {
// create a new file object
$file = new HM\File(dirname(__DIR__) . '/controllers/' . $this->name .'.controller.php');
// load default template
$template = SRC::getContent('controller', 'default.controller.php');
// search and replace placeholder in default template
$search = array('{controllerName}', '{actionName}');
$replace = array($this->name . 'Controller', $this->name."Action");
$template = str_replace($search, $replace, $template);
$file->create(0775);
$file->write($template);
}
/*
* @description
* create controller action in controller file
*/
public function createAction($action) {
// get controller as file
$controller = new HM\File($this->filepath);
// get file as content
$fileContent = ($controller->getContent());
// if Action not exist, create
if (strpos($fileContent, $action) === false) :
$template = SRC::getContent('action', 'defaultAction.php');
$template = str_replace('{actionName}', $action, $template);
$fileContent = trim(substr($fileContent, 0, -1));
$controller->write($fileContent);
$controller->append($template);
endif;
}
}
\ No newline at end of file
......@@ -10,14 +10,14 @@ use HM;
* https://gitlab.com/mascoda/simple-mvc/wikis/home
*
*/
public $page;
public $page;
public $controller;
public $method;
public $method;
public $template;
public $view;
public $title;
/*
* Constructor
......@@ -35,12 +35,12 @@ use HM;
*/
function __construct($page, $template = 'default')
{
$this->page($page);
$this->page($page);
$this->getController();
$this->template($template);
$this->view();
}
/*
* Page
......@@ -58,9 +58,9 @@ use HM;
private function page($page)
{
# define page from the uri
$page = ltrim(rtrim($page, '/'), '/');
$this->page = ($page === '') ? 'index' : $page;
# define page from the uri
$page = ltrim(rtrim($page, '/'), '/');
$this->page = ($page === '') ? 'index' : $page;
}
/**
......@@ -80,22 +80,22 @@ use HM;
private function getController()
{
# define the first directory from path as controller
$action = array_filter(explode('/', $this->page));
$action = end($action);
$controller = array_filter(explode('/', $this->page));
$controller = array_shift($controller);
$this->controller = (object)
array(
'path' => realpath(dirname(__DIR__) . '/controller/' . $controller . '.controller.php'),
'file' => $controller . '.controller.php',
'name' => $controller . 'Controller',
'action' => $action,
'namespace' => 'simpleController\\'
);
# define the first directory from path as controller
$action = array_filter(explode('/', $this->page));
$action = end($action).'Action';
$controller = array_filter(explode('/', $this->page));
$controller = array_shift($controller);
$this->controller = (object)
array(
'path' => realpath(dirname(__DIR__) . '/controllers/' . $controller . '.controller.php'),
'file' => $controller . '.controller.php',
'name' => $controller . 'Controller',
'action' => $action,
'namespace' => 'simpleController\\'
);
}
/**
* View
......@@ -117,12 +117,18 @@ use HM;
*/
private function view()
{
$this->view = (object)
array(
'path' => realpath(dirname(__DIR__) . '/views/' . $this->page . '.view.php'),
'file' => $this->page . '.view.php',
'name' => $this->page
);
$dir = array_filter(explode('/', $this->page));
$dir = array_pop($dir);
$this->view = (object)
array(
'path' => realpath(dirname(__DIR__) . '/views/' . $this->page . '/' . $dir . '.view.php'),
'file' => $this->page . '.view.php',
'name' => $this->page
);
}
/**
* View
......@@ -140,73 +146,8 @@ use HM;
*/
public function template($template)
{
# create the template
$this->template = dirname(__DIR__) . '/public/templates/' . $template .'/' . $template . '.template.php';
}
/**
* Create a page
*
* @description
* Create a full page included controller and view by a path
* $page->create('/catalog/product/') will be create files in
* /controller/catalog.controller.php
* /render/views/catalog/catalog.view.php
* /render/views/catalog/product/product.view.php
*
* @param
* page (string)
* @desc -
*
* @return
* -
*
*/
public function create()
{
// need for subdirectories
$path_to = '';
// get pages of the given path (removed slashes)
$pages = explode('/', rtrim(ltrim($this->page, '/'),'/'));
// create controller
$controller = new HM\File(dirname(__DIR__) . '/controller/' . $pages[0] .'.controller.php');
if (!$controller->exists()) :
$defaultController = SRC::getContent('controller', 'default.controller.php');
$search = array('{controllerName}', '{actionName}');
$replace = array($pages[0] . 'Controller', $pages[0]);
$defaultController = str_replace($search, $replace, $defaultController);
$controller->write($defaultController);
endif;
// create directories for the views recursively
$viewDirectory = new HM\Dir(dirname(dirname(__DIR__) . '/views/' . $this->page));
if (!$viewDirectory->exists()) :
$viewDirectory->create($mode = 0775, $recursive = true);
endif;
// created controller and views
foreach ($pages as $page) :
// init a new page
$page = new MVC($page);
// create views
$view = new HM\File(dirname(__DIR__) . '/views/' . $path_to . $page->page . '.view.php');
if (!$view->exists()) :
$view->create($mode = 0755);
$view->write(SRC::getContent('views', 'default.view.php'));
endif;
// extend the path for viewfiles
$path_to .= $page->page . '/';
endforeach;
// replace placeholder
#$file->write(str_replace('{TITLE}' , basename($file->path), file_get_contents(dirname(__DIR__) . '/render/template/default.view.php')));
}
}
\ No newline at end of file
<?
namespace simple;
use HM;
use stdClass;
class Manifest {
/**
* Generate page with controller and view
*
* @description
* https://gitlab.com/mascoda/simple-mvc/wikis/home
*
*/
public $path;
public $manifest;
/*
* Constructor
*
* @description
* build the complete page with controller and view
*
* @param
* page (string)
* template (string) [optional]
*
* @return
* -
*
*/
function __construct()
{
$this->path = dirname(__DIR__) . '/simplemvc/' . 'manifest.json';
}
/**
* @param
* page (string)
* @desc -
*
* @return
* -
*
*/
public function get() {
return (new HM\File( $this->path))->getJson(TRUE);
}
/**
* Create pages from manifest
*
* @description
* Create a full page included controller and view by a path
* $page->create('/catalog/product/') will be create files in
* /controller/catalog.controller.php
* /render/views/catalog/catalog.view.php
* /render/views/catalog/product/product.view.php
*
* @param
* page (string)
* @desc -
*
* @return
* -
*
*/
public function build() {
$manifest = $this->get();
$this->createPages($manifest['pages']);
}
/**
* @param
* page (string)
* @desc -
*
* @return
* -
*
*/
public function createPages($pages) {
foreach ($pages as $page) :
// create controllers
$controller = new Controller($page['controller']['name']);
// create controller files
if ($controller->exists() === false) :
$controller->createFile();
endif;
// create views
$view = new View($page['view']['path']);
// create view directories
$viewDirectory = new HM\Dir(dirname(__DIR__) . '/views/' . $view->path);
if (!$viewDirectory->exists()) :
$viewDirectory->create($mode = 0775, $recursive = true);
endif;
// create view files
if ($view->exists() === false) :
$view->createFile();
endif;
// create action by view in controller
$controller->createAction($view->action);
// if subpages exists
if (is_array($page['sub'])) :
$this->createPages($page['sub']);
endif;
endforeach;
}
}
\ No newline at end of file
<?
namespace simple;
use HM;
use stdClass;
class Page {
/**
* Generate page with controller and view
*
* @description
* https://gitlab.com/mascoda/simple-mvc/wikis/home
*
*/
public $path;
/*
* Constructor
*
* @description
* build the complete page with controller and view
*
* @param
* page (string)
* template (string) [optional]
*
* @return
* -
*
*/
function __construct($path)
{
$this->path = ltrim(rtrim($path, '/'), '/');
}
/*
* Page
*
* @description
* create page, if it does not exist, move on to 404
*
* @param
* page (string)
*
* @return
* -
*
*/
public function add()
{
$manifestFile = (new HM\File( __DIR__ . '/' . 'manifest.json'));
$manifest = $manifestFile->getJson(TRUE);
$newPages = array();
$path_to = '';
$forPage = array();
$uri = explode('/' , $this->path);
foreach (array_reverse(explode('/' , $this->path)) as $page) :
$controller = new Controller($this->path);
$view = new View($path = implode('/',$uri));
if (!$view->exists()) :
$newPage = array($page =>
array(
'controller' =>
array(
'name' => $controller->name,
'action' => $controller->getAction($view