Skip to content

Commit fc345ca

Browse files
author
Daniel Golub
committed
class registry added using Singleton Pattern
1 parent eb662fd commit fc345ca

11 files changed

Lines changed: 138 additions & 121 deletions

File tree

app/base/Language/Language.base.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
class Language
88
{
9-
public function __construct($_CONFIG)
9+
private $config;
10+
11+
public function __construct($param)
1012
{
11-
$this->config = $_CONFIG;
13+
$this->config = $param;
1214
}
1315

1416
public function getString($str = 'all', $language = 'default')

app/base/Security/Security.base.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,6 @@ public function validate($str, $mode)
142142
break;
143143
}
144144
}
145-
146-
public function __construct($params = NULL)
147-
{
148-
if($params != NULL) {
149-
// Setup what we need for this controller
150-
// $sec = new Security;
151-
foreach ($params as $key => $val)
152-
{
153-
$this->{$key} = $val;
154-
}
155-
}
156-
}
157145
}
158146

159147
?>

app/base/Upload/Upload.base.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
class Upload
88
{
9-
public function __construct($config)
10-
{
11-
$this->config = $config;
12-
}
13-
149
public function generate($file, $path, $rename = false)
1510
{
1611
$ext = explode(".", $file['name']);

app/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"enabled" => true,
3535
"default" => "en"
3636
),
37-
"api" => "disabled"
37+
"api" => false,
3838
);
3939

4040
?>

app/controllers/example.controller.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@
66

77
class controller_example
88
{
9-
public function __construct($params)
10-
{
11-
// Setup what we need for this controller
12-
// $sec = new Security;
13-
foreach ($params as $key => $val)
14-
{
15-
$this->{$key} = $val;
16-
}
17-
}
18-
199
public function action_index()
2010
{
2111
$data = array();
22-
$data['title'] = 'Codengine » Welcome :)';
12+
$data['title'] = 'Codengine » Example';
2313
View::forge("example/index", $data, false);
2414
}
2515
}

app/controllers/welcome.controller.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66

77
class controller_welcome
88
{
9-
public function __construct($params)
9+
public function __construct()
1010
{
11-
// Setup what we need for this controller
12-
// $sec = new Security;
13-
foreach ($params as $key => $val)
14-
{
15-
$this->{$key} = $val;
16-
}
11+
$registry = Registry::getInstance();
12+
foreach (reset($registry) as $key => $value) { $this->{$key} = $value; }
1713
}
1814

1915
public function action_index()
@@ -60,7 +56,8 @@ public function action_index()
6056
else
6157
{
6258
$data['title'] = 'Codengine » Welcome :)';
63-
$data['strings'] = $this->language->getString('hi', 'default');
59+
$registry = Registry::getInstance();
60+
$data['strings'] = $registry->get('language')->getString('hi', 'default');
6461
View::forge('welcome/index', $data);
6562
}
6663
}

app/registry.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/*
3+
* Codengine
4+
* FilePath: /app/registy.php
5+
*/
6+
7+
class Registry implements ArrayAccess
8+
{
9+
// basic setup
10+
private static $instance = null;
11+
private $registry = array();
12+
13+
public static function getInstance() {
14+
if(self::$instance === null) {
15+
self::$instance = new Registry();
16+
}
17+
18+
return self::$instance;
19+
}
20+
21+
private function __construct() {}
22+
private function __clone() {}
23+
24+
// registry functions, set and get
25+
public function set($key, $value) {
26+
if (isset($this->registry[$key])) {
27+
throw new Exception("There is already an entry for key " . $key);
28+
}
29+
30+
$this->registry[$key] = $value;
31+
}
32+
33+
public function get($key) {
34+
if (!isset($this->registry[$key])) {
35+
throw new Exception("There is no entry for key " . $key);
36+
}
37+
38+
return $this->registry[$key];
39+
}
40+
41+
42+
// array access
43+
44+
public function offsetExists( $key ) {
45+
return isset($this->registry[$key]);
46+
}
47+
48+
public function offsetGet ( $key ) {
49+
if(isset($this->registry[$key])) {
50+
return $this->registry[$key];
51+
}
52+
53+
return null;
54+
}
55+
56+
public function offsetSet ( $key , $value ) {
57+
return $this->registry[$key] = $value;
58+
}
59+
60+
public function offsetUnset ( $key ) {
61+
unset($this->registry[$key]);
62+
}
63+
}
64+
65+
?>

app/route.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
array_unshift($params, "");
2929
}
3030

31+
// push url parameters to the registry file
32+
$registry->set("params", $params);
33+
3134
// if you have a user system you rely on you can deny access to all/specific controllers here.
3235

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

4245
// now we need to include the controller based on the url's first parameter (welcome)
43-
if(in_array($params[1].".controller.php", $controllers))
46+
if(is_array($controllers) && in_array($params[1].".controller.php", $controllers))
4447
{
45-
REQUIRE_ONCE 'controllers/'.$params[1].".controller.php";
46-
47-
// default classes to pass to the controller
48-
$load = array(
49-
"sec" => $Sec, // security
50-
"db" => $DB, // database
51-
"params" => $params, // url parameters
52-
);
53-
54-
// additional classes to pass to the controller only if they are enabled in /app/config.php
55-
if($_CONFIG['language']['enabled'] == true)
56-
$load['language'] = $Language;
57-
if($_CONFIG['upload']['enabled'] === true)
58-
$load['upload'] = $Upload;
59-
if($_CONFIG['api'] == 'enabled')
60-
$load['api'] = $API;
61-
62-
// then run the controller and initiate action_index
63-
$name = "controller_".$params[1];
64-
$$name = new $name($load);
65-
$$name->action_index();
48+
require_once 'app/controllers/'.$params[1].".controller.php";
49+
$new = 'controller_'.$params[1];
50+
$registry->set($params[1], new $new);
51+
$registry->get($params[1])->action_index();
6652
}
6753

6854
else // 404 - controller not found - redirect to home page

app/start.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

engine

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,11 @@ var default_controller = "<?php\n\
3636
\n\
3737
class controller_"+arguments[2]+"\n\
3838
{\n\
39-
public function __construct($params)\n\
40-
{\n\
41-
// Setup what we need for this controller\n\
42-
// $sec = new Security;\n\
43-
foreach ($params as $key => $val)\n\
44-
{\n\
45-
$this->{$key} = $val;\n\
46-
}\n\
47-
}\n\
39+
public function __construct()\n\
40+
{\n\
41+
$registry = Registry::getInstance();\n\
42+
foreach (reset($registry) as $key => $value) { $this->{$key} = $value; }\n\
43+
}\n\
4844
\n\
4945
public function action_index()\n\
5046
{\n\

0 commit comments

Comments
 (0)