Просмотр исходного кода

PHP 4 compatible plugin engine

pecesama 16 лет назад
Родитель
Сommit
eb69c37903
5 измененных файлов с 30 добавлено и 45 удалено
  1. 2 2
      admin/options.php
  2. 17 32
      classes/plugin.class.php
  3. 7 5
      classes/plugins.class.php
  4. 2 2
      classes/util.class.php
  5. 2 4
      entry.php

+ 2 - 2
admin/options.php Просмотреть файл

@@ -233,8 +233,8 @@ if ($user->isAdmin()) {
233 233
 											<option value="0" <?php if(!$conf->check_version) echo "selected=\"selected\""; ?>><?php echo __("Deactive")?></option>
234 234
 										</select>
235 235
 									</li>
236
-<?php	print_r($trigger);echo "<br>";die("Antes de trigger->call add options panel");
237
-									$trigger->call('add_options_panel');	
236
+<?php
237
+									$trigger->call('add_options_panel');									
238 238
 ?>									
239 239
 								</ul>
240 240
 							</fieldset>

+ 17 - 32
classes/plugin.class.php Просмотреть файл

@@ -12,28 +12,23 @@ if(!defined('entry') || !entry) die('Not a valid page');
12 12
   =========================== */
13 13
 ?>
14 14
 <?php
15
-	class plugin {	
15
+	class plugin {
16 16
 		
17 17
 		var $actions = array();
18 18
 		var $exists = array();
19 19
 	
20
-		function call($name) {            
21
-
20
+		function call($name) {			
21
+			
22 22
 			if (!$this->exists($name)) {
23 23
 				return false;
24
-			}            
25
-
26
- 			/*echo "<br />==========<br />";
27
-			echo $name;
28
-			echo "<br />";*/
24
+			} 			
29 25
 			
30 26
 			$index = 0;
31
-			//foreach (plugins::$instances as $plugin) {
27
+			
32 28
 			foreach ($GLOBALS['plugins::$instances'] as $plugin) {
33 29
 				if(array_key_exists($index,$this->actions[$name])){
34 30
 					$action = $this->actions[$name][$index][1]; 
35
-					if (is_callable(array($plugin, $action))) {
36
-						//echo "ejecutar: ".$action."()<br/>";					
31
+					if (is_callable(array($plugin, $action))) {						
37 32
 						$plugin->$action();
38 33
 						$index++;
39 34
 					}
@@ -44,18 +39,9 @@ if(!defined('entry') || !entry) die('Not a valid page');
44 39
 		function exists($name) {
45 40
             if (isset($this->exists[$name])) {
46 41
                 return $this->exists[$name];
47
-			}
48
-
49
-            //foreach (plugins::$instances as $plugin) {
50
-			foreach ($GLOBALS['plugins::$instances'] as $plugin) {
51
-				/*print_r(plugins::$instances);
52
-				echo "<br />";
53
-				print_r($plugin);
54
-				echo "<br />";
55
-				print_r($this->actions[$name]);
56
-				echo "<br />";
57
-				echo $this->actions[$name][0][1];
58
-				echo "<br />";*/
42
+			}			
43
+            
44
+			foreach ($GLOBALS['plugins::$instances'] as $plugin) {				
59 45
 				if(array_key_exists($name,$this->actions)){
60 46
 					if (is_callable(array($plugin, $this->actions[$name][0][1]))) {
61 47
 						return $this->exists[$name] = true;
@@ -65,16 +51,15 @@ if(!defined('entry') || !entry) die('Not a valid page');
65 51
 
66 52
             return $this->exists[$name] = false;
67 53
         }		
68
-        
69
-		function instance(){
70
-			//$instance;			
71
-			if( !isset($GLOBALS['$instance']) ) {
72
-				$GLOBALS['$instance'] = new plugin();
54
+		
55
+		//I really hate you PHP4's OOP implementation
56
+		function &instance() {
57
+			static $instance;
58
+			if (!isset($instance)) {
59
+				$instance = new plugin();
73 60
 			}
74
-			//print_r($GLOBALS['$instance']);
75
-			//die();
76
-			return $GLOBALS['$instance'];
77
-        }
61
+			return $instance;   
62
+		}
78 63
 		
79 64
 	}
80 65
 ?>

+ 7 - 5
classes/plugins.class.php Просмотреть файл

@@ -12,12 +12,14 @@ if(!defined('entry') || !entry) die('Not a valid page');
12 12
   =========================== */
13 13
 ?>
14 14
 <?php
15
-	$GLOBALS['plugins::$instances'] = array(); /* class static property */
16
-	class plugins {		
15
+	/* Simulating a class static property */
16
+	$GLOBALS['plugins::$instances'] = array();
17
+	
18
+	class plugins {
17 19
 		
18
-		function addAction($name, $function) {
19
-	    	$plugEngine = plugin::instance();
20
-	    	$plugEngine->actions[$name][] = array($this, $function);	    	
20
+		function addAction($name, $function) {	    	
21
+			$plugEngine =& plugin::instance(); //DO NOT remove the & after the =
22
+	    	$plugEngine->actions[$name][] = array($this, $function);
21 23
 	    }
22 24
 		
23 25
 	}

+ 2 - 2
classes/util.class.php Просмотреть файл

@@ -654,17 +654,17 @@ class util {
654 654
         
655 655
 		$actives = json_decode($conf->active_plugins,1);
656 656
 		$actives = $actives[1];
657
+		
657 658
         foreach ($actives as $index => $plugin) {
658 659
             if (!file_exists(Absolute_Path."plugins/".$plugin)) {
659 660
                 unset($actives[$index]);
660 661
                 continue;
661
-            }else{
662
+            } else {
662 663
 				require_once(Absolute_Path.'plugins/'.$plugin);
663 664
 			}            
664 665
 			if (!class_exists($index)) {
665 666
                 continue;
666 667
 			}
667
-			//plugins::$instances[$index] = new $index;
668 668
 			$GLOBALS['plugins::$instances'][$index] = new $index;
669 669
         }
670 670
 	}

+ 2 - 4
entry.php Просмотреть файл

@@ -2,13 +2,12 @@
2 2
 ob_start();
3 3
 if(!defined('entry') || !entry) die('Not a valid page');
4 4
 
5
-
6 5
 error_reporting (E_ALL);
7 6
 ini_set('display_errors', '1');
8 7
 
9 8
 // PHP settings specific to Gelato
10 9
 ini_set('pcre.backtrack_limit', '10000');
11
-// Globals to be used throughout the application
10
+
12 11
 define('Absolute_Path', dirname(__FILE__).DIRECTORY_SEPARATOR);
13 12
 $installed = true;
14 13
 $configFile = Absolute_Path.'config.php';
@@ -48,7 +47,6 @@ if (!extension_loaded('json')) {
48 47
 
49 48
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'configuration.class.php');
50 49
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'util.class.php');
51
-//require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'functions.php');
52 50
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'gelato.class.php');
53 51
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'templates.class.php');
54 52
 require_once(Absolute_Path.'classes'.DIRECTORY_SEPARATOR.'themes.class.php');
@@ -74,7 +72,7 @@ if($installed){
74 72
 	session_start();
75 73
 	
76 74
 	util::init_plugins();
77
-	$trigger = plugin::instance();
75
+	$trigger =& plugin::instance(); //DO NOT remove the & after the =	
78 76
 	
79 77
 	$trigger->call('gelato_init');	
80 78