I want to use Elastica, a PHP client of ElasticSearch - a search engine somewhat similar to solr.
Now the installation guide of Elastica says to use autoload. But as far as I got it, the autoload features of yii work a little different. I anyway tried and created a symbolic link of the main folder Elastica in components and added to my main.php the line
Now the guide says I can instantiate Elastica with $elasticaClient = new Elastica_Client(); which would call components/Elastica/Client.php because they included this nasty str_replace-feature in their autoload approach:
str_replace('_', '/', $class);
Well, Yii is obviously looking for Elastica_Client.php instead.
How would I do it right in a yii-way in order to be able to instantiate as mentioned and not touching the Elastica code or file structure?
But since ElasticSearch as well as Elastica are in a fast development cycle and I would like to be able to simply pull the updates from GitHub - without renaming the 89 files each time - it doens’t seem to be the ideal solution.
Thank you for your solution. To write my own autoloader extension is probably beyond my current level of knowledge of Yii as well as PHP but I will certainly keep it in mind for later.
Maybe something like this. It’s only an idea, not tested (don’t know anything about Elastica).
Create Application component ‘ElasticaLoader.php’ in protected/components folder.
class ElasticaLoader extends CApplicationComponent
{
public $libPath;
//see: http://elastica.io/en#section-include
public function __autoload_elastica ($class)
{
$path = str_replace('_', '/', $class);
if (file_exists($this->libPath . $path . '.php')) {
require_once($this->libPath . $path . '.php');
}
}
public function init()
{
Yii::registerAutoloader(array($this,'__autoload_elastica'),true);
}
}
Preload the component in config/main.php
...
'preload'=>array('log','importelastica'),
...
'components'=>array(
...
'importelastica'=>array(
'class' => 'application.components.ElasticaLoader',
'libPath' => '/var/www/', //assume you installed Elastica to /var/www/Elastica
),
...
If you don’t need to ‘preload’ in the application, remove importelastica from ‘preload’ and call ‘Yii::app()->getComponent(‘importelastica’);’ in a controllers/modules init() or actionX method.