Support Joomla!

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /joomla/factory.php

Documentation is available at factory.php

  1. <?php
  2. /**
  3.  * @version        $Id: factory.php 13341 2009-10-27 03:03:54Z ian $
  4.  * @package        Joomla.Framework
  5.  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  6.  * @license        GNU/GPL, see LICENSE.php
  7.  *  Joomla! is free software. This version may have been modified pursuant
  8.  *  to the GNU General Public License, and as distributed it includes or
  9.  *  is derivative of works licensed under the GNU General Public License or
  10.  *  other free or open source software licenses.
  11.  *  See COPYRIGHT.php for copyright notices and details.
  12.  */
  13. defined('JPATH_BASE'or die();
  14. /**
  15.  * Joomla Framework Factory class
  16.  *
  17.  * @static
  18.  * @package        Joomla.Framework
  19.  * @since    1.5
  20.  */
  21. class JFactory
  22. {
  23.     /**
  24.      * Get a application object
  25.      *
  26.      * Returns a reference to the global {@link JApplication} object, only creating it
  27.      * if it doesn't already exist.
  28.      *
  29.      * @access public
  30.      * @param    mixed    $id         A client identifier or name.
  31.      * @param    array    $config     An optional associative array of configuration settings.
  32.      * @return object JApplication 
  33.      */
  34.     function &getApplication($id null$config array()$prefix='J')
  35.     {
  36.         static $instance;
  37.  
  38.         if (!is_object($instance))
  39.         {
  40.             jimport'joomla.application.application' );
  41.  
  42.             if (!$id{
  43.                 JError::raiseError(500'Application Instantiation Error');
  44.             }
  45.  
  46.             $instance JApplication::getInstance($id$config$prefix);
  47.         }
  48.  
  49.         return $instance;
  50.     }
  51.  
  52.     /**
  53.      * Get a configuration object
  54.      *
  55.      * Returns a reference to the global {@link JRegistry} object, only creating it
  56.      * if it doesn't already exist.
  57.      *
  58.      * @access public
  59.      * @param string    The path to the configuration file
  60.      * @param string    The type of the configuration file
  61.      * @return object JRegistry 
  62.      */
  63.     function &getConfig($file null$type 'PHP')
  64.     {
  65.         static $instance;
  66.  
  67.         if (!is_object($instance))
  68.         {
  69.             if ($file === null{
  70.                 $file dirname(__FILE__).DS.'config.php';
  71.             }
  72.  
  73.             $instance JFactory::_createConfig($file$type);
  74.         }
  75.  
  76.         return $instance;
  77.     }
  78.  
  79.     /**
  80.      * Get a session object
  81.      *
  82.      * Returns a reference to the global {@link JSession} object, only creating it
  83.      * if it doesn't already exist.
  84.      *
  85.      * @access public
  86.      * @param array An array containing session options
  87.      * @return object JSession 
  88.      */
  89.     function &getSession($options array())
  90.     {
  91.         static $instance;
  92.  
  93.         if (!is_object($instance)) {
  94.             $instance JFactory::_createSession($options);
  95.         }
  96.  
  97.         return $instance;
  98.     }
  99.  
  100.     /**
  101.      * Get a language object
  102.      *
  103.      * Returns a reference to the global {@link JLanguage} object, only creating it
  104.      * if it doesn't already exist.
  105.      *
  106.      * @access public
  107.      * @return object JLanguage 
  108.      */
  109.     function &getLanguage()
  110.     {
  111.         static $instance;
  112.  
  113.         if (!is_object($instance))
  114.         {
  115.             //get the debug configuration setting
  116.             $conf =JFactory::getConfig();
  117.             $debug $conf->getValue('config.debug_lang');
  118.  
  119.             $instance JFactory::_createLanguage();
  120.             $instance->setDebug($debug);
  121.         }
  122.  
  123.         return $instance;
  124.     }
  125.  
  126.     /**
  127.      * Get a document object
  128.      *
  129.      * Returns a reference to the global {@link JDocument} object, only creating it
  130.      * if it doesn't already exist.
  131.      *
  132.      * @access public
  133.      * @return object JDocument 
  134.      */
  135.     function &getDocument()
  136.     {
  137.         static $instance;
  138.  
  139.         if (!is_object$instance )) {
  140.             $instance JFactory::_createDocument();
  141.         }
  142.  
  143.         return $instance;
  144.     }
  145.  
  146.     /**
  147.      * Get an user object
  148.      *
  149.      * Returns a reference to the global {@link JUser} object, only creating it
  150.      * if it doesn't already exist.
  151.      *
  152.      * @param     int     $id     The user to load - Can be an integer or string - If string, it is converted to ID automatically.
  153.      *
  154.      * @access public
  155.      * @return object JUser 
  156.      */
  157.     function &getUser($id null)
  158.     {
  159.         jimport('joomla.user.user');
  160.  
  161.         if(is_null($id))
  162.         {
  163.             $session  =JFactory::getSession();
  164.             $instance =$session->get('user');
  165.             if (!is_a($instance'JUser')) {
  166.                 $instance =JUser::getInstance();
  167.             }
  168.         }
  169.         else
  170.         {
  171.             $instance =JUser::getInstance($id);
  172.         }
  173.  
  174.         return $instance;
  175.     }
  176.  
  177.     /**
  178.      * Get a cache object
  179.      *
  180.      * Returns a reference to the global {@link JCache} object
  181.      *
  182.      * @access public
  183.      * @param string The cache group name
  184.      * @param string The handler to use
  185.      * @param string The storage method
  186.      * @return object JCache 
  187.      */
  188.     function &getCache($group ''$handler 'callback'$storage null)
  189.     {
  190.         $handler ($handler == 'function''callback' $handler;
  191.  
  192.         $conf =JFactory::getConfig();
  193.  
  194.         if(!isset($storage)) {
  195.             $storage $conf->getValue('config.cache_handler''file');
  196.         }
  197.  
  198.         $options array(
  199.             'defaultgroup'     => $group,
  200.             'cachebase'     => $conf->getValue('config.cache_path'),
  201.             'lifetime'         => $conf->getValue('config.cachetime'60,    // minutes to seconds
  202.             'language'         => $conf->getValue('config.language'),
  203.             'storage'        => $storage
  204.         );
  205.  
  206.         jimport('joomla.cache.cache');
  207.  
  208.         $cache =JCache::getInstance$handler$options );
  209.         $cache->setCaching($conf->getValue('config.caching'));
  210.         return $cache;
  211.     }
  212.  
  213.     /**
  214.      * Get an authorization object
  215.      *
  216.      * Returns a reference to the global {@link JAuthorization} object, only creating it
  217.      * if it doesn't already exist.
  218.      *
  219.      * @access public
  220.      * @return object JAuthorization 
  221.      */
  222.     function &getACL)
  223.     {
  224.         static $instance;
  225.  
  226.         if (!is_object($instance)) {
  227.             $instance JFactory::_createACL();
  228.         }
  229.  
  230.         return $instance;
  231.     }
  232.  
  233.     /**
  234.      * Get a template object
  235.      *
  236.      * Returns a reference to the global {@link JTemplate} object, only creating it
  237.      * if it doesn't already exist.
  238.      *
  239.      * @access public
  240.      * @return object JTemplate 
  241.      */
  242.     function &getTemplate)
  243.     {
  244.         static $instance;
  245.  
  246.         if (!is_object($instance)) {
  247.             $instance JFactory::_createTemplate();
  248.         }
  249.  
  250.         return $instance;
  251.     }
  252.  
  253.     /**
  254.      * Get a database object
  255.      *
  256.      * Returns a reference to the global {@link JDatabase} object, only creating it
  257.      * if it doesn't already exist.
  258.      *
  259.      * @return object JDatabase 
  260.      */
  261.     function &getDBO()
  262.     {
  263.         static $instance;
  264.  
  265.         if (!is_object($instance))
  266.         {
  267.             //get the debug configuration setting
  268.             $conf =JFactory::getConfig();
  269.             $debug $conf->getValue('config.debug');
  270.  
  271.             $instance JFactory::_createDBO();
  272.             $instance->debug($debug);
  273.         }
  274.  
  275.         return $instance;
  276.     }
  277.  
  278.     /**
  279.      * Get a mailer object
  280.      *
  281.      * Returns a reference to the global {@link JMail} object, only creating it
  282.      * if it doesn't already exist
  283.      *
  284.      * @access public
  285.      * @return object JMail 
  286.      */
  287.     function &getMailer)
  288.     {
  289.         static $instance;
  290.  
  291.         if is_object($instance) ) {
  292.             $instance JFactory::_createMailer();
  293.         }
  294.  
  295.         // Create a copy of this object - do not return the original because it may be used several times
  296.         // PHP4 copies objects by value whereas PHP5 copies by reference
  297.         $copy    (PHP_VERSION 5$instance clone($instance);
  298.  
  299.         return $copy;
  300.     }
  301.  
  302.     /**
  303.      * Get an XML document
  304.      *
  305.      * @access public
  306.      * @param string The type of xml parser needed 'DOM', 'RSS' or 'Simple'
  307.      * @param array: 
  308.      *          boolean ['lite'] When using 'DOM' if true or not defined then domit_lite is used
  309.      *          string  ['rssUrl'] the rss url to parse when using "RSS"
  310.      *          string    ['cache_time'] with 'RSS' - feed cache time. If not defined defaults to 3600 sec
  311.      * @return object Parsed XML document object
  312.      */
  313.  
  314.      function &getXMLParser$type 'DOM'$options array())
  315.      {
  316.         $doc null;
  317.  
  318.         switch (strtolower$type ))
  319.         {
  320.             case 'rss' :
  321.             case 'atom' :
  322.             {
  323.                 if (!is_null$options['rssUrl'))
  324.                 {
  325.                     jimport ('simplepie.simplepie');
  326.                     if(!is_writable(JPATH_BASE.DS.'cache')) {
  327.                         $options['cache_time'0;
  328.                     }
  329.                     $simplepie new SimplePie(
  330.                         $options['rssUrl'],
  331.                         JPATH_BASE.DS.'cache',
  332.                         isset$options['cache_time'$options['cache_time'0
  333.                     );
  334.                     $simplepie->handle_content_type();
  335.                     if ($simplepie->init()) {
  336.                         $doc $simplepie;
  337.                     else {
  338.                         JError::raiseWarning'SOME_ERROR_CODE'JText::_('ERROR LOADING FEED DATA') );
  339.                     }
  340.                 }
  341.             }    break;
  342.  
  343.             case 'simple' :
  344.             {
  345.                 jimport('joomla.utilities.simplexml');
  346.                 $doc new JSimpleXML();
  347.             }    break;
  348.  
  349.             case 'dom'  :
  350.             default :
  351.             {
  352.                 if (!isset($options['lite']|| $options['lite'])
  353.                 {
  354.                     jimport('domit.xml_domit_lite_include');
  355.                     $doc new DOMIT_Lite_Document();
  356.                 }
  357.                 else
  358.                 {
  359.                     jimport('domit.xml_domit_include');
  360.                     $doc new DOMIT_Document();
  361.                 }
  362.             }
  363.         }
  364.  
  365.         return $doc;
  366.     }
  367.  
  368.     /**
  369.     * Get an editor object
  370.     *
  371.     * @access public
  372.     * @param string $editor The editor to load, depends on the editor plugins that are installed
  373.     * @return object JEditor 
  374.     */
  375.     function &getEditor($editor null)
  376.     {
  377.         jimport'joomla.html.editor' );
  378.  
  379.         //get the editor configuration setting
  380.         if(is_null($editor))
  381.         {
  382.             $conf =JFactory::getConfig();
  383.             $editor $conf->getValue('config.editor');
  384.         }
  385.  
  386.         $instance =JEditor::getInstance($editor);
  387.  
  388.         return $instance;
  389.     }
  390.  
  391.     /**
  392.      * Return a reference to the {@link JURI} object
  393.      *
  394.      * @access public
  395.      * @return object JURI 
  396.      * @since 1.5
  397.      */
  398.     function &getURI($uri 'SERVER')
  399.     {
  400.         jimport('joomla.environment.uri');
  401.  
  402.         $instance =JURI::getInstance($uri);
  403.         return $instance;
  404.     }
  405.  
  406.     /**
  407.      * Return a reference to the {@link JDate} object
  408.      *
  409.      * @access public
  410.      * @param mixed $time The initial time for the JDate object
  411.      * @param int $tzOffset The timezone offset.
  412.      * @return object JDate 
  413.      * @since 1.5
  414.      */
  415.     function &getDate($time 'now'$tzOffset 0)
  416.     {
  417.         jimport('joomla.utilities.date');
  418.         static $instances;
  419.         static $classname;
  420.         static $mainLocale;
  421.  
  422.         if(!isset($instances)) {
  423.             $instances array();
  424.         }
  425.  
  426.         $language =JFactory::getLanguage();
  427.         $locale $language->getTag();
  428.  
  429.         if(!isset($classname|| $locale != $mainLocale{
  430.             //Store the locale for future reference
  431.             $mainLocale $locale;
  432.             $localePath JPATH_ROOT DS 'language' DS $mainLocale DS $mainLocale '.date.php';
  433.             if($mainLocale !== false && file_exists($localePath)) {
  434.                 $classname 'JDate'.str_replace('-''_'$mainLocale);
  435.                 JLoader::register$classname,  $localePath);
  436.                 if(!class_exists($classname)) {
  437.                     //Something went wrong.  The file exists, but the class does not, default to JDate
  438.                     $classname 'JDate';
  439.                 }
  440.             else {
  441.                 //No file, so default to JDate
  442.                 $classname 'JDate';
  443.             }
  444.         }
  445.         $key $time '-' $tzOffset;
  446.  
  447.         if(!isset($instances[$classname][$key])) {
  448.             $tmp new $classname($time$tzOffset);
  449.             //We need to serialize to break the reference
  450.             $instances[$classname][$keyserialize($tmp);
  451.             unset($tmp);
  452.         }
  453.  
  454.         $date unserialize($instances[$classname][$key]);
  455.         return $date;
  456.     }
  457.  
  458.  
  459.  
  460.     /**
  461.      * Create a configuration object
  462.      *
  463.      * @access private
  464.      * @param string    The path to the configuration file
  465.      * @param string    The type of the configuration file
  466.      * @return object JRegistry 
  467.      * @since 1.5
  468.      */
  469.     function &_createConfig($file$type 'PHP')
  470.     {
  471.         jimport('joomla.registry.registry');
  472.  
  473.         require_once $file;
  474.  
  475.         // Create the registry with a default namespace of config
  476.         $registry new JRegistry('config');
  477.  
  478.         // Create the JConfig object
  479.         $config new JFrameworkConfig();
  480.  
  481.         // Load the configuration values into the registry
  482.         $registry->loadObject($config);
  483.  
  484.         return $registry;
  485.     }
  486.  
  487.     /**
  488.      * Create a session object
  489.      *
  490.      * @access private
  491.      * @param array $options An array containing session options
  492.      * @return object JSession 
  493.      * @since 1.5
  494.      */
  495.     function &_createSession$options array())
  496.     {
  497.         jimport('joomla.session.session');
  498.  
  499.         //get the editor configuration setting
  500.         $conf =JFactory::getConfig();
  501.         $handler =  $conf->getValue('config.session_handler''none');
  502.  
  503.         // config time is in minutes
  504.         $options['expire'($conf->getValue('config.lifetime')) $conf->getValue('config.lifetime'60 900;
  505.  
  506.         $session JSession::getInstance($handler$options);
  507.         if ($session->getState(== 'expired'{
  508.             $session->restart();
  509.         }
  510.  
  511.         return $session;
  512.     }
  513.  
  514.     /**
  515.      * Create an ACL object
  516.      *
  517.      * @access private
  518.      * @return object JAuthorization 
  519.      * @since 1.5
  520.      */
  521.     function &_createACL()
  522.     {
  523.         //TODO :: take the authorization class out of the application package
  524.         jimport'joomla.user.authorization' );
  525.  
  526.         $db =&  JFactory::getDBO();
  527.  
  528.         $options array(
  529.             'db'                => &$db,
  530.             'db_table_prefix'    => $db->getPrefix('core_acl_',
  531.             'debug'                => 0
  532.         );
  533.         $acl new JAuthorization$options );
  534.  
  535.         return $acl;
  536.     }
  537.  
  538.     /**
  539.      * Create an database object
  540.      *
  541.      * @access private
  542.      * @return object JDatabase 
  543.      * @since 1.5
  544.      */
  545.     function &_createDBO()
  546.     {
  547.         jimport('joomla.database.database');
  548.         jimport'joomla.database.table' );
  549.  
  550.         $conf =JFactory::getConfig();
  551.  
  552.         $host         $conf->getValue('config.host');
  553.         $user         $conf->getValue('config.user');
  554.         $password     $conf->getValue('config.password');
  555.         $database    $conf->getValue('config.db');
  556.         $prefix     $conf->getValue('config.dbprefix');
  557.         $driver     $conf->getValue('config.dbtype');
  558.         $debug         $conf->getValue('config.debug');
  559.  
  560.         $options    array 'driver' => $driver'host' => $host'user' => $user'password' => $password'database' => $database'prefix' => $prefix );
  561.  
  562.         $db =JDatabase::getInstance$options );
  563.  
  564.         if JError::isError($db) ) {
  565.             jexit('Database Error: ' $db->toString() );
  566.         }
  567.  
  568.         if ($db->getErrorNum(0{
  569.             JError::raiseError(500 'JDatabase::getInstance: Could not connect to database <br />' 'joomla.library:'.$db->getErrorNum().' - '.$db->getErrorMsg() );
  570.         }
  571.  
  572.         $db->debug$debug );
  573.         return $db;
  574.     }
  575.  
  576.     /**
  577.      * Create a mailer object
  578.      *
  579.      * @access private
  580.      * @return object JMail 
  581.      * @since 1.5
  582.      */
  583.     function &_createMailer()
  584.     {
  585.         jimport('joomla.mail.mail');
  586.  
  587.         $conf    =JFactory::getConfig();
  588.  
  589.         $sendmail     $conf->getValue('config.sendmail');
  590.         $smtpauth     $conf->getValue('config.smtpauth');
  591.         $smtpuser     $conf->getValue('config.smtpuser');
  592.         $smtppass      $conf->getValue('config.smtppass');
  593.         $smtphost     $conf->getValue('config.smtphost');
  594.         $smtpsecure    $conf->getValue('config.smtpsecure');
  595.         $smtpport    $conf->getValue('config.smtpport');
  596.         $mailfrom     $conf->getValue('config.mailfrom');
  597.         $fromname     $conf->getValue('config.fromname');
  598.         $mailer     $conf->getValue('config.mailer');
  599.  
  600.         // Create a JMail object
  601.         $mail         =JMail::getInstance();
  602.  
  603.         // Set default sender
  604.         $mail->setSender(array ($mailfrom$fromname));
  605.  
  606.         // Default mailer is to use PHP's mail function
  607.         switch ($mailer)
  608.         {
  609.             case 'smtp' :
  610.                 $mail->useSMTP($smtpauth$smtphost$smtpuser$smtppass$smtpsecure$smtpport);
  611.                 break;
  612.             case 'sendmail' :
  613.                 $mail->useSendmail($sendmail);
  614.                 break;
  615.             default :
  616.                 $mail->IsMail();
  617.                 break;
  618.         }
  619.  
  620.         return $mail;
  621.     }
  622.  
  623.  
  624.     /**
  625.      * Create a template object
  626.      *
  627.      * @access private
  628.      * @param array An array of support template files to load
  629.      * @return object JTemplate 
  630.      * @since 1.5
  631.      */
  632.     function &_createTemplate($files array())
  633.     {
  634.         jimport('joomla.template.template');
  635.  
  636.         $conf =JFactory::getConfig();
  637.  
  638.         $tmpl new JTemplate;
  639.  
  640.         // patTemplate
  641.         if ($conf->getValue('config.caching')) {
  642.              $tmpl->enableTemplateCache'File'JPATH_BASE.DS.'cache'.DS);
  643.         }
  644.  
  645.         $tmpl->setNamespace'jtmpl' );
  646.  
  647.         // load the wrapper and common templates
  648.         $tmpl->readTemplatesFromFile'page.html' );
  649.         $tmpl->applyInputFilter('ShortModifiers');
  650.  
  651.         // load the stock templates
  652.         if (is_array$files ))
  653.         {
  654.             foreach ($files as $file{
  655.                 $tmpl->readTemplatesFromInput$file );
  656.             }
  657.         }
  658.  
  659.         $tmpl->addGlobalVar'option',                 $GLOBALS['option');
  660.         $tmpl->addGlobalVar'self',                 str_replace(array('"''<''>'"'")''$_SERVER["PHP_SELF"]) );
  661.         $tmpl->addGlobalVar'uri_query',             $_SERVER['QUERY_STRING');
  662.         $tmpl->addGlobalVar'REQUEST_URI',            JRequest::getURI() );
  663.         if (isset($GLOBALS['Itemid'])) {
  664.             $tmpl->addGlobalVar'itemid'$GLOBALS['Itemid');
  665.         }
  666.  
  667.         return $tmpl;
  668.     }
  669.  
  670.     /**
  671.      * Create a language object
  672.      *
  673.      * @access private
  674.      * @return object JLanguage 
  675.      * @since 1.5
  676.      */
  677.     function &_createLanguage()
  678.     {
  679.         jimport('joomla.language.language');
  680.  
  681.         $conf    =JFactory::getConfig();
  682.         $locale    $conf->getValue('config.language');
  683.         $lang    =JLanguage::getInstance($locale);
  684.         $lang->setDebug($conf->getValue('config.debug_lang'));
  685.  
  686.         return $lang;
  687.     }
  688.  
  689.     /**
  690.      * Create a document object
  691.      *
  692.      * @access private
  693.      * @return object JDocument 
  694.      * @since 1.5
  695.      */
  696.     function &_createDocument()
  697.     {
  698.         jimport('joomla.document.document');
  699.  
  700.         $lang    =JFactory::getLanguage();
  701.  
  702.         //Keep backwards compatibility with Joomla! 1.0
  703.         $raw    JRequest::getBool('no_html');
  704.         $type    JRequest::getWord('format'$raw 'raw' 'html');
  705.  
  706.         $attributes array (
  707.             'charset'    => 'utf-8',
  708.             'lineend'    => 'unix',
  709.             'tab'        => '  ',
  710.             'language'    => $lang->getTag(),
  711.             'direction'    => $lang->isRTL('rtl' 'ltr'
  712.         );
  713.  
  714.         $doc =JDocument::getInstance($type$attributes);
  715.         return $doc;
  716.     }
  717. }

Documentation generated on Sat, 14 Nov 2009 11:12:42 +0000 by phpDocumentor 1.3.1