Mage Class Reference

List of all members.

Static Public Member Functions

static getVersion ()
static reset ()
static register ($key, $value, $graceful=false)
static unregister ($key)
static registry ($key)
static setRoot ($appRoot='')
static getRoot ()
static objects ($key=null)
static getBaseDir ($type='base')
static getModuleDir ($type, $moduleName)
static getStoreConfig ($path, $id=null)
static getStoreConfigFlag ($path, $id=null)
static getBaseUrl ($type=Mage_Core_Model_Store::URL_TYPE_LINK, $secure=null)
static getUrl ($route='', $params=array())
static getDesign ()
static getConfig ()
static addObserver ($eventName, $callback, $data=array(), $observerName='', $observerClass='')
static dispatchEvent ($name, array $data=array())
static getModel ($modelClass='', $arguments=array())
static getSingleton ($modelClass='', array $arguments=array())
static getResourceModel ($modelClass, $arguments=array())
static getResourceSingleton ($modelClass='', array $arguments=array())
static getBlockSingleton ($type)
static helper ($name)
static exception ($module='Mage_Core', $message='', $code=0)
static throwException ($message, $messageStorage=null)
static app ($code= '', $type= 'store', $options=array())
static run ($code= '', $type= 'store', $options=array())
static isInstalled ($options=array())
static log ($message, $level=null, $file= '')
static logException (Exception $e)
static setIsDeveloperMode ($mode)
static getIsDeveloperMode ()
static printException (Exception $e, $extra= '')
static getScriptSystemUrl ($folder, $exitIfNot=false)
static setIsDownloader ($flag=true)

Static Public Attributes

static $headersSentThrowsException = true


Detailed Description

Main Mage hub class

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 60 of file Mage.php.


Member Function Documentation

static addObserver ( eventName,
callback,
data = array(),
observerName = '',
observerClass = '' 
) [static]

Add observer to even object

Parameters:
string $eventName
callback $callback
array $arguments
string $observerName

Definition at line 281 of file Mage.php.

00282     {
00283         if ($observerClass=='') {
00284             $observerClass = 'Varien_Event_Observer';
00285         }
00286         $observer = new $observerClass();
00287         $observer->setName($observerName)->addData($data)->setEventName($eventName)->setCallback($callback);
00288         return Mage::registry('events')->addObserver($observer);
00289     }

static app ( code = '',
type = 'store',
options = array() 
) [static]

Initialize and retrieve application

Parameters:
string $code
string $type
string|array $options
Returns:
Mage_Core_Model_App

Definition at line 418 of file Mage.php.

00419     {
00420         if (null === self::$_app) {
00421             Varien_Profiler::start('mage::app::construct');
00422             self::$_app = new Mage_Core_Model_App();
00423             Varien_Profiler::stop('mage::app::construct');
00424 
00425             Mage::setRoot();
00426             Mage::register('events', new Varien_Event_Collection());
00427 
00428 
00429             Varien_Profiler::start('mage::app::register_config');
00430             Mage::register('config', new Mage_Core_Model_Config());
00431             Varien_Profiler::stop('mage::app::register_config');
00432 
00433             Varien_Profiler::start('mage::app::init');
00434             self::$_app->init($code, $type, $options);
00435             Varien_Profiler::stop('mage::app::init');
00436 
00437             self::$_app->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);
00438         }
00439         return self::$_app;
00440     }

static dispatchEvent ( name,
array data = array() 
) [static]

Dispatch event

Calls all observer callbacks registered for this event and multiobservers matching event name pattern

Parameters:
string $name
array $args

Definition at line 300 of file Mage.php.

00301     {
00302         Varien_Profiler::start('DISPATCH EVENT:'.$name);
00303         $result = Mage::app()->dispatchEvent($name, $data);
00304         #$result = Mage::registry('events')->dispatch($name, $data);
00305         Varien_Profiler::stop('DISPATCH EVENT:'.$name);
00306         return $result;
00307     }

static exception ( module = 'Mage_Core',
message = '',
code = 0 
) [static]

Return new exception by module to be thrown

Parameters:
string $module
string $message
integer $code

Definition at line 396 of file Mage.php.

00397     {
00398         $className = $module.'_Exception';
00399         return new $className($message, $code);
00400     }

static getBaseDir ( type = 'base'  )  [static]

Retrieve application root absolute path

Returns:
string

Definition at line 205 of file Mage.php.

00206     {
00207         return Mage::getConfig()->getOptions()->getDir($type);
00208     }

static getBaseUrl ( type = Mage_Core_Model_Store::URL_TYPE_LINK,
secure = null 
) [static]

Get base URL path by type

Parameters:
string $type
Returns:
string

Definition at line 236 of file Mage.php.

00237     {
00238         return Mage::app()->getStore()->getBaseUrl($type, $secure);
00239     }

static getBlockSingleton ( type  )  [static]

Deprecated, use Mage.helper()

Parameters:
string $type
Returns:
object

Definition at line 372 of file Mage.php.

00373     {
00374         $action = Mage::app()->getFrontController()->getAction();
00375         return $action ? $action->getLayout()->getBlockSingleton($type) : false;
00376     }

static getConfig (  )  [static]

Get a config object

Returns:
Mage_Core_Model_Config

Definition at line 268 of file Mage.php.

00269     {
00270         return Mage::registry('config');
00271     }

static getDesign (  )  [static]

Get design package singleton

Returns:
Mage_Core_Model_Design_Package

Definition at line 258 of file Mage.php.

00259     {
00260         return Mage::getSingleton('core/design_package');
00261     }

static getIsDeveloperMode (  )  [static]

Retrieve enabled developer mode

Returns:
bool

Definition at line 619 of file Mage.php.

00620     {
00621         return self::$_isDeveloperMode;
00622     }

static getModel ( modelClass = '',
arguments = array() 
) [static]

Retrieve model object

string $modelClass array $arguments Mage_Core_Model_Abstract

Definition at line 317 of file Mage.php.

00318     {
00319         return Mage::getConfig()->getModelInstance($modelClass, $arguments);
00320     }

static getModuleDir ( type,
moduleName 
) [static]

Definition at line 210 of file Mage.php.

00211     {
00212         return Mage::getConfig()->getModuleDir($type, $moduleName);
00213     }

static getResourceModel ( modelClass,
arguments = array() 
) [static]

Retrieve object of resource model

Parameters:
string $modelClass
array $arguments
Returns:
Object

Definition at line 345 of file Mage.php.

00346     {
00347         return Mage::getConfig()->getResourceModelInstance($modelClass, $arguments);
00348     }

static getResourceSingleton ( modelClass = '',
array arguments = array() 
) [static]

Retrieve resource vodel object singleton

Parameters:
string $modelClass
array $arguments
Returns:
object

Definition at line 357 of file Mage.php.

00358     {
00359         $registryKey = '_resource_singleton/'.$modelClass;
00360         if (!Mage::registry($registryKey)) {
00361             Mage::register($registryKey, Mage::getResourceModel($modelClass, $arguments));
00362         }
00363         return Mage::registry($registryKey);
00364     }

static getRoot (  )  [static]

Get application root absolute path

Returns:
string

Definition at line 177 of file Mage.php.

00178     {
00179         return Mage::registry('appRoot');
00180     }

static getScriptSystemUrl ( folder,
exitIfNot = false 
) [static]

Define system folder directory url by virtue of running script directory name Try to find requested folder by shifting to domain root directory

Parameters:
string $folder
boolean $exitIfNot
Returns:
string

Definition at line 686 of file Mage.php.

00687     {
00688         $runDirUrl  = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
00689         $runDir     = rtrim(dirname($_SERVER['SCRIPT_FILENAME']), DS);
00690 
00691         $baseUrl    = null;
00692         if (is_dir($runDir.'/'.$folder)) {
00693             $baseUrl = str_replace(DS, '/', $runDirUrl);
00694         } else {
00695             $runDirUrlArray = explode('/', $runDirUrl);
00696             $runDirArray = explode('/', $runDir);
00697             $count       = count($runDirArray);
00698 
00699             for ($i=0; $i < $count; $i++) {
00700                 array_pop($runDirUrlArray);
00701                 array_pop($runDirArray);
00702                 $_runDir = implode('/', $runDirArray);
00703                 if (!empty($_runDir)) {
00704                     $_runDir .= '/';
00705                 }
00706 
00707                 if (is_dir($_runDir.$folder)) {
00708                     $_runDirUrl = implode('/', $runDirUrlArray);
00709                     $baseUrl = str_replace(DS, '/', $_runDirUrl);
00710                     break;
00711                 }
00712             }
00713         }
00714 
00715         if (is_null($baseUrl)) {
00716             $errorMessage = "Unable detect system directory: $folder";
00717             if ($exitIfNot) {
00718                 // exit because of infinity loop
00719                 exit($errorMessage);
00720             } else {
00721                 self::printException(new Exception(), $errorMessage);
00722             }
00723         }
00724 
00725         return $baseUrl;
00726     }

static getSingleton ( modelClass = '',
array arguments = array() 
) [static]

Retrieve model object singleton

Parameters:
string $modelClass
array $arguments
Returns:
Mage_Core_Model_Abstract

Definition at line 329 of file Mage.php.

00330     {
00331         $registryKey = '_singleton/'.$modelClass;
00332         if (!Mage::registry($registryKey)) {
00333             Mage::register($registryKey, Mage::getModel($modelClass, $arguments));
00334         }
00335         return Mage::registry($registryKey);
00336     }

static getStoreConfig ( path,
id = null 
) [static]

Definition at line 215 of file Mage.php.

00216     {
00217         return self::app()->getStore($id)->getConfig($path);
00218     }

static getStoreConfigFlag ( path,
id = null 
) [static]

Definition at line 220 of file Mage.php.

00221     {
00222         $flag = strtolower(Mage::getStoreConfig($path, $id));
00223         if (!empty($flag) && 'false'!==$flag && '0'!==$flag) {
00224             return true;
00225         } else {
00226             return false;
00227         }
00228     }

static getUrl ( route = '',
params = array() 
) [static]

Generate url by route and parameters

Parameters:
string $route
array $params
Returns:
string

Definition at line 248 of file Mage.php.

00249     {
00250         return Mage::getModel('core/url')->getUrl($route, $params);
00251     }

static getVersion (  )  [static]

Definition at line 85 of file Mage.php.

00086     {
00087         return '1.3.2.1';
00088     }

static helper ( name  )  [static]

Retrieve helper object

Parameters:
helper name $name
Returns:
Mage_Core_Helper_Abstract

Definition at line 384 of file Mage.php.

00385     {
00386         return Mage::app()->getHelper($name);
00387     }

static isInstalled ( options = array()  )  [static]

Retrieve application installation flag

Parameters:
string|array $options
Returns:
bool

Definition at line 506 of file Mage.php.

00507     {
00508         $isInstalled = self::registry('_is_installed');
00509         if ($isInstalled === null) {
00510             self::setRoot();
00511 
00512             if (is_string($options)) {
00513                 $options = array(
00514                     'etc_dir' => $options
00515                 );
00516             }
00517             $etcDir = 'etc';
00518             if (!empty($options['etc_dir'])) {
00519                 $etcDir = $options['etc_dir'];
00520             }
00521             $localConfigFile = self::getRoot() . DS . $etcDir . DS . 'local.xml';
00522 
00523             $isInstalled = false;
00524 
00525             if (is_readable($localConfigFile)) {
00526                 $localConfig = simplexml_load_file($localConfigFile);
00527                 date_default_timezone_set('UTC');
00528                 if (($date = $localConfig->global->install->date) && strtotime($date)) {
00529                     $isInstalled = true;
00530                 }
00531             }
00532             self::register('_is_installed', $isInstalled);
00533         }
00534         return $isInstalled;
00535     }

static log ( message,
level = null,
file = '' 
) [static]

log facility (??)

Parameters:
string $message
integer $level
string $file

Definition at line 544 of file Mage.php.

00545     {
00546         if (!self::getConfig()) {
00547             return;
00548         }
00549         if (!Mage::getStoreConfig('dev/log/active')) {
00550             return;
00551         }
00552 
00553         static $loggers = array();
00554 
00555         $level  = is_null($level) ? Zend_Log::DEBUG : $level;
00556         if (empty($file)) {
00557             $file = Mage::getStoreConfig('dev/log/file');
00558             $file   = empty($file) ? 'system.log' : $file;
00559         }
00560 
00561         try {
00562             if (!isset($loggers[$file])) {
00563                 $logFile = Mage::getBaseDir('var').DS.'log'.DS.$file;
00564                 $logDir = Mage::getBaseDir('var').DS.'log';
00565 
00566                 if (!is_dir(Mage::getBaseDir('var').DS.'log')) {
00567                     mkdir(Mage::getBaseDir('var').DS.'log', 0777);
00568                 }
00569 
00570                 if (!file_exists($logFile)) {
00571                     file_put_contents($logFile,'');
00572                     chmod($logFile, 0777);
00573                 }
00574 
00575                 $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
00576                 $formatter = new Zend_Log_Formatter_Simple($format);
00577                 $writer = new Zend_Log_Writer_Stream($logFile);
00578                 $writer->setFormatter($formatter);
00579                 $loggers[$file] = new Zend_Log($writer);
00580             }
00581 
00582             if (is_array($message) || is_object($message)) {
00583                 $message = print_r($message, true);
00584             }
00585 
00586             $loggers[$file]->log($message, $level);
00587         }
00588         catch (Exception $e){
00589 
00590         }
00591     }

static logException ( Exception $  e  )  [static]

Definition at line 593 of file Mage.php.

00594     {
00595         if (!self::getConfig()) {
00596             return;
00597         }
00598         $file = Mage::getStoreConfig('dev/log/exception_file');
00599         self::log("\n".$e->getTraceAsString(), Zend_Log::ERR, $file);
00600     }

static objects ( key = null  )  [static]

Varien Objects Cache

Parameters:
string $key optional, if specified will load this key
Returns:
Varien_Object_Cache

Definition at line 188 of file Mage.php.

00189     {
00190         if (!self::$_objects) {
00191             self::$_objects = new Varien_Object_Cache;
00192         }
00193         if (is_null($key)) {
00194             return self::$_objects;
00195         } else {
00196             return self::$_objects->load($key);
00197         }
00198     }

static printException ( Exception $  e,
extra = '' 
) [static]

Display exception

Parameters:
Exception $e

Definition at line 629 of file Mage.php.

00630     {
00631         if (self::$_isDeveloperMode) {
00632             print '<pre>';
00633 
00634             if (!empty($extra)) {
00635                 print $extra . "\n\n";
00636             }
00637 
00638             print $e->getMessage() . "\n\n";
00639             print $e->getTraceAsString();
00640             print '</pre>';
00641         }
00642         else {
00643             self::getConfig()->createDirIfNotExists(self::getBaseDir('var') . DS . 'report');
00644             $reportId   = intval(microtime(true) * rand(100, 1000));
00645             $reportFile = self::getBaseDir('var') . DS . 'report' . DS . $reportId;
00646             $reportData = array(
00647                 !empty($extra) ? $extra . "\n\n" : '' . $e->getMessage(),
00648                 $e->getTraceAsString()
00649             );
00650             $reportData = serialize($reportData);
00651 
00652             file_put_contents($reportFile, $reportData);
00653             chmod($reportFile, 0777);
00654 
00655             $storeCode = 'default';
00656             try {
00657                 $storeCode = self::app()->getStore()->getCode();
00658             }
00659             catch (Exception $e) {}
00660 
00661             $baseUrl = self::getScriptSystemUrl('report', true);
00662             $reportUrl = rtrim($baseUrl, '/') . '/report/?id='
00663             . $reportId . '&s=' . $storeCode;
00664 
00665             if (!headers_sent()) {
00666                 header('Location: ' . $reportUrl);
00667             }
00668             else {
00669                 print '<script type="text/javascript">';
00670                 print "window.location.href = '{$reportUrl}';";
00671                 print '</script>';
00672             }
00673         }
00674 
00675         die();
00676     }

static register ( key,
value,
graceful = false 
) [static]

Register a new variable

Parameters:
string $key
mixed $value
bool $graceful

Definition at line 112 of file Mage.php.

00113     {
00114         if(isset(self::$_registry[$key])) {
00115             if ($graceful) {
00116                 return;
00117             }
00118             Mage::throwException('Mage registry key "'.$key.'" already exists');
00119         }
00120         self::$_registry[$key] = $value;
00121     }

static registry ( key  )  [static]

Retrieve a value from registry by a key

Parameters:
string $key
Returns:
mixed

Definition at line 139 of file Mage.php.

00140     {
00141         if (isset(self::$_registry[$key])) {
00142             return self::$_registry[$key];
00143         }
00144         return null;
00145     }

static reset (  )  [static]

Set all my static data to defaults

Definition at line 94 of file Mage.php.

00095     {
00096         self::$_registry = array();
00097         self::$_app      = null;
00098         self::$_useCache = array();
00099         self::$_objects  = null;
00100         self::$_isDownloader    = false;
00101         self::$_isDeveloperMode = false;
00102         // do not reset $headersSentThrowsException
00103     }

static run ( code = '',
type = 'store',
options = array() 
) [static]

Front end main entry point

Parameters:
string $code
string $type
string|array $options

Definition at line 449 of file Mage.php.

00450     {
00451         try {
00452             Varien_Profiler::start('mage');
00453 
00454             Varien_Profiler::start('mage::app');
00455             self::app($code, $type, $options);
00456             Varien_Profiler::stop('mage::app');
00457 
00458             Varien_Profiler::start('mage::dispatch');
00459             self::app()->getFrontController()->dispatch();
00460             Varien_Profiler::stop('mage::dispatch');
00461 
00462             Varien_Profiler::stop('mage');
00463         }
00464         catch (Mage_Core_Model_Session_Exception $e) {
00465             header('Location: ' . Mage::getBaseUrl());
00466             die();
00467         }
00468         catch (Mage_Core_Model_Store_Exception $e) {
00469             $baseUrl = self::getScriptSystemUrl('404');
00470             if (!headers_sent()) {
00471                 header('Location: ' . rtrim($baseUrl, '/').'/404/');
00472             }
00473             else {
00474                 print '<script type="text/javascript">';
00475                 print "window.location.href = '{$baseUrl}';";
00476                 print '</script>';
00477             }
00478             die();
00479         }
00480         catch (Exception $e) {
00481             if (self::isInstalled() || self::$_isDownloader) {
00482                 self::printException($e);
00483                 exit();
00484             }
00485             try {
00486                 self::dispatchEvent('mage_run_exception', array('exception' => $e));
00487                 if (!headers_sent()) {
00488                     header('Location:'.self::getUrl('install'));
00489                 }
00490                 else {
00491                     self::printException($e);
00492                 }
00493             }
00494             catch (Exception $ne) {
00495                 self::printException($ne, $e->getMessage());
00496             }
00497         }
00498     }

static setIsDeveloperMode ( mode  )  [static]

Set enabled developer mode

Parameters:
bool $mode
Returns:
bool

Definition at line 608 of file Mage.php.

00609     {
00610         self::$_isDeveloperMode = (bool)$mode;
00611         return self::$_isDeveloperMode;
00612     }

static setIsDownloader ( flag = true  )  [static]

Definition at line 728 of file Mage.php.

00729     {
00730         self::$_isDownloader = $flag;
00731     }

static setRoot ( appRoot = ''  )  [static]

Set application root absolute path

Parameters:
string $appRoot

Definition at line 152 of file Mage.php.

00153     {
00154         if (self::registry('appRoot')) {
00155             return ;
00156         }
00157         if (''===$appRoot) {
00158             // automagically find application root by dirname of Mage.php
00159             $appRoot = dirname(__FILE__);
00160         }
00161 
00162         $appRoot = realpath($appRoot);
00163 
00164         if (is_dir($appRoot) and is_readable($appRoot)) {
00165             Mage::register('appRoot', $appRoot);
00166         } else {
00167             Mage::throwException($appRoot.' is not a directory or not readable by this user');
00168         }
00169     }

static throwException ( message,
messageStorage = null 
) [static]

Definition at line 402 of file Mage.php.

00403     {
00404         if ($messageStorage && ($storage = Mage::getSingleton($messageStorage))) {
00405             $storage->addError($message);
00406         }
00407         throw new Mage_Core_Exception($message);
00408     }

static unregister ( key  )  [static]

Definition at line 123 of file Mage.php.

00124     {
00125         if (isset(self::$_registry[$key])) {
00126             if (is_object(self::$_registry[$key]) && (method_exists(self::$_registry[$key],'__destruct'))) {
00127                 self::$_registry[$key]->__destruct();
00128             }
00129             unset(self::$_registry[$key]);
00130         }
00131     }


Member Data Documentation

$headersSentThrowsException = true [static]

Definition at line 83 of file Mage.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:22:40 2009 for Magento by  doxygen 1.5.8