leeaneHome

Archive for the tag 'development'

Plugin Architecture for CodeIgniter

I have finally started development of Threadler - my uber new discussion forum system! I have stalled a lot on this project mainly because I couldn’t decide which framework (if any) to use for development. I have been using CodeIgniter for the past 2 or 3 years but I was tempted by Kohana (a CI spin-off) and the prestige of building my own framework.

But in the end, I settled with CodeIgniter as I am pretty sure (for now) that I can make it do everything that I want it to.

So the first stumbling block was building the software to allow the use of plugins (a.k.a modules, extensions or add-ons depending on your favourite buzz word). Plugins will be used to extend the functionality of Threadler. For example, the bare-bones installation will not have Private Messaging installed, instead you can add this as a pre-built module and voila!

My plugins architecture must cater for:

  • Hooks/events/triggers to add specified code into the existing Threadler code base
  • All new pages (made up of controllers, models and views) which the new module may need

For example, a Private Messaging module may use a hook to add in the “Private Message” link within the normal forum display. But then it also requires new pages to display the form required to send/receive private messages.

Anyway, here is what I have come up with so far, remember it is still a  work in progress. I have basically used a combination of the elements below:

Disclaimer: The code below is proof of concept stuff only! I have not fully tested or tried to extend this to any degree. Also, I have not yet implemented the administration section to activate/deactivate a module - this will be another blog post.


Module Requirements

For it all to work, the end result is that modules must have the following things:

  • Be contained within a folder within /system/application/modules/ (e.g. /system/application/modules/private_messaging/)
  • Should contain:
    • module_info.php file - contains information about the module (e.g. author, name of module etc)
    • activate.php (optional) - contains code which is run when the module is activated in an administration section (e.g. sql statements to create private messaging tables)
    • deactivate.php (optional) - contains code which is run when the module is deactivated
    • events.php (optional) - registers which triggers/hooks the module requires
    • controllers (optional) - used if the module requires separate files (as per a normal CI Controller). All controllers must extend the Modules_Controller (see later for reasoning).
    • views (optional) - used if the module requires separate view files (as per normal CI View files)
    • models (optional) - used if the module requires separate model files (as per normal CI Model files)

Module Example Code

Here is a very basic test module which can be found in /system/application/modules/test/. This module does not require any activate.php or deactivate.php file because it pretty much does nothing except echo out some wonderful text.

The module_info file:

1
2
3
4
5
6
7
8
<?php
$data['module']['name'] = "test";
$data['module']['displayname'] = "Test Module";
$data['module']['description'] = 'Module which does very little';
$data['module']['version'] = "1.0.0";
$data['module']['author'] = "Leeane";
$data['module']['homepage'] = "http://blog.leeane.com";
?>

The events file:

<?php
class test_events
{
	function test_events(&$threadler_events)
	{
		$threadler_events->register('testing', $this, 'my_test');
 
 
		$threadler_events->register('testing', $this, 'my_test2');
	}
	function my_test()
	{
		echo 'This is a test of an event trigger<br />';
	}
	function my_test2()
	{
		echo 'This is yet another test of an event trigger<br />';
	}
}
?>

The example controller (accessible by http://mysite.com/test/):

1
2
3
4
5
6
7
8
9
10
11
<?php
class Test extends Modules_Controller {
	function Test()
	{
		parent::Modules_Controller();	
	}
	function index()
	{
		$this->load->view('welcome_message');
	}
}

The example view file:

1
2
3
4
5
<html>
<head><title>Welcome to CodeIgniter</title></head>
<h1>Testing the Modules thingo!</h1>
Now lets test the output of a trigger/event/hook thingo! 
<?php $this->threadler_events->trigger('testing');?>

OK thats enough of the module itself, onto the library code.

Libraries

First up, the events and hooks library (this is largely taken from the 68Kb project - I encourage you to look at the relevant project page at Google Code):

/system/application/libraries/Threadler_events.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Threadler Events
 *
 * This is largely based on the Kb_events class authored by Eric Barnes (68Kb).
 *
 * @author		Eric Barnes
 * @link		http://www.68kb.com/hooks.html
 */
 
class Threadler_events
{
	/**
	 * @var array Array of registered hooks and their listners
	 */
	var $listeners = array();
 
	/**
	 * Threadler events
	 * 
	 * Allow users to extend the system.
	 * Idea from Iono
	 */
	function threadler_events()
	{
		$data='';
		$CI =& get_instance();
		//if($CI->db->table_exists('modules'))
		//{
		//	$CI->db->from('modules');
		//	$CI->db->where('active', '1'); 
		//	$query = $CI->db->get();
		//	foreach ($query->result() as $row)
		//	{
				if (@file_exists(APPPATH .'modules/test/events.php'))
				{
					include_once(APPPATH .'modules/test/events.php');
					$class = 'test_events';
					if (class_exists($class)) 
					{
						new $class($this);
					}
				}
		//	}
		//}
	}
 
	/**
	 * Register a listner for a given hook
	 *
	 * @param string $hook
	 * @param object $class_reference
	 * @param string $method
	 */
	function register($hook, &$class_reference, $method)
	{
		// Specifies a key so we can't define the same handler more than once
		$key = get_class($class_reference).'->'.$method;
		$this->listeners[$hook][$key] = array(&$class_reference, $method);
	}
 
	/**
	 * Trigger an event
	 *
	 * @param string $hook
	 * @param mixed $data
	 */
	function trigger($hook, $data='')
	{
		$call_it='';
		// Are there any hooks?
		if (isset($this->listeners[$hook]) && is_array($this->listeners[$hook]) && count($this->listeners[$hook]) > 0)
		{
			// Loop
			foreach ($this->listeners[$hook] as $listener)
			{
				// Set up variables
				$class =& $listener[0];
				$method = $listener[1];
				if(method_exists($class,$method))
				{
					// Call method dynamically
					$call_it.=$class->$method($data);
				}
			}
		}
		return $call_it;
	}
}

Please note, you would want to uncomment out the database calls in the file above - I took them out just to get this proof of concept working. I have hard-coded this to work with the “test” module only for now. Please also note that for this all to work you will need to autoload the “threadler_events” library by adding $this->load->library(’threadler_events’); in your /system/application/config.php file.

/system/application/libraries/Threadler_hooks.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php 
if (!defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * Threadler Hooks
 *
 * This is largely based on the Kb_hooks class authored by Eric Barnes (68Kb).
 *
 * @author		Eric Barnes
 * @link		http://www.68kb.com/hooks.html
 */
	class Threadler_hooks 
	{
		/**
		* @var array All User modules
		* @access private
		*/
		var $user_modules;
 
		var $listeners = array();
 
		function Threadler_hooks()
		{
		}
 
		// ------------------------------------------------------------------------
 
		/**
		 * Test if a function exists
		 *
		 * @access	private
		 * @param	string	the function name
		 * @return	bool
		 */
		function _test_exists($function_name) 
		{
			return function_exists($function_name);
		}
 
		// ------------------------------------------------------------------------
 
		/**
		 * Invoke the hook
		 *
		 * This function actually invokes the hook. 
		 * It is used so two modules can share one hook.
		 *
		 * @access	private
		 * @param	string	the function name
		 * @param	array	the function params
		 * @return	bool
		 */
		function _invoke_hook($function_name, $params) 
		{
			return call_user_func($function_name, $params);
		}
 
		// ------------------------------------------------------------------------
 
		/**
		 * Call the hook
		 *
		 * Throughout the script when a certain action 
		 * occurs it will call this hook.
		 *
		 * @access	public
		 * @param	string	the function name
		 * @param	array	the function params
		 * @return	bool
		 */
	 	function call_hook($function, $params='')
		{
			// Find all the user modules
			$data='';
			$CI =& get_instance();
			$CI->load->helper('url');
			//$CI->db->from('modules');
			//$CI->db->where('active', '1'); 
			//$query = $CI->db->get();
			//echo $CI->db->last_query();
			//foreach ($query->result() as $row)
			//{
				if (@file_exists(APPPATH .'modules/test/hooks.php'))
				{
					require_once(APPPATH .'modules/test/hooks.php');
				}
				$function_name = 'test_'.$function;
				if ($this->_test_exists($function_name)) 
				{
					$data.=$this->_invoke_hook($function_name, $params);
				}
			//}
 			return $data;
		}
}

Please note, you would want to uncomment out the database calls in the file above - I took them out just to get this proof of concept working. I have hard-coded this to work with the “test” module only for now.

Next you want to install the Modular Extensions HMVC plugin - basically you just need to drop the Modules.php, Controller.php and MY_Router.php files into the /system/application/libraries/ folder and you’re all done.

Lastly, I do not want people to be able to access a modules controllers directly unless the module has been activated in the administration section. Thankfully, the hooks/events will not trigger unless the module is activated (thanks to 68Kb project’s code, although those relevant bits were commented out in this example). But people will be able to directly access http://mysite.com/test/ and view the test controller which is probably not a good idea!

So for this, we want to make a new Controller class which all modules must inherit from.
I.e. class MyNiftyModule extends Module_Controller {…}

The module can be tested for conformity to this rule at the point a module is added to the library of available modules on the threadler website - I haven’t got code for this yet. This new Module_Controller will be called before any module’s controller is run, and will check (thanks to a database call) if the relevant module is activated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php if (!defined('BASEPATH')) exit('No direct access allowed');
/**
 * Modules_Controller
 *
 *
 * Description:
 * This library is used in all modules for Threadler (via "class ModuleName extends Modules_Controller {...}")
 * It checks if a given module is "activated" in the database before displaying any content.
 *
 * This is used to prevent anyone from directly accessing an unactivated modules' controllers
 *
 */
 
class Modules_Controller extends Controller {
	function Modules_Controller()
	{
		parent::Controller();
		$CI =& get_instance();
 
		//grab modules name (this will be the folder under /threadler/modules/)
		$module_name = $CI->uri->segment(1);
 
		//query the database to determine if the module is activated
		$query = $CI->db->query("SELECT 1 
								 FROM threadler_modules 
								 WHERE module_name = ".$CI->db->escape($module_name)."
								 AND enabled = 'yes'");
		$num_results = $query->num_rows();
 
		if ($num_results != 1)
		{
			//stop everything!
			show_error('Uh-oh! This module has not been activated in the threadler administration section.');
		}
		else
		{
			//do nothing, allow the controller to load as per normal
		}		
	}	
}

I am sure you can get the gist for the kind of SQL table you need for this to function properly.

Testing

When we go to http://mysite.com/test/, we get:

Testing the Modules thingo!
Now lets test the output of a trigger/event/hook thingo!
This is a test of an event trigger
This is yet another test of an event trigger

Conclusions

It all seems to work fairly well so far. There’s obviously more work to do with actually activating the module in the admin section and maybe a priority setting for a modules hook etc but I am fairly happy with what I have so far (all be it a quick throw-together).

I am a bit new to plugins so if you see any issues with the architecture or code I have posted please let me know. I am interested to see how it could be improved.

  • Share/Save/Bookmark

10 of the Best Tools For Web Development on a Mac

Having only had my macbook for a couple of weeks, I am still very new to OSX and all the apps which are available. The primary reason I bought the lovely new macbook is for web development - specifically for PHP/MySQL sites which I develop.

I spent a number of weeks both before and after purchasing the macbook looking for the perfect web development environment for me. In this post I will outline what I am using and why, any pitfalls I have had and also alternative software which you might want to try out.

Apache/MySQL/PHP

screenshot_s1_3_enProbably no real suprises here, but I opted to go with MAMP (MacOSX, Apache, MySQL, PHP) as the tool to manage my web and database servers. It is incredibly easy to setup.

There are two versions, MAMP (free) and MAMP Pro (not free). MAMP Pro gives you access to setup and manage your virtual hosts as well as GUI access to other things you might want such as loading certain Apache modules. I tried out both, and honestly I have uninstalled MAMP Pro (and just kept the standard MAMP) as I found I was actually having issues where my Apache server would fail to start with MAMP Pro. I definitely do need access to edit virtual hosts but read on for my alternative program to do this.

MAMP allows you to choose the PHP version to use (4 or 5) as well as any caching engines or php accelerators. Stuff that any serious developer would appreciate having quick and easy access to. You can also easily change the ports which Apache and MySQL use to the defaults (80 and 3306). Definitely makes things a lot easier if you use the normal ports.

Download: http://www.mamp.info/en/index.php
Price: Free
Alternatives: XAMPP (includes Perl), or do it all via the terminal yourself! See http://mymacinations.com/2007/10/28/apache-php-and-mysql-on-leopard/ for more info.

Virtual Hosts

vhSince I didn’t want to be paying for MAMP Pro, I came across another free application which will let me manage my virtual hosts in a nice and easy GUI screen. It is called Virtual HostsX and once again is pretty simple to use. It automatically detects MAMP and prompts you if you want to update MAMP’s apache configuration files (yes). I believe it is limited to 3 active virutal host entries at any one time - but this shouldn’t pose to much of an issue for me. If you need more entries at any one time, hey its only $9 to buy and support the developer.

An important thing which new people to Virtual HostsX sometimes do not realise is that when installing the program, it automatically enables the “Web Sharing” option in your System Preferences > Sharing screen. So make sure you go in there and un-tick the Web Sharing option - this will just cause all sorts of issues if you are using MAMP.

One pitfall I had with MAMP was a compatibility issue with Virtual HostsX application (the virtual hosts weren’t being picked up properly). After finding a thread on the same thing, it was recommended to use MAMP 1.7.1. For this reason, I actually had to downgrade from MAMP 1.7.2 to MAMP 1.7.1.

Download: http://clickontyler.com/virtualhostx/
Price: Free - $9 if you want more than 3 virutal host entries at any one time.
Alternatives: MAMP Pro (I couldn’t get this to work though) or edit apache config files via terminal yourself.

Code Editor

codaOhh mac people are spoiled for choices in this category! On my Windows machine I was a Dreamweaver user (hey it is decent and does the job). And although the Adobe CS4 suite was one of the first things I installed on my machine (yay for academic agreements) I wanted to try out a few of the mac editor apps which I kept on hearing so much about. More specifically, Coda and TextMate. After using both for a short while, I came to the conclusion that:

Coda - wins.

After watching the videos on the panic website, everything just seemed to make sense. It seems to be very intuitive and so far I am really liking it. When first opening the application, I was quite underwhelmed as there didn’t seem to be many bells and whistles. But after watching the videos and playing around with it for an hours, I realised that all the bells and whistles (and more) are there, they just do not clutter the interface in a way that I am used to with something like Dreamweaver. Kudos to the developers - I think this really sets a new standard in code editors.

Download: http://www.panic.com/coda/
Price: Demo available. $99 to purchase after demo expires.
Alternatives: TextMate, Dreamweaver etc

Database Manager

screenshot-1I normally like to use a desktop-based tool to manage SQL databases. On Windows I have used SQLyog and the MYSQL Administrator package. The popular choice for this functionality on a Mac seems to be Sequel Pro (formally Cocoa MySQL). It certainly is more than adequate for most users and I am happy with it so far. The interface is pretty standard for these types of programs and I haven’t had any problems working out how to do something.

Important: If you are using MAMP, make sure you put the following into the “socket” field in Sequel Pro: “/applications/mamp/tmp/mysql/mysql.sock”. Otherwise, you wont be able to connect to the MySQL server!

Download: http://www.mjmedia.com.au/sequel-pro.html
Price: Free
Alternatives: phpMyAdmin (web-based), NaviCat, Aqua Data Studio etc

Database Designer

a-200Everyone loves a good database modeler - right? For your bigger projects you want to have a good tool to map out your tables, keys and relationships. Make sure everything looks about right before you dive into the deep end. I didn’t really look around all that much for software for this. I stumbled across a free tool called Power*Architect which looks to be a tool which will handle the job nicely.

Download: http://www.sqlpower.ca/page/architect
Price: Free
Alternatives: SQLEditor

FTP

transmitAlthough I don’t have much need for FTP anymore (thanks to SVN) it is handy to have a good FTP program for the odd job here and there. There are a number of alternatives here, but the main contenders are CyberDuck (free) and Transmit (paid). I have to admit that I prefer Transmit’s interface (especially since I am using Coda which is by the same developers) but at the end of the day, CyberDuck is a fantastic free program which is fine for what I am actually using FTP for.

Download: http://www.panic.com/transmit/
Price: Demo available - $29.95 for full version.
Alternatives: CyberDuck FTP

SVN Tool

versionsSome sort of code versioning is an absolute must for any serious developer. Once you make the jump into versioning you will never look back. There are a few options (cvs, git etc) but I choose to use subversion (svn) as there are plenty of tools out there to make it all very easy to integrate into your development process.

Coda has some built in svn integration, but since you cannot add comments to each revision with Coda I was on the lookout for something a bit more powerful. There are a number of options out there and for now I have settled on Versions.app which is a powerful (and pretty) tool to manage all your svn goodness. Whether or not I end up buying it after the demo runs out will depend on how I go over the next few weeks.

Download: http://www.versionsapp.com
Price: Demo available - $52 for full version.
Alternatives: SmartSVN, SynchroSVN etc

SVN Hosting

assembla_logoThere are a few options when it comes to hosting - you can host it yourself or use a hosted solution. I have opted for a hosted solution although I am still tossing up between Dreamhost’s SVN option or using an application such as Assembla (which will include wiki, miletstones, trac etc). I like the idea of Assembla so will probably end up giving their free plan a go.

Download: http://www.assembla.com
Price: Free plan (200mb limit)
Alternatives: Unfuddle, Google Code, Sourceforge or set it up yourself on your own server.

Graphics

adobephotoshopcs3I couldn’t go past Adobe PhotoShop and Adobe Illustrator for this. Seriously, if you have access to them for free you really need to consider using these tools. CS4 has some great improvements (tabbed windows ftw!) and I am still finding new features every time I use it. It truly is the industry standard for a reason.

Download: http://adobe.com
Price: Very expensive… unless you have access to free version (e.g. academic agreement etc)
Alternatives: GIMP, Fireworks etc

Browser

firefoxEveryone will have a favorite browser - and for me that is Firefox. Combined with a few plugins such as Web Developer Toolbar, ColorZilla, YSlow and HTML Validator it is unbeatable in my opinion. But it doesn’t stop there as we need to check our websites in other browsers (Safari, Opera, IE6 and IE7 at a minimum).

Testing IE is the biggest pain of all on a mac. You can use VMware Fusion or Parallels to install Windows and test IE7. But then you cannot run native versions of IE6 and IE7 on Windows at the same time. Boo to Microsoft. So that means two installations of Windows which therefore means you need 4Gb of ram otherwise your development machine will be crawling.

I am still a bit undecided about all of this. I have installed ies4osx which at the moment only supports IE6 (despite the fact that the website says both IE6 and IE7). IE6 seems to work well enough so hopefully the developers can add in IE7 to the new version soon. I have been wanting to avoid installing Windows (via vmware fusion or parallels) as I do not want to slow my machine down just because I need to run IE6 and IE7. So for now I am just going to check rendering on my Windows desktop machine (with Multiple IE’s installed).

Conclusion

Now there is definitely room for improvement, and I will always be on the lookout for something better. If you have any great suggestions please let me know. I also haven’t really started using all of these together to any great degree just yet - the first task was just getting is all set up.

Maybe things will change in the future - but for now I am pretty darn happy with it all.

  • Share/Save/Bookmark