Getting started with MageBridge - Yireo

MageBridge from the command-line

Using MageBridge you can easily call Magento API-methods from within Joomla! to make your own extensions. But sometimes it might also be needed to use this functionality from the command-line, for instance when migrating to new sites or when running cronjobs. Luckily enough, the Joomla! Framework allows for easy script building, and this tutorial shows you how.

Initializing the Joomla! Framework

The Joomla! Framework is a set of classes that can be used easily in custom PHP applications. When including the Joomla! class-files, it is necessary to bypass the _JEXEC security check present in all PHP-files of Joomla!. Also, Joomla! needs to know which directory is the Joomla! base-directory. So our PHP-script starts off with the definition of some constant variables:

<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );

Note that the Joomla! base-directory is in this case the parent-folder of the folder where we keep this script (JOOMLA/tmp/sample.php). It might be a different directory in your case, so you might just as well define the absolute path to Joomla!:

define( 'JPATH_BASE', '/var/www/html/joomla' );

After this, we can change directory to the Joomla! base and include the PHP-scripts of the Joomla! Framework.

if(!is_file(JPATH_BASE.DS.'includes'.DS.'framework.php')) {    
die('Incorrect Joomla! base-path');
}
chdir(JPATH_BASE);
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

Starting the Joomla! application

At this point, the framework is there and available for use. The first step is to create a new instance of an application (backend or frontend) and initialise it. In this case, we are just going to initialize the frontend.

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

And now the fun starts. We can start using queries using the Joomla! Database Layer:

$db = JFactory::getDBO();

But you can do other things as well. In this case we are going to use MageBridge to fetch data from Magento.

 

Initializing MageBridge

With most other Joomla! extensions, you need to include every PHP-class manually but MageBridge is shipped with a so-called loader that can automatically load the right PHP class-file when a specific class is asked for. This eases the use of MageBridge elements tremenduously. So the first thing we are going to do is include the loader:

include_once JPATH_SITE.DS.'components'.DS.'com_magebridge'.DS.'helpers'.DS.'loader.php';

After this, we can use the bridge-classes as we see fit. You can read more about the bridge-classes in other tutorials. The main point is that when you need something from Magento, you first need to register your request, next build the bridge, and than we are going to fetch our completed request. For this we need the following two classes:

$register = MageBridgeModelRegister::getInstance();
$bridge = MageBridgeModelBridge::getInstance();

Getting what we want

Once we have these in place, we can add our request to the register. In this case, we are also going to clean the register first - the register has some cool tricks to preload Joomla! modules, but from the command-line we don't need things like that.

$register->clean();
$id = $register->add('api', 'catalog_product.list', array());

The request is registered, so if the bridge is being built, the request is sent to Magento and (hopefully) completed with data.

$bridge->build();
$products = $register->getDataById($id);

Because we have used the generic Magento API to fetch products, it's best to consult the Magento Wiki for more details. All the arguments that apply to the Magento API (called through SOAP or XML-RPC) also apply to MageBridge.

Created on Saturday, 11 September 2010
Modified on Saturday, 11 September 2010

About Yireo

Yireo tries to help webdevelopers build successful Joomla! and Magento sites.

More about Yireo