-
jLinux The Linux Dude
  
-
Posts: 157
Threads: 35
Joined: Jun 2015
Reputation:
2
I wanted to use a plugin system for the app that im creating, and the one I found was from here, while it was a little out of date, it seemed pretty straight forward and efficient. So once I downloaded it and fixed a couple typos/settings and changed some old depreciated functions, it works great! The only downside is that it has all DataBase functionality incorporated into it, I really wanted to create a Plugins model to use.
I created a plugins model and added it to the autoloader, and tried to use it just like I would use a model in any of my other libraries..
PHP Code: <?php $this->_ci =& get_instance(); $this->_ci->Plugins_model->tester('string..');
Heres the error and backtrace:
Quote:Severity: Notice
Message: Undefined property: Account::$Plugins_model
Filename: libraries/Plugins_lib.php
Line Number: 65
Backtrace:
File: /...../htdocs/application/libraries/Plugins_lib.php<br />
Line: 65<br />
Function: _error_handler
File: /...../htdocs/application/controllers/Account.php<br />
Line: 22<br />
Function: __construct
File: /...../htdocs/index.php<br />
Line: 292<br />
Function: require_once
And from the logs: Quote:Severity: Error --> Call to a member function tester() on null /....../htdocs/application/libraries/Plugins_lib.php 65
Anyone see why I wouldnt be able to use a regular model in here? when it works just fine in any other library, im guessing its something simple that ive just over looked..
Heres the full working code of the Plugins_lib.php file, with my new changes.
PHP Code: <?php /** * Codeigniter Plugin System * * A hook based plugin library for adding in Wordpress like plugin functionality. * * NOTICE OF LICENSE * * Licensed under the Open Software License version 3.0 * * This source file is subject to the Open Software License (OSL 3.0) that is * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: * http://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to obtain it * through the world wide web, please send an email to * [email protected] so we can send you a copy immediately. * * @package CI Plugin System * @author Dwayne Charrington * @copyright Copyright (c) 2012 - Dwayne Charrington * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://ilikekillnerds.com * @since Version 1.1 */
class Plugins_lib {
// Codeigniter instance protected $_ci;
// Instance of this class public static $instance;
// Action statics public static $actions; public static $current_action; public static $run_actions;
// Plugins public static $plugins_pool; public static $plugins_active;
// Directory public $plugins_dir;
// Error and Message Pools public static $errors; public static $messages;
const PLUGIN_ERROR_PREFIX = 'PLUGINERR'; // ------------------------------------------------------------------------
/** * Constructor * * @param mixed $params * @return Plugins */ public function __construct($params = array()) { // Codeigniter instance $this->_ci =& get_instance(); $this->_ci->load->database(); $this->_ci->load->helper('directory'); $this->_ci->load->helper('file'); $this->_ci->load->helper('url');
// Set the plugins directory if passed via paramater if (array_key_exists('plugins_dir', $params)) { $this->set_plugin_dir($params['plugins_dir']); } else // else set to default value { $basepath = str_replace("\\", "/", FCPATH); $this->set_plugin_dir($basepath . "plugins/"); }
// Remove index.php string on the plugins directory if any $this->plugins_dir = str_replace("index.php", "", $this->plugins_dir);
// Find all plugins $this->find_plugins();
// Get all activated plugins $this->get_activated_plugins();
// Include plugins $this->include_plugins();
self::$messages = ""; // Clear messages self::$errors = ""; // Clear errors }
// ------------------------------------------------------------------------
/** * Get Requirements * * Parse through the plugins .php code to check for any classes that it * attempts to use, returns an array of class names * * @param string $plugin System name of plugin (NOT display name) * @access public * @return array */ public function find_classes($plugin) { $plugin_path = FCPATH . "/plugins/{$plugin}/";
require_once 'class_profiles.php';
//die('CLASSES:<pre>' . print_r($classes, TRUE));
$files = array();
if( ! $dir = @new RecursiveDirectoryIterator( realpath($plugin_path) )) { Logger_lib::error("Failed to read from plugin path: {$plugin_path}", TRUE, TRUE);
return FALSE; }
$objects = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object) { if(preg_match('/\.php$/', $name)) { array_push($files, $name); } }
$matches = array();
foreach($files as $f) { $line_num = 0;
if( ! $fh = @fopen($f, 'r')) { Logger_lib::error("Failed to open plugin file for parsing: {$f}", TRUE, TRUE);
return FALSE; }
$file_lines = array();
while ( ! feof($fh)) { $line_num++; $line = fgets($fh, 4096);
$file_lines[] = trim($line);
foreach($classes as $c => $d) { if (preg_match('/' . $d['regex'] . '/', $line)) { $matches[$c][] = array( 'file' => $f, 'class' => $d['class'], 'line_num' => $line_num, 'line' => trim($line) );
}
} }
fclose($fh); }
die('<pre>' . print_r($matches, TRUE));
exit; }
// ------------------------------------------------------------------------
/** * Set Plugin Dir * Set the location of where all of the plugins are located * * @param mixed $directory */ public function set_plugin_dir($directory) { if (!empty($directory)) { $this->plugins_dir = trim($directory); } }
// ------------------------------------------------------------------------
/** * Instance * The instance of this plugin class * */ public static function instance() { if (!self::$instance) { self::$instance = new Plugins_lib(); }
return self::$instance; }
// ------------------------------------------------------------------------
/** * Find Plugins * * Find plugins in the plugins directory. * */ public function find_plugins() { $plugin_query = $this->_ci->db->get('installed_plugins');
$plugins_db = array();
foreach($plugin_query->result() as $r) { $plugins_db[$r->plugin_system_name] = (array)$r; }
$plugins = scandir($this->plugins_dir, 1);
if ($plugins != false) { foreach ($plugins AS $key => $name) { $name = strtolower(trim($name));
// If the plugin hasn't already been added and isn't a file if (!isset(self::$plugins_pool[$name]) AND !stripos($name, ".")) { // Make sure a valid plugin file by the same name as the folder exists if (file_exists($this->plugins_dir.$name."/".$name.".php")) { self::$plugins_pool[$name] = $plugins_db[$name]; } else { self::$errors[$name][] = "Plugin file ".$name.".php does not exist."; } } } } }
// ------------------------------------------------------------------------
/** * Get Activated Plugins * Get all activated plugins from the database * */ public function get_activated_plugins() { // Only plugins in the database are active ones //$plugins = $this->_ci->db->where('plugin_status', 1)->get('installed_plugins'); $plugins = $this->_ci->db->get('installed_plugins');
// If we have activated plugins if ($plugins->num_rows() > 0) { // For every plugin, store it foreach ($plugins->result_array() as $plugin) { $this->get_plugin_headers($plugin['plugin_system_name']);
self::$plugins_active[$plugin['plugin_system_name']] = $plugin['plugin_system_name']; } } else { return true; } }
// ------------------------------------------------------------------------
/** * Include Plugins * Include all active plugins that are in the database * */ public function include_plugins() { if(self::$plugins_active AND ! empty(self::$plugins_active)) { // Validate and include our found plugins foreach (self::$plugins_active AS $name => $value) { // The plugin information being added to the database $data = array( "plugin_system_name" => $name, "plugin_name" => trim(self::$plugins_pool[$name]['plugin_headers']['plugin_name']), "plugin_uri" => trim(self::$plugins_pool[$name]['plugin_headers']['plugin_uri']), "plugin_version" => trim(self::$plugins_pool[$name]['plugin_headers']['plugin_version']), "plugin_description" => trim(self::$plugins_pool[$name]['plugin_headers']['plugin_description']), "plugin_author" => trim(self::$plugins_pool[$name]['plugin_headers']['plugin_author']), "plugin_author_uri" => trim(self::$plugins_pool[$name]['plugin_headers']['plugin_author_uri']) );
$this->_ci->db->where('plugin_system_name', $name)->update('installed_plugins', $data);
// If the file was included include_once $this->plugins_dir.$name."/".$name.".php";
// Run the install action for this plugin //self::do_action('install_' . $name); self::do_action(ucfirst($name) . '|install'); } } }
// ------------------------------------------------------------------------
/** * Get Plugin Headers * * Get the header information from all plugins in * the plugins pool for use later on. * * @param mixed $plugin */ public function get_plugin_headers($plugin) { if (self::$plugins_pool !== false AND !empty(self::$plugins_pool)) { $plugin = strtolower(trim($plugin)); // Lowercase and trim the plugin name
$plugin_content = file_get_contents($this->plugins_dir.$plugin."/".$plugin.".php");
preg_match ('|Plugin Name:(.*)$|mi', $plugin_content, $name); preg_match ('|Plugin URI:(.*)$|mi', $plugin_content, $uri); preg_match ('|Version:(.*)|i', $plugin_content, $version); preg_match ('|Description:(.*)$|mi', $plugin_content, $description); preg_match ('|Author:(.*)$|mi', $plugin_content, $author_name); preg_match ('|Author URI:(.*)$|mi', $plugin_content, $author_uri);
if (isset($name[1])) { $arr['plugin_name'] = trim($name[1]); }
if (isset($uri[1])) {
$arr['plugin_uri'] = trim($uri[1]); }
if (isset($version[1])) { $arr['plugin_version'] = trim($version[1]); }
if (isset($description[1])) { $arr['plugin_description'] = trim($description[1]); }
if (isset($author_name[1])) { $arr['plugin_author'] = trim($author_name[1]); }
if (isset($author_uri[1])) { $arr['plugin_author_uri'] = trim($author_uri[1]); }
// For every plugin header item foreach ($arr AS $k => $v) { // If the key doesn't exist or the value is not the same, update the array if (!isset(self::$plugins_pool[$plugin]['plugin_headers'][$k]) OR self::$plugins_pool[$plugin]['plugin_headers'][$k] != $v) { self::$plugins_pool[$plugin]['plugin_headers'][$k] = trim($v); } else { return true; } } } }
// ------------------------------------------------------------------------
/** * Activate Plugin * * Activates a plugin only if it exists in the * plugins_pool. After activating, reload page * to get the newly activated plugin * * @param mixed $name */ public function activate_plugin($name) { $name = strtolower(trim($name)); // Make sure the name is lowercase and no spaces
//die('ACTIVATE POOL: <pre>' . print_r(self::$plugins_active, TRUE));
// Okay the plugin exists, push it to the activated array if (isset(self::$plugins_pool[$name]) AND !isset(self::$plugins_active[$name])) { $db = $this->_ci->db->select('plugin_system_name')->where('plugin_system_name', $name)->get('installed_plugins', 1);
if ($db->num_rows() == 0) { $this->_ci->db->insert('installed_plugins', array('plugin_system_name' => $name)); }
// Run the activate hook //self::do_action('activate_' . $name); self::do_action(ucfirst($name) . '|activate'); }
}
// ------------------------------------------------------------------------
/** * Deactivate Plugin * * Deactivates a plugin * * @param string $name */ public function deactivate_plugin($name) { $name = strtolower(trim($name)); // Make sure the name is lowercase and no spaces
// Okay the plugin exists if (isset(self::$plugins_active[$name])) { $this->_ci->db->where('plugin_system_name', $name)->delete('installed_plugins'); self::$messages[] = "Plugin ".self::$plugins_pool[$name]['plugin_headers']['plugin_name']." has been deactivated!";
// Deactivate hook //self::do_action('deactivate_' . $name); self::do_action(ucfirst($name) . '|deactivate'); } }
// ------------------------------------------------------------------------
/** * Plugin Info * * Get information about a specific plugin * * @param mixed $name */ public function plugin_info($name) { if (isset(self::$plugins_pool[$name])) { return self::$plugins_pool[$name]['plugin_headers']; } else { return true; } }
// ------------------------------------------------------------------------
/** * Print Plugins * * This plugin returns the array of all plugins found * */ public function print_plugins() { return self::$plugins_pool; }
// ------------------------------------------------------------------------
/** * Add Action * * Add a new hook trigger action * * @param mixed $name * @param mixed $function * @param mixed $priority */ public function add_action($name, $function, $priority=10) { //$backtrace = debug_backtrace();
//die('<pre>' . print_r($backtrace[1], TRUE));
// If we have already registered this action return true //if (isset(self::$actions[$name][$priority][$function])) if (isset(self::$actions[$name])) { //die('A'); return true; }
//die('Actions: <pre>' . print_r(self::$actions, TRUE));
/** * Allows us to iterate through multiple action hooks. */ if (is_array($name)) { foreach ($name AS $name) { // Store the action hook in the $hooks array self::$actions[$name][$priority][implode('|', $function)] = array("function" => $function); } } else { // Store the action hook in the $hooks array self::$actions[$name][$priority][implode('|', $function)] = array("function" => $function); }
//die('<pre>' . print_r(self::$actions, TRUE));
return true; }
// ------------------------------------------------------------------------
/** * Do Action * * Trigger an action for a particular action hook * * @param mixed $name * @param mixed $arguments * @return mixed */ public function do_action($action, $arguments = "") { // Oh, no you didn't. Are you trying to run an action hook that doesn't exist? if ( ! isset(self::$actions[$action])) { return $arguments; }
// Set the current running hook to this self::$current_action = $action;
// Key sort our action hooks ksort(self::$actions[$action]);
// Loop through actions foreach(self::$actions[$action] as $priority => $names) { if (is_array($names)) { // Loop through each plugin foreach($names as $name) { $plugin_obj = call_user_func($name['function'][0] . '::instance');
// Make sure the action beign called is a method in the plugin if(method_exists($plugin_obj, $name['function'][1])) { // This line runs our function and stores the result in a variable $returnargs = call_user_func_array( [ $plugin_obj, $name['function'][1] ], array( &$arguments ) );
if( $returnargs ) { $arguments = $returnargs; }
// Store our run hooks in the hooks history array self::$run_actions[ $name ][ $priority ]; } else { //($err, $inc_args = NULL, $fatal = FALSE, $prefix = NULL) Logger_lib::error("Failed to execute method {$name['function'][0]}::{$name['function'][1]} for action {$action}, method does not exist",FALSE,FALSE, self::PLUGIN_ERROR_PREFIX); } } } }
// No hook is running any more self::$current_action = '';
return $arguments; }
// ------------------------------------------------------------------------
/** * Remove Action * * Remove an action hook. No more needs to be said. * * @param mixed $name * @param mixed $function * @param mixed $priority */ public function remove_action($name, $function, $priority=10) { // If the action hook doesn't, just return true if (!isset(self::$actions[$name][$priority][$function])) { return true; }
// Remove the action hook from our hooks array unset(self::$actions[$name][$priority][$function]); }
// ------------------------------------------------------------------------
/** * Current Action * * Get the currently running action hook * */ public function current_action() { return self::$current_action; }
// ------------------------------------------------------------------------
/** * Has Run * * Check if a particular hook has been run * * @param mixed $hook * @param mixed $priority */ public function has_run($action, $priority = 10) { if (isset(self::$actions[$action][$priority])) { return true; } else { return false; } }
// ------------------------------------------------------------------------
/** * Action Exists * * Does a particular action hook even exist? * * @param mixed $name */ public function action_exists($name) { if (isset(self::$actions[$name])) { return true; } else { return false; } }
// ------------------------------------------------------------------------
/** * Will print our information about all plugins and actions * neatly presented to the user. * */ public static function debug_class() { if (isset(self::$plugins_pool)) { echo "<h2>Found plugins</h2>"; echo "<p>All plugins found in the plugins directory.</p>"; echo "<pre>"; print_r(self::$plugins_pool); echo "</pre>"; echo "<br />"; echo "<br />"; }
if (isset(self::$plugins_active)) { echo "<h2>Activated plugins</h2>"; echo "<p>Activated plugins that have already been included and are usable.</p>"; echo "<pre>"; print_r(self::$plugins_active); echo "</pre>"; echo "<br />"; echo "<br />"; }
if (isset(self::$actions)) { echo "<h2>Register action hooks</h2>"; echo "<p>Action hooks that have been registered by the application and can be called via plugin files.</p>"; echo "<pre>"; print_r(self::$actions); echo "</pre>"; echo "<br />"; echo "<br />"; }
if (isset(self::$run_actions)) { echo "<h2>Previously run action hooks</h2>"; echo "<p>Hooks that have been called previously.</p>"; echo "<pre>"; print_r(self::$run_actions); echo "</pre>"; echo "<br />"; echo "<br />"; } } }
// ------------------------------------------------------------------------
/** * Add a new action hook * * @param mixed $name * @param mixed $function * @param mixed $priority */ function add_action($name, $function, $priority=10) { return Plugins_lib::instance()->add_action($name, $function, $priority); }
// ------------------------------------------------------------------------
/** * Run an action * * @param mixed $name * @param mixed $arguments * @return mixed */ function do_action($name, $arguments = "") { return Plugins_lib::instance()->do_action($name, $arguments); }
// ------------------------------------------------------------------------
/** * Remove an action * * @param mixed $name * @param mixed $function * @param mixed $priority */ function remove_action($name, $function, $priority=10) { return Plugins_lib::instance()->remove_action($name, $function, $priority); }
// ------------------------------------------------------------------------
/** * Check if an action actually exists * * @param mixed $name */ function action_exists($name) { return Plugins_lib::instance()->action_exists($name); }
// ------------------------------------------------------------------------
/** * Set the location of where our plugins are located * * @param mixed $directory */ function set_plugin_dir($directory) { Plugins_lib::instance()->set_plugin_dir($directory); }
// ------------------------------------------------------------------------
/** * Activate a specific plugin * * @param mixed $name */ function activate($name) { return Plugins_lib::instance()->activate_plugin($name); }
// ------------------------------------------------------------------------
/** * Deactivate a specific plugin * * @param mixed $name */ function deactivate($name) { return Plugins_lib::instance()->deactivate_plugin($name); }
// ------------------------------------------------------------------------
/** * Print Plugins * Returns the list of plugins * */ function print_plugins() { return Plugins_lib::instance()->print_plugins(); }
// ------------------------------------------------------------------------
/** * Return the number of plugins found * */ function count_found_plugins() { return count(Plugins_lib::$plugins_pool); }
// ------------------------------------------------------------------------
/** * Return number of plugins activated * */ function count_activated_plugins() { return count(Plugins_lib::$plugins_active); }
// ------------------------------------------------------------------------
/** * Debug function will return all plugins registered and hooks * */ function debug_class() { Plugins_lib::debug_class(); }
// ------------------------------------------------------------------------
/** * Return all errors * */ function plugin_errors() { if (is_array(Plugins_lib::$errors)) { foreach (Plugins_lib::$errors AS $k => $error) { echo $error."\n\r"; } } else { return true; } }
// ------------------------------------------------------------------------
/** * Return all messages * */ function plugin_messages() { if (is_array(Plugins_lib::$messages)) { foreach (Plugins_lib::$messages AS $k => $message) { echo $message."\n\r"; } } else { return true; } }
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
According to your post,
Filename: libraries/Plugins_lib.php
Line Number: 65
is
$this->_ci->load->database();
I have no clue why " Undefined property: Account::$Plugins_model" occurs.
-
Blair2004 Tendoo Core Developper
  
-
Posts: 120
Threads: 34
Joined: May 2015
Reputation:
1
$this->_ci->Plugin_model... is not set that's why it's null. Check if you have correctly loaded "Plugin_Model" before retreiving it from "get_instance()".
I wanted to use that "plugin" sytem also for my CMS, but i saw too many work to merge it and it's a WordPress feature.
Anyway good luck !!!
|