Skip to content

Commit

Permalink
Fix for issue #9:Error when using shell/indexer.php in Magento 1.7
Browse files Browse the repository at this point in the history
Moved autoloader to a helper class.
Extra check if autoloader is registered when creating Engine instance.
  • Loading branch information
jeroenvermeulen committed May 3, 2014
1 parent e80a1bb commit 745b37e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
53 changes: 53 additions & 0 deletions app/code/community/JeroenVermeulen/Solarium/Helper/Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* JeroenVermeulen_Solarium
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this Module to
* newer versions in the future.
*
* @category JeroenVermeulen
* @package JeroenVermeulen_Solarium
* @copyright Copyright (c) 2014 Jeroen Vermeulen (http://www.jeroenvermeulen.eu)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

class JeroenVermeulen_Solarium_Helper_Autoloader extends Mage_Core_Helper_Abstract
{

/**
* Prepends our autoloader, so we can load the extra library classes when they are needed.
*/
public function register() {
$registryKey = 'JeroenVermeulen_Solarium_Autoloader_registered';
if ( ! Mage::registry( $registryKey ) ) {
Mage::register( $registryKey, true );
spl_autoload_register( array( $this, 'load' ), true, true );
}
}

/**
* This function can autoload classes starting with:
* - Solarium
* - Symfony\Component\EventDispatcher
*
* @param string $class
*/
public static function load( $class ) {
if ( preg_match( '#^(Solarium|Symfony\\\\Component\\\\EventDispatcher)\b#', $class ) ) {
$phpFile = Mage::getBaseDir( 'lib' ) . DIRECTORY_SEPARATOR
. str_replace( '\\', DIRECTORY_SEPARATOR, $class ) . '.php';
/** @noinspection PhpIncludeInspection */
require_once( $phpFile );
}
}

}
2 changes: 2 additions & 0 deletions app/code/community/JeroenVermeulen/Solarium/Model/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public static function getConf( $setting, $storeId = null ) {
* @see https://github.com/basdenooijer/solarium/issues/254
*/
public function __construct() {
// Make sure the autoloader is registered, because in Magento < 1.8 some events are missing.
Mage::helper( 'jeroenvermeulen_solarium/autoloader' )->register();
$helper = Mage::helper( 'jeroenvermeulen_solarium' );
if ( self::isEnabled() ) {
$host = trim( self::getConf( 'server/host' ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

class JeroenVermeulen_Solarium_Model_Observer_Autoloader extends Varien_Event_Observer
class JeroenVermeulen_Solarium_Model_Observer extends Varien_Event_Observer
{

/**
* This an observer function for the event 'controller_front_init_before'.
* It prepends our autoloader, so we can load the extra libraries.
*
* @param Varien_Event_Observer $observer
*/
public function controllerFrontInitBefore( /** @noinspection PhpUnusedParameterInspection */ $observer ) {
$this->_registerAutoLoader();
Mage::helper( 'jeroenvermeulen_solarium/autoloader' )->register();
}

/**
Expand All @@ -41,34 +40,7 @@ public function controllerFrontInitBefore( /** @noinspection PhpUnusedParameterI
* @param Varien_Event_Observer $observer
*/
public function shellReindexInitProcess( /** @noinspection PhpUnusedParameterInspection */ $observer ) {
$this->_registerAutoLoader();
}

/**
* This function can autoload classes starting with:
* - Solarium
* - Symfony\Component\EventDispatcher
*
* @param string $class
*/
public static function load( $class ) {
if ( preg_match( '#^(Solarium|Symfony\\\\Component\\\\EventDispatcher)\b#', $class ) ) {
$phpFile = Mage::getBaseDir( 'lib' ) . DIRECTORY_SEPARATOR
. str_replace( '\\', DIRECTORY_SEPARATOR, $class ) . '.php';
/** @noinspection PhpIncludeInspection */
require_once( $phpFile );
}
}

/**
* Prepends our autoloader, so we can load the extra libraries.
*/
private function _registerAutoLoader() {
static $registered;
if ( empty( $registered ) ) {
$registered = true;
spl_autoload_register( array( $this, 'load' ), true, true );
}
Mage::helper( 'jeroenvermeulen_solarium/autoloader' )->register();
}

}
5 changes: 3 additions & 2 deletions app/code/community/JeroenVermeulen/Solarium/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@
<controller_front_init_before>
<observers>
<jeroenvermeulen_solarium_controller_front_init_before>
<class>jeroenvermeulen_solarium/observer_autoloader</class>
<class>jeroenvermeulen_solarium/observer</class>
<method>controllerFrontInitBefore</method>
</jeroenvermeulen_solarium_controller_front_init_before>
</observers>
</controller_front_init_before>
<shell_reindex_init_process>
<!-- This event only works for Magento >= 1.8 -->
<observers>
<jeroenvermeulen_solarium_shell_reindex_init_process>
<class>jeroenvermeulen_solarium/observer_autoloader</class>
<class>jeroenvermeulen_solarium/observer</class>
<method>shellReindexInitProcess</method>
</jeroenvermeulen_solarium_shell_reindex_init_process>
</observers>
Expand Down

0 comments on commit 745b37e

Please sign in to comment.