[eluser]tonanbarbarian[/eluser]
So not wanting to break what in Matchbox but still wanting my code to run without having to add a _remap or lots of callers I came up with the following modification to the Matchbox library
It is a change to the find method
but first I add 3 more properties
Code:
/**
* @sm2mod
* Should the system track modules rather than callers
*
* @var boolean
*/
var $_track_modules = false;
/**
* @sm2mod
* Modules that have been loaded and should be searched for a resource
* Only used if we are tracking modules
*
* @var array
*/
var $loaded_modules = array();
/**
* @sm2mod
* Directories to look at based on the modules and directory properties
* Only used if we are tracking modules
*
* @var array
*/
var $test_directories = array();
then we modify find
Code:
/**
* Locates resources
*
* @param string
* @param string
* @param string
* @param string
* $param integer
* @return mixed
* @access public
*/
function find($resource, $module = '', $search = 1)
{
log_message('debug', '---Matchbox---');
log_message('debug', 'Finding: ' . $resource.'::'.$module);
// @sm2mod
if ($this->_track_modules) {
if (!in_array($this->_module, $this->loaded_modules)) {
$this->loaded_modules[] = $this->_module;
foreach ($this->directory_array() as $directory) {
$this->test_directories[] = APPPATH . $directory . '/' . $this->_module . '/';
}
}
if ($module !== '') {
$this->loaded_modules[] = $module;
foreach ($this->directory_array() as $directory) {
$this->test_directories[] = APPPATH . $directory . '/' . $module . '/';
}
}
$directories = $this->test_directories;
if ($search == 3) {
$directories[] = '';
} else {
$directories[] = APPPATH;
if ($search == 2) {
$directories[] = BASEPATH;
}
}
} else {
// original matchbox code
$directories = array();
if ($module !== '') {
foreach ($this->directory_array() as $directory) {
$directories[] = APPPATH . $directory . '/' . $module . '/';
}
} else {
$caller = $this->detect_caller();
log_message('debug', 'No module so using: ' . $caller);
foreach ($this->directory_array() as $directory) {
$directories[] = APPPATH . $directory . '/' . $caller . '/';
}
if ($search == 3) {
$directories[] = '';
} else {
$directories[] = APPPATH;
if ($search == 2) {
$directories[] = BASEPATH;
}
}
}
}
foreach ($directories as $directory) {
$filepath = $directory . $resource;
log_message('debug', 'Looking in: ' . $filepath);
if (file_exists($filepath)) {
log_message('debug', 'Found');
log_message('debug', '--------------');
return $filepath;
}
}
log_message('debug', 'Not found');
log_message('debug', '--------------');
return false;
}
The end result of this being that Matchbox operates as normal, until you change $this->_track_modules to true.
Then it keeps track of all of the modules actually requested in the find calls, and only looks there for the resource.
It will look in the module that was called in the uri first and then any of the extra modules that were specifically called.
To me this is a more logical way of doing things, and as an added bonus it is twice as fast as the backtrace because there is not as much information to process on each find.
I am not sure if this modification will ever make it into the main Matchbox code, but I feel it has use in improving performance, and working with _remap and other libraries based processing easier.
And you can always turn it on specifically in each controller that needs it.