Posted (Updated ) in PHP

While aimlessly browsing my Twitter feed last night, I came across this tutorial on using CodeIgniter‘s Zip class. Kohana 3.1 doesn’t seem to come with similar functionality out of the box (at least from the brief glance I took), so I figured I’d write up a quick patch to get CI’s class working with it.

Download the module with example controller and test files here.

You can read up on how to use the class in CodeIgniter’s documentation or the tutorial linked to above. I’ve included a controller and 2 test files in the download above as a kind of ‘quick start guide’. To access them simply go to /koziptest and /koziptest/invalid for a successful and unsuccessful download respectively.

There isn’t really any more to say about this one other than the fact that I made the methods in my module chainable for convenience and consistency with other Kohana classes. Note that this means the read_file, read_dir and archive methods will now throw exceptions if you use an invalid file/directory path or your input or output files aren’t readable/writable – so remember to use a try..catch block! I’ve also made the download method take a Request variable as its first argument – simply pass your controllers current request to it like so:

$Zip->download( $this->response )

Read More »

Posted (Updated ) in Database, PHP

NOTE: If you’re looking for the Doctrine 2 modules for K3 see this post.

Download the module here.

I’ve previously written a module for Kohana 3 for Doctrine, as well as drivers for Kohana’s Auth and Session modules, however I’ve finally had the time to merge the three and fix up some longstanding issues present with them; namely the folder structure and lack of PDO support. To make my life a little easier I’ve moved the project to Google Code too.

You can find the project here. It’s got Doctrine 1.23 already in there, you should just be able to drop it in and add it to your bootstrap file. It also includes sample schema and data fixtures for the kohana auth and session modules – they can be found in /models/fixtures.

Good luck!

Read More »

Posted (Updated ) in Database, PHP

Update Feb 22 2012: 1.0.8 is out. Now works with and requires Doctrine 2.2. Thanks to Markus for his patch.

Since releasing my original Doctrine 2 module for Kohana 3, I’ve done a bit of reshuffling of folders and added some additional features from my Doctrine 1.2 module. Due to the extent of modifications, I decided to put up a new post with some added information on how to use the new module.

Features:

  • Doctrine 2 integration (obviously)
  • Auth and Session drivers
  • /doctrine controller to load your schema files and generate the DB tables
  • Works in Kohana 3.0 and 3.1

For the impatient ones out there, here’s the download link:
Doctrine 2 Module for Kohana 3

 

As always, install the module (remembering to add it to your bootstrap file) and download the latest copy of Doctrine 2 from here, placing into the /modules/doctrine2/classes/vendor/doctrine folder. It should be noted that my module is tailored to the tarball download and not the SVN or Github ones which have a slightly different folder structure.

Read More »

Posted (Updated ) in Database, PHP

UPDATE 28 Dec 2010: This module is now defunct. Please instead use the Better Doctrine 2 Module for Kohana 3.

For those of you who don’t already know, Doctrine 2 stable was just released. It features what is essentially a complete rewrite and paradigm shift from the 1.2 version and comes with a laundry list of improvements. You can read the official blog post here. To celebrate the occasion I decided to do up a quick Kohana 3 module. Info & specz below:

Firstly the configuration. You’ll need the following files & folders:
/doctrine/Entities (These are essentially (to my meager knowledge) your model files.)
/doctrine/Proxies
/modules/doctrine/classes/doctrine
/modules/doctrine/init.php

Download the latest copy of Doctrine 2 from here and extract into the /modules/doctrine/classes/doctrine folder.

Here’s /modules/doctrine/init.php

<?php
	use Doctrine\ORM\EntityManager,
		Doctrine\ORM\Configuration;
 
	class Doctrine
	{
		private static $_instance = null;
		private $_application_mode = 'development';
		private $_em = array();
 
		/*
		 * Rename $conn_name to whatever you want your default DB connection to be
		 */
		public static function em( $conn_name = 'default' )
		{
			if ( self::$_instance === null )
				self::$_instance = new Doctrine();
 
			return isset(self::$_instance->em[ $conn_name ])
				? self::$_instance->em[ $conn_name ]
				: reset(self::$_instance->_em);
		}
 
		public function __construct()
		{
			require __DIR__.'/classes/doctrine/Doctrine/Common/ClassLoader.php';
 
			$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', __DIR__.'/classes/doctrine');
			$classLoader->register();
			//This allows Doctrine-CLI tool & YAML mapping driver
			$classLoader = new \Doctrine\Common\ClassLoader('Symfony', __DIR__.'/classes/doctrine/Doctrine');
			$classLoader->register();
			//Load entities
			$classLoader = new \Doctrine\Common\ClassLoader('Entities', APPPATH.'doctrine');
			$classLoader->register();
 
			//Set up caching method
			$cache = $this->_application_mode == 'development'
				? new \Doctrine\Common\Cache\ArrayCache
				: new \Doctrine\Common\Cache\ApcCache;
 
			$config = new Configuration;
			$config->setMetadataCacheImpl( $cache );
			$driver = $config->newDefaultAnnotationDriver( APPPATH.'doctrine/Entities' );
			$config->setMetadataDriverImpl( $driver );
			$config->setQueryCacheImpl( $cache );
 
			$config->setProxyDir( APPPATH.'doctrine/Proxies' );
			$config->setProxyNamespace('Proxies');
			$config->setAutoGenerateProxyClasses( $this->_application_mode == 'development' );
 
			$dbconfs = Kohana::config('database');
 
			foreach ( $dbconfs as $conn_name => $dbconf )
				$this->_em[ $conn_name ] = EntityManager::create(array(
					'dbname' 	=> $dbconf['connection']['database'],
					'user' 		=> $dbconf['connection']['username'],
					'password' 	=> $dbconf['connection']['password'],
					'host' 		=> $dbconf['connection']['hostname'],
					'driver' 	=> 'pdo_mysql',
				), $config);
		}
	}

Once the module is enabled, you can access Doctrine from anywhere with the following code:

Doctrine::em()

The module supports multiple databases and uses the Kohana database config file for connection details. To grab any specific database use the following (where connection_name is the name specified in the config file)

Doctrine::em('connection_name')

Download the complete module here.

Hope this helps.

Read More »

Posted (Updated ) in PHP

I’ve been working with the Zend_Gdata tools lately to perform various Google Calendar operations. One thing I’ve noticed, though, is that there are no tutorials on how to cache the auth request between pages. Caching the authentication would result in one less expensive operation to an external server upon each new file request (The retrieving of a new, working Zend_Gdata_HTTPClient object). Figuring out how to accomplish this seemingly simple task proved a little more difficult than I’d have liked and ironically ended up being very simple 🙂

Read More »

Posted (Updated ) in Database, PHP

Here’s my second Doctrine 1.2 driver for Kohana 3, this time for the Session class. I wrote this because I don’t particularly like the Kohana Database or ORM classes (that or I just don’t want to take the time to learn them!) and it’s released under a creative commons v3 licence So here it is. Enjoy!

Kohana 3 Doctrine Session Driver 1.02 See here for details

UPDATE 22 Oct 2010: Bugfix release – should actually work now 🙂
UPDATE 03 Jan 2011: Fixed rare bug in DB cleaning function.
UPDATE 21 Feb 2011: New module at a new home! See here for details

Read More »

Posted (Updated ) in PHP

Have you ever wanted to generate a resized version of an image just using a HTML image tag and not worrying about entering large chunks of resizing code or storing cached images of various sizes? Today’s tutorial will show you exactly how to do that with Kohana 3. In this tutorial I will teach how to use the URL http://www.mysite.com/sized/image/// to load a dynamically generated, resized image.

The trickiest concept to grasp in this tutorial are the URL segments. Imagine your site was located at http://www.mysite.com and you were browsing to this page http://www.mysite.com/archive/posts/my-post. In this instance, ‘segment 1’ would be archive, segment 2 posts and segment 3 my-post. Segments are split with the / character. In Kohana, URL segments take the following form: controller/method/method_arg_1/method_arg_2/etc…

Read More »

Posted (Updated ) in PHP

As with any project involving generation of .xlsx spreadsheets there’s only really one way to go – PHPExcel. This amazing piece of software boasts a large feature set – allowing quick and (relative) painless generation of XLS, XLSX, PDF, and CSV to name a few as well as importing of the same sans PDF. I recently had to integrate PHPExcel into Kohana 3 so I figured I’d take the time to post the how-to and hopefully make your lives that little bit easier. In addition I’ve included a quick and dirty Spreadsheet class to make XLSX generation that bit faster.

Read More »

Posted (Updated ) in PHP

This is quite possibly one of the simplest integrations in Doctrine history (It’s a 1 liner, folks). The following is a short tutorial on how to create a SwiftMailer module for Kohana 3

Create the following files/folders:
/modules/swiftmailer
/modules/swiftmailer/init.php
/modules/swiftmailer/classes

Inside /modules/swiftmailer/classes/ drop the official latest build of Swift Mailer.

Enter the following into init.php.

<?php
	require Kohana::find_file('classes', 'Swift-4.0.6/lib/swift_required');

There. Wasn’t that easy? Remember to enable the module by adding it to your bootstrap.php file!

Read More »

Posted (Updated ) in PHP

In Kohana 2, html::script() and html::stylesheet() accepted an array or string as their first arguments. This allowed you to easily add multiple scripts and styles simultaneously without needing to write several lines of redundant code. Kohana 3 has removed this useful ability so I thought I’d write a very quick extension to add the feature back in. The following script adds HTML::scripts() and HTML::styles() (As Kohana 3 slightly renamed their functions to HTML::script() and HTML::style() respectively).

Simply add the following file html.php to your /classes directory:

<?php defined('SYSPATH') or die('No direct script access.');
 
class HTML extends Kohana_HTML {
	public static function scripts(array $scripts, $attributes=array(), $index = FALSE)
	{
		$response = '';
 
		//Data sanitisation
		$index = $index ? TRUE : false;
		if ( !is_array($attributes) ) $attributes = array();
 
		foreach ( $scripts as $script )
			$response .= html::script($script, $attributes, $index);
 
		return $response;
	}
 
	public static function styles(array $styles, $attributes=array(), $index = FALSE)
	{
		$response = '';
 
		//Data sanitisation
		$index = $index ? TRUE : false;
		if ( !is_array($attributes) ) $attributes = array();
 
		foreach ( $styles as $style )
			$response .= html::style($style, $attributes, $index);
 
		return $response;
	}
}

Download: html.php

Usage:

<?php
echo HTML::styles(array(
	'assets/css/reset',
	'assets/css/styles',
);
echo HTML::scripts(array(
	'assets/js/jquery',
	'assets/js/jquery.ui.all',
);
?>

Note: Both functions accept the latter 2 parameters available in Kohana 3 HTML::scripts() and HTML::styles(). If used, these arguments will be applied to every element in the first arguments array.

Read More »