Skip to content

Commit

Permalink
class registry added using Singleton Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Golub committed Nov 28, 2014
1 parent eb662fd commit fc345ca
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 121 deletions.
6 changes: 4 additions & 2 deletions app/base/Language/Language.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

class Language
{
public function __construct($_CONFIG)
private $config;

public function __construct($param)
{
$this->config = $_CONFIG;
$this->config = $param;
}

public function getString($str = 'all', $language = 'default')
Expand Down
12 changes: 0 additions & 12 deletions app/base/Security/Security.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,6 @@ public function validate($str, $mode)
break;
}
}

public function __construct($params = NULL)
{
if($params != NULL) {
// Setup what we need for this controller
// $sec = new Security;
foreach ($params as $key => $val)
{
$this->{$key} = $val;
}
}
}
}

?>
5 changes: 0 additions & 5 deletions app/base/Upload/Upload.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

class Upload
{
public function __construct($config)
{
$this->config = $config;
}

public function generate($file, $path, $rename = false)
{
$ext = explode(".", $file['name']);
Expand Down
2 changes: 1 addition & 1 deletion app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"enabled" => true,
"default" => "en"
),
"api" => "disabled"
"api" => false,
);

?>
12 changes: 1 addition & 11 deletions app/controllers/example.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,10 @@

class controller_example
{
public function __construct($params)
{
// Setup what we need for this controller
// $sec = new Security;
foreach ($params as $key => $val)
{
$this->{$key} = $val;
}
}

public function action_index()
{
$data = array();
$data['title'] = 'Codengine » Welcome :)';
$data['title'] = 'Codengine » Example';
View::forge("example/index", $data, false);
}
}
Expand Down
13 changes: 5 additions & 8 deletions app/controllers/welcome.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

class controller_welcome
{
public function __construct($params)
public function __construct()
{
// Setup what we need for this controller
// $sec = new Security;
foreach ($params as $key => $val)
{
$this->{$key} = $val;
}
$registry = Registry::getInstance();
foreach (reset($registry) as $key => $value) { $this->{$key} = $value; }
}

public function action_index()
Expand Down Expand Up @@ -60,7 +56,8 @@ public function action_index()
else
{
$data['title'] = 'Codengine » Welcome :)';
$data['strings'] = $this->language->getString('hi', 'default');
$registry = Registry::getInstance();
$data['strings'] = $registry->get('language')->getString('hi', 'default');
View::forge('welcome/index', $data);
}
}
Expand Down
65 changes: 65 additions & 0 deletions app/registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/*
* Codengine
* FilePath: /app/registy.php
*/

class Registry implements ArrayAccess
{
// basic setup
private static $instance = null;
private $registry = array();

public static function getInstance() {
if(self::$instance === null) {
self::$instance = new Registry();
}

return self::$instance;
}

private function __construct() {}
private function __clone() {}

// registry functions, set and get
public function set($key, $value) {
if (isset($this->registry[$key])) {
throw new Exception("There is already an entry for key " . $key);
}

$this->registry[$key] = $value;
}

public function get($key) {
if (!isset($this->registry[$key])) {
throw new Exception("There is no entry for key " . $key);
}

return $this->registry[$key];
}


// array access

public function offsetExists( $key ) {
return isset($this->registry[$key]);
}

public function offsetGet ( $key ) {
if(isset($this->registry[$key])) {
return $this->registry[$key];
}

return null;
}

public function offsetSet ( $key , $value ) {
return $this->registry[$key] = $value;
}

public function offsetUnset ( $key ) {
unset($this->registry[$key]);
}
}

?>
30 changes: 8 additions & 22 deletions app/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
array_unshift($params, "");
}

// push url parameters to the registry file
$registry->set("params", $params);

// if you have a user system you rely on you can deny access to all/specific controllers here.

// // check if user has credentials
Expand All @@ -40,29 +43,12 @@
// if($user !== true) { ......

// now we need to include the controller based on the url's first parameter (welcome)
if(in_array($params[1].".controller.php", $controllers))
if(is_array($controllers) && in_array($params[1].".controller.php", $controllers))
{
REQUIRE_ONCE 'controllers/'.$params[1].".controller.php";

// default classes to pass to the controller
$load = array(
"sec" => $Sec, // security
"db" => $DB, // database
"params" => $params, // url parameters
);

// additional classes to pass to the controller only if they are enabled in /app/config.php
if($_CONFIG['language']['enabled'] == true)
$load['language'] = $Language;
if($_CONFIG['upload']['enabled'] === true)
$load['upload'] = $Upload;
if($_CONFIG['api'] == 'enabled')
$load['api'] = $API;

// then run the controller and initiate action_index
$name = "controller_".$params[1];
$$name = new $name($load);
$$name->action_index();
require_once 'app/controllers/'.$params[1].".controller.php";
$new = 'controller_'.$params[1];
$registry->set($params[1], new $new);
$registry->get($params[1])->action_index();
}

else // 404 - controller not found - redirect to home page
Expand Down
50 changes: 0 additions & 50 deletions app/start.php

This file was deleted.

14 changes: 5 additions & 9 deletions engine
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@ var default_controller = "<?php\n\
\n\
class controller_"+arguments[2]+"\n\
{\n\
public function __construct($params)\n\
{\n\
// Setup what we need for this controller\n\
// $sec = new Security;\n\
foreach ($params as $key => $val)\n\
{\n\
$this->{$key} = $val;\n\
}\n\
}\n\
public function __construct()\n\
{\n\
$registry = Registry::getInstance();\n\
foreach (reset($registry) as $key => $value) { $this->{$key} = $value; }\n\
}\n\
\n\
public function action_index()\n\
{\n\
Expand Down
50 changes: 49 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,54 @@

session_start();

REQUIRE_ONCE 'app/start.php';
require_once 'app/registry.php';

try {
// get the instance of the registry
$registry = Registry::getInstance();

// ... load the Codengine configuration and extensions
require_once 'app/config.php';
$registry->set("config", $_CONFIG);
require_once 'app/base/View/View.base.php';
$registry->set("view", $_CONFIG);

// than execute all other Codengine extenstions
$_CONFIG = $registry->get("config");
$_CONFIG = $registry->get("view");

// basic classes
require_once 'app/base/Security/Security.base.php';
$registry->set("sec", new Security()); // security

if($_CONFIG['db']['enabled'] === true) { // load db if enabled
require_once 'app/base/Database/Database.base.php';
$registry->set("db", new Database($_CONFIG['db']['hostname'], $_CONFIG['db']['username'], $_CONFIG['db']['password'], $_CONFIG['db']['dbname']));
}

if($_CONFIG['upload']['enabled'] === true) { // load file uploading if enabled
require_once 'app/base/Upload/Upload.base.php';
$registry->set("upload", new Upload());
}

if($_CONFIG['language']['enabled'] === true) { // load language stacks if enabled
require_once 'app/base/Language/Language.base.php';
$registry->set("language", new Language($_CONFIG));
}

if($_CONFIG['api'] === true || $_CONFIG['api'] == 'enabled') { // load api communication if enabled. 'enabled' for backward compatibility.
require_once 'app/base/API/API.base.php';
$registry->set("api", new API());
}

$controllers = array_filter(scandir('app/controllers'), function($item) {
return !is_dir('app/controllers/' . $item);
});

// now load the routing system
require_once 'app/route.php';
} catch(Exception $e) {
echo $e->getMessage();
}

?>

0 comments on commit fc345ca

Please sign in to comment.