Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changes to facilitate use with modular separation/HMVC.

Changed ActiveRecord\Config to use an array for the model directory locations.
Modified the activerecord_autoload() function to search within the array for the needed model class.
Added PHPActiveRecord::add_model_path() method to add model paths relative to CI APPPATH constant.
Example, in a controller: $this->phpactiverecord->add_model_path('modules/users/models');
  • Loading branch information...
commit ff6be367bd1e16352eb2241c46909e30062229ef 1 parent 59f2fe6
@anaxamaxan authored
View
40 libraries/PHPActiveRecord.php
@@ -14,15 +14,15 @@ public function __construct()
$spark_path = dirname(__DIR__).'/';
// Include the CodeIgniter database config file
- // Is the config file in the environment folder?
- if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php'))
- {
- if ( ! file_exists($file_path = APPPATH.'config/database.php'))
+ // Is the config file in the environment folder?
+ if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php'))
{
- show_error('PHPActiveRecord: The configuration file database.php does not exist.');
+ if ( ! file_exists($file_path = APPPATH.'config/database.php'))
+ {
+ show_error('PHPActiveRecord: The configuration file database.php does not exist.');
+ }
}
- }
- require($file_path);
+ require($file_path);
// Include the ActiveRecord bootstrapper
require_once $spark_path.'vendor/php-activerecord/ActiveRecord.php';
@@ -32,20 +32,19 @@ public function __construct()
if ($db && $active_group)
{
- foreach ($db as $conn_name => $conn)
+ foreach ($db as $conn_name => $conn)
{
// Build the DSN string for each connection
$connections[$conn_name] = $conn['dbdriver'].
'://' .$conn['username'].
':' .$conn['password'].
'@' .$conn['hostname'].
- '/' .$conn['database'].
- '?charset='. $db[$name]['char_set'];
+ '/' .$conn['database'];
}
// Initialize PHPActiveRecord
ActiveRecord\Config::initialize(function ($cfg) use ($connections, $active_group) {
- $cfg->set_model_directory(APPPATH.'models/');
+ $cfg->add_model_directory(APPPATH.'models/');
$cfg->set_connections($connections);
// This connection is the default for all models
@@ -54,6 +53,25 @@ public function __construct()
}
}
+
+ /**
+ * Add a model path for the AR autoloader to search
+ *
+ * @param string $path
+ * @return bool
+ */
+ public function add_model_path($path = '')
+ {
+ try {
+ ActiveRecord\Config::instance()->add_model_directory(APPPATH.$path);
+ }
+ catch (ActiveRecord\ConfigException $cx)
+ {
+ log_message('error','PHPActiveRecord Exception: '.$cx->getMessage());
+ return false;
+ }
+ }
+
}
/* End of file PHPActiveRecord.php */
View
42 vendor/php-activerecord/ActiveRecord.php
@@ -27,23 +27,33 @@
function activerecord_autoload($class_name)
{
- $path = ActiveRecord\Config::instance()->get_model_directory();
- $root = realpath(isset($path) ? $path : '.');
-
- if (($namespaces = ActiveRecord\get_namespaces($class_name)))
+ $paths = ActiveRecord\Config::instance()->get_model_directories();
+
+ //within each model directory, look for the model.
+ foreach((array)$paths as $path)
{
- $class_name = array_pop($namespaces);
- $directories = array();
-
- foreach ($namespaces as $directory)
- $directories[] = $directory;
+ $root = realpath($path);
+
+ //namespace within the $class_name? if so, look in subdir based on namespace
+ 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);
+ }
+
+ //if file exists, include it.
+ $file = "$root/$class_name.php";
+
+ if (file_exists($file))
+ {
+ require $file;
+ if(class_exists($class_name, false)) break; //make double-sure the file actually contains the class we're after
+ }
- $root .= DIRECTORY_SEPARATOR . implode($directories, DIRECTORY_SEPARATOR);
}
-
- $file = "$root/$class_name.php";
-
- if (file_exists($file))
- require $file;
+
}
-?>
View
50 vendor/php-activerecord/lib/Config.php
@@ -59,6 +59,14 @@ class Config extends Singleton
private $model_directory;
/**
+ * Directory for the auto_loading of model classes.
+ *
+ * @see activerecord_autoload
+ * @var array
+ */
+ private $model_directories = array();
+
+ /**
* Switch for logging.
*
* @var bool
@@ -191,27 +199,44 @@ public function set_default_connection($name)
/**
* Sets the directory where models are located.
- *
+ *
+ * @deprecated Use add_model_directory() instead.
* @param string $dir Directory path containing your models
* @return void
*/
public function set_model_directory($dir)
{
- $this->model_directory = $dir;
+ $this->add_model_directory($dir);
}
-
+
/**
- * Returns the model directory.
- *
- * @return string
- * @throws ConfigException if specified directory was not found
+ * Add a directory to the model directories array.
+ *
+ * @throws ConfigException
+ * @param string $dir
+ * @return void
*/
- public function get_model_directory()
+ public function add_model_directory($dir = '')
{
- if (!file_exists($this->model_directory))
- throw new ConfigException('Invalid or non-existent directory: '.$this->model_directory);
-
- return $this->model_directory;
+ if(empty($dir))
+ throw new ConfigException('Cannot add an empty string to model directories.');
+
+ if (!file_exists($dir))
+ throw new ConfigException('Cannot add invalid or non-existent model directory: '.$dir);
+
+ //put new directory at the beginning of the array, since client code will
+ // tend to add a model directory right before using a model in that directory.
+ array_unshift($this->model_directories,$dir);
+ }
+
+ /**
+ * Get an array of model directories.
+ *
+ * @return array
+ */
+ public function get_model_directories()
+ {
+ return $this->model_directories;
}
/**
@@ -301,4 +326,3 @@ public function set_cache($url, $options=array())
Cache::initialize($url,$options);
}
};
-?>
Please sign in to comment.
Something went wrong with that request. Please try again.