Skip to content

Instantly share code, notes, and snippets.

@rdev5
Created May 23, 2013 15:55
Show Gist options
  • Save rdev5/5637130 to your computer and use it in GitHub Desktop.
Save rdev5/5637130 to your computer and use it in GitHub Desktop.
PHP ActiveRecord ORM library loader for CodeIgniter 2.1.3
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function initialize_php_activerecord() {
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50300)
die('PHP ActiveRecord requires PHP 5.3 or higher');
define('PHP_ACTIVERECORD_VERSION_ID','1.0');
// This constant allows you to prepend your file to the autoload stack rather than append it.
if (!defined('PHP_ACTIVERECORD_AUTOLOAD_PREPEND')) {
define('PHP_ACTIVERECORD_AUTOLOAD_PREPEND',true);
}
// This line simply states that if we haven't opted to disable the autoloader, add it to the autoload stack
if (!defined('PHP_ACTIVERECORD_AUTOLOAD_DISABLE')) {
// Because we're prepending - we need to load the library after the models
spl_autoload_register('activerecord_autoload', false, PHP_ACTIVERECORD_AUTOLOAD_PREPEND);
spl_autoload_register('activerecord_lib_autoload', false, PHP_ACTIVERECORD_AUTOLOAD_PREPEND);
}
// The Utils.php file has some namespaced procedural functions, so we must require it manually.
require 'lib/Utils'.EXT;
require 'lib/Exceptions'.EXT;
require 'lib/Expressions'.EXT;
require 'lib/Column'.EXT;
require 'lib/Relationship'.EXT;
require 'lib/Serialization'.EXT;
require 'lib/Validations'.EXT;
// Include the CodeIgniter database config so we can access the variables declared within
include(APPPATH.'config/database'.EXT);
$dsn = array();
if ($db) {
foreach ($db as $name => $db_values) {
// Convert to dsn format
$dsn[$name] = $db[$name]['dbdriver'] .
'://' . $db[$name]['username'] .
':' . $db[$name]['password'] .
'@' . $db[$name]['hostname'] .
'/' . $db[$name]['database'];
}
}
// Initialize ActiveRecord
ActiveRecord\Config::initialize(function($cfg) use ($dsn, $active_group){
$cfg->set_model_directory(APPPATH.'models');
$cfg->set_connections($dsn);
$cfg->set_default_connection($active_group);
});
}
function activerecord_lib_autoload($class_name)
{
$lib_path = APPPATH.'third_party/php-activerecord/lib/';
if (strpos($class_name, 'ActiveRecord') !== FALSE)
{
$class = substr($class_name, strpos($class_name, '\\')+1);
if (file_exists($lib_path.$class.EXT))
require $lib_path.$class.EXT;
}
}
function activerecord_autoload($class_name)
{
$path = ActiveRecord\Config::instance()->get_model_directory();
$root = realpath(isset($path) ? $path : '.');
if (($namespaces = ActiveRecord\get_namespaces($class_name)))
{
$class_name = array_pop($namespaces);
$directories = array();
foreach ($namespaces as $directory)
$directories[] = $directory;
$root .= DIRECTORY_SEPARATOR . implode($directories, DIRECTORY_SEPARATOR);
}
$file = "$root/$class_name".EXT;
if (file_exists($file))
require $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment