Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/factory.php

Documentation is available at factory.php

  1. <?php
  2. /**
  3.  * @version        $Id: factory.php 6725 2007-02-26 11:20:17Z tcp $
  4.  * @package        Joomla.Framework
  5.  * @copyright    Copyright (C) 2005 - 2007 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.  
  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 configuration object
  25.      *
  26.      * Returns a reference to the global {@link JRegistry} object, only creating it
  27.      * if it doesn't already exist.
  28.      *
  29.      * @access public
  30.      * @param string    The path to the configuration file
  31.      * @param string    The type of the configuration file
  32.      * @return object JRegistry 
  33.      */
  34.     function &getConfig($file null$type 'PHP')
  35.     {
  36.         static $instance;
  37.  
  38.         if(is_null($file)) {
  39.             $file dirname(__FILE__.DS'config.php';
  40.         }
  41.  
  42.         if (!is_object($instance)) {
  43.             $instance JFactory::_createConfig($file$type);
  44.         }
  45.  
  46.         return $instance;
  47.     }
  48.  
  49.     /**
  50.      * Get a session object
  51.      *
  52.      * Returns a reference to the global {@link JSession} object, only creating it
  53.      * if it doesn't already exist.
  54.      *
  55.      * @access public
  56.      * @param array An array containing session options
  57.      * @return object JSession 
  58.      */
  59.     function &getSession($options array())
  60.     {
  61.         static $instance;
  62.  
  63.         if (!is_object($instance)) {
  64.             $instance JFactory::_createSession($options);
  65.         }
  66.  
  67.         return $instance;
  68.     }
  69.  
  70.     /**
  71.      * Get a language object
  72.      *
  73.      * Returns a reference to the global {@link JLanguage} object, only creating it
  74.      * if it doesn't already exist.
  75.      *
  76.      * @access public
  77.      * @return object JLanguage 
  78.      */
  79.     function &getLanguage()
  80.     {
  81.         static $instance;
  82.  
  83.         if (!is_object($instance)) {
  84.             $instance JFactory::_createLanguage();
  85.         }
  86.  
  87.         return $instance;
  88.     }
  89.  
  90.     /**
  91.      * Get a document object
  92.      *
  93.      * Returns a reference to the global {@link JDocument} object, only creating it
  94.      * if it doesn't already exist.
  95.      *
  96.      * @access public
  97.      * @return object JLanguage 
  98.      */
  99.     function &getDocument()
  100.     {
  101.         static $instance;
  102.  
  103.         if (!is_object$instance )) {
  104.             $instance JFactory::_createDocument();
  105.         }
  106.  
  107.         return $instance;
  108.     }
  109.  
  110.     /**
  111.      * Get an user object
  112.      *
  113.      * Returns a reference to the global {@link JUser} object, only creating it
  114.      * if it doesn't already exist.
  115.      *
  116.      * @access public
  117.      * @return object JUser 
  118.      */
  119.     function &getUser()
  120.     {
  121.         jimport('joomla.user.user');
  122.         $session  =JFactory::getSession();
  123.         $instance =$session->get('user');
  124.  
  125.         return $instance;
  126.     }
  127.  
  128.     /**
  129.      * Get a cache object
  130.      *
  131.      * Returns a reference to the global {@link JCache} object, only creating it
  132.      * if it doesn't already exist.
  133.      *
  134.      * @access public
  135.      * @param string The cache group name
  136.      * @param string The cache class name
  137.      * @return object JCache 
  138.      */
  139.     function &getCache($group=''$handler 'callback')
  140.     {
  141.         $handler ($handler == 'function''callback' $handler;
  142.         jimport('joomla.cache.cache');
  143.  
  144.         $conf =JFactory::getConfig();
  145.  
  146.         // If we are in the installation application, we don't need to be
  147.         // creating any directories or have caching on
  148.         $options array(
  149.             'cachebase'     => JPATH_CACHE,
  150.             'defaultgroup'     => $group,
  151.             'lifetime'         => $conf->getValue('config.cachetime')
  152.         );
  153.  
  154.         $cache =JCache::getInstance$handler$options );
  155.         $cache->setCaching($conf->getValue('config.caching'));
  156.         return $cache;
  157.     }
  158.  
  159.     /**
  160.      * Get an authorization object
  161.      *
  162.      * Returns a reference to the global {@link JAuthorization} object, only creating it
  163.      * if it doesn't already exist.
  164.      *
  165.      * @access public
  166.      * @return object JAuthorization 
  167.      */
  168.     function &getACL)
  169.     {
  170.         static $instance;
  171.  
  172.         if (!is_object($instance)) {
  173.             $instance JFactory::_createACL();
  174.         }
  175.  
  176.         return $instance;
  177.     }
  178.  
  179.     /**
  180.      * Get a template object
  181.      *
  182.      * Returns a reference to the global {@link JTemplate} object, only creating it
  183.      * if it doesn't already exist.
  184.      *
  185.      * @access public
  186.      * @return object JTemplate 
  187.      */
  188.     function &getTemplate)
  189.     {
  190.         static $instance;
  191.  
  192.         if (!is_object($instance)) {
  193.             $instance JFactory::_createTemplate();
  194.         }
  195.  
  196.         return $instance;
  197.     }
  198.  
  199.     /**
  200.      * Get a database object
  201.      *
  202.      * Returns a reference to the global {@link JDatabase} object, only creating it
  203.      * if it doesn't already exist.
  204.      *
  205.      * @return object JDatabase 
  206.      */
  207.     function &getDBO()
  208.     {
  209.         static $instance;
  210.  
  211.         if (!is_object($instance))
  212.         {
  213.             //get the debug configuration setting
  214.             $conf =JFactory::getConfig();
  215.             $debug $conf->getValue('config.debug');
  216.  
  217.             $instance JFactory::_createDBO();
  218.             $instance->debug($debug);
  219.         }
  220.  
  221.         return $instance;
  222.     }
  223.  
  224.     /**
  225.      * Get a mailer object
  226.      *
  227.      * Returns a reference to the global {@link JMail} object, only creating it
  228.      * if it doesn't already exist
  229.      *
  230.      * @access public
  231.      * @return object JMail 
  232.      */
  233.     function &getMailer)
  234.     {
  235.         static $instance;
  236.  
  237.         if (is_object($instance))
  238.             unset($instance);
  239.  
  240.         $instance JFactory::_createMailer();
  241.  
  242.         return $instance;
  243.     }
  244.  
  245.     /**
  246.      * Get an XML document
  247.      *
  248.      * @access public
  249.      * @param string The type of xml parser needed 'DOM', 'RSS' or 'Simple'
  250.      * @param array: 
  251.      *          boolean ['lite'] When using 'DOM' if true or not defined then domit_lite is used
  252.      *          string  ['rssUrl'] the rss url to parse when using "RSS"
  253.      *          string    ['cache_time'] with 'RSS' - feed cache time. If not defined defaults to 3600 sec
  254.      * @return object Parsed XML document object
  255.      */
  256.  
  257.      function &getXMLParser$type 'DOM'$options array())
  258.      {
  259.         $doc null;
  260.  
  261.         switch ($type)
  262.         {
  263.             case 'RSS' :
  264.             case 'Atom' :
  265.                 if (!is_null$options['rssUrl')) {
  266.                     jimport ('simplepie.simplepie');
  267.                     $simplepie new SimplePie();
  268.                     $simplepie->feed_url($options['rssUrl']);
  269.                     $simplepie->cache_location(JPATH_BASE.DS.'cache');
  270.                     $simplepie->init();
  271.                     $simplepie->handle_content_type();
  272.                     if ($simplepie->data{
  273.                         $doc $simplepie;
  274.                     else {
  275.                     // Raise Error
  276.                     }
  277.                 }
  278.                 break;
  279.  
  280.             case 'Simple' :
  281.                 jimport('joomla.utilities.simplexml');
  282.                 $doc new JSimpleXML();
  283.                 break;
  284.  
  285.             case 'DOM'  :
  286.             default :
  287.                 if (!isset($options['lite']|| $options['lite'])
  288.                 {
  289.                     jimport('domit.xml_domit_lite_include');
  290.                     $doc new DOMIT_Lite_Document();
  291.                 }
  292.                 else
  293.                 {
  294.                     jimport('domit.xml_domit_include');
  295.                     $doc new DOMIT_Document();
  296.                 }
  297.         }
  298.  
  299.         return $doc;
  300.     }
  301.  
  302.     /**
  303.     * Get an editor object
  304.     *
  305.     * @access public
  306.     * @param string $editor The editor to load, depends on the editor plugins that are installed
  307.     * @return object JEditor 
  308.     */
  309.     function &getEditor($editor null)
  310.     {
  311.         jimport'joomla.html.editor' );
  312.  
  313.         //get the editor configuration setting
  314.         if(is_null($editor))
  315.         {
  316.             $conf =JFactory::getConfig();
  317.             $editor $conf->getValue('config.editor');
  318.         }
  319.  
  320.         $instance =JEditor::getInstance($editor);
  321.  
  322.         return $instance;
  323.     }
  324.  
  325.     /**
  326.      * Return a reference to the {@link JURI} object
  327.      *
  328.      * @access public
  329.      * @return object JURI 
  330.      * @since 1.5
  331.      */
  332.     function &getURI($uri 'SERVER')
  333.     {
  334.         jimport('joomla.environment.uri');
  335.  
  336.         $instance =JURI::getInstance($uri);
  337.         return $instance;
  338.     }
  339.  
  340.     /**
  341.      * Create a configuration object
  342.      *
  343.      * @access private
  344.      * @param string    The path to the configuration file
  345.      * @param string    The type of the configuration file
  346.      * @return object JRegistry 
  347.      * @since 1.5
  348.      */
  349.     function &_createConfig($file$type 'PHP')
  350.     {
  351.         jimport('joomla.registry.registry');
  352.  
  353.         require_once$file );
  354.  
  355.         // Create the registry with a default namespace of config
  356.         $registry new JRegistry'config');
  357.  
  358.         // Create the JConfig object
  359.         $config new JFrameworkConfig();
  360.  
  361.         // Load the configuration values into the registry
  362.         $registry->loadObject($config);
  363.  
  364.         return $registry;
  365.     }
  366.  
  367.     /**
  368.      * Create a session object
  369.      *
  370.      * @access private
  371.      * @param array $options An array containing session options
  372.      * @return object JSession 
  373.      * @since 1.5
  374.      */
  375.     function &_createSession$options array())
  376.     {
  377.         jimport('joomla.environment.session');
  378.  
  379.         //get the editor configuration setting
  380.         $conf =JFactory::getConfig();
  381.         $handler =  $conf->getValue('config.session_handler''none');
  382.         $options['expire'$conf->getValue('config.lifetime'15);
  383.  
  384.         $session JSession::getInstance($handler$options);
  385.         if ($session->getState(== 'expired'{
  386.             $session->restart();
  387.         }
  388.  
  389.         return $session;
  390.     }
  391.  
  392.     /**
  393.      * Create an ACL object
  394.      *
  395.      * @access private
  396.      * @return object JAuthorization 
  397.      * @since 1.5
  398.      */
  399.     function &_createACL()
  400.     {
  401.         //TODO :: take the authorization class out of the application package
  402.         jimport'joomla.user.authorization' );
  403.  
  404.         $db =&  JFactory::getDBO();
  405.  
  406.         $options array(
  407.             'db'                => &$db,
  408.             'db_table_prefix'    => $db->getPrefix('core_acl_',
  409.             'debug'                => 0
  410.         );
  411.         $acl new JAuthorization$options );
  412.  
  413.         return $acl;
  414.     }
  415.  
  416.     /**
  417.      * Create an database object
  418.      *
  419.      * @access private
  420.      * @return object JDatabase 
  421.      * @since 1.5
  422.      */
  423.     function &_createDBO()
  424.     {
  425.         jimport('joomla.database.database');
  426.  
  427.         $conf =JFactory::getConfig();
  428.  
  429.         $host         $conf->getValue('config.host');
  430.         $user         $conf->getValue('config.user');
  431.         $password     $conf->getValue('config.password');
  432.         $db           $conf->getValue('config.db');
  433.         $dbprefix     $conf->getValue('config.dbprefix');
  434.         $dbtype     $conf->getValue('config.dbtype');
  435.         $debug         $conf->getValue('config.debug');
  436.  
  437.         $db =JDatabase::getInstance$dbtype$host$user$password$db$dbprefix );
  438.  
  439.         if ($db->getErrorNum(0{
  440.             JError::raiseError('joomla.library:'.$db->getErrorNum()'JDatabase::getInstance: Could not connect to database <br/>' $db->getErrorMsg() );
  441.         }
  442.         $db->debug$debug );
  443.         return $db;
  444.     }
  445.  
  446.     /**
  447.      * Create a mailer object
  448.      *
  449.      * @access private
  450.      * @return object JMail 
  451.      * @since 1.5
  452.      */
  453.     function &_createMailer()
  454.     {
  455.         jimport('joomla.utilities.mail');
  456.  
  457.         $conf    =JFactory::getConfig();
  458.  
  459.         $sendmail     $conf->getValue('config.sendmail');
  460.         $smtpauth     $conf->getValue('config.smtpauth');
  461.         $smtpuser     $conf->getValue('config.smtpuser');
  462.         $smtppass      $conf->getValue('config.smtppass');
  463.         $smtphost     $conf->getValue('config.smtphost');
  464.         $mailfrom     $conf->getValue('config.mailfrom');
  465.         $fromname     $conf->getValue('config.fromname');
  466.         $mailer     $conf->getValue('config.mailer');
  467.  
  468.         $mail new JMail();
  469.  
  470.         // Set default sender
  471.         $mail->setSender(array ($mailfrom$fromname));
  472.  
  473.         // Default mailer is to use PHP's mail function
  474.         switch ($mailer)
  475.         {
  476.             case 'smtp' :
  477.                 $mail->useSMTP($smtpauth$smtphost$smtpuser$smtppass);
  478.                 break;
  479.             case 'sendmail' :
  480.                 $mail->useSendmail();
  481.                 break;
  482.             default :
  483.                 $mail->IsMail();
  484.                 break;
  485.         }
  486.  
  487.         return $mail;
  488.     }
  489.  
  490.  
  491.     /**
  492.      * Create a template object
  493.      *
  494.      * @access private
  495.      * @param array An array of support template files to load
  496.      * @return object JTemplate 
  497.      * @since 1.5
  498.      */
  499.     function &_createTemplate($files array())
  500.     {
  501.         jimport('joomla.template.template');
  502.  
  503.         $conf =JFactory::getConfig();
  504.  
  505.         $tmpl new JTemplate;
  506.  
  507.         // patTemplate
  508.         if ($conf->getValue('config.caching')) {
  509.              $tmpl->enableTemplateCache'File'JPATH_BASE.DS.'cache'.DS);
  510.         }
  511.  
  512.         $tmpl->setNamespace'jtmpl' );
  513.  
  514.         // load the wrapper and common templates
  515.         $tmpl->readTemplatesFromFile'page.html' );
  516.         $tmpl->applyInputFilter('ShortModifiers');
  517.  
  518.         // load the stock templates
  519.         if (is_array$files ))
  520.         {
  521.             foreach ($files as $file{
  522.                 $tmpl->readTemplatesFromInput$file );
  523.             }
  524.         }
  525.  
  526.         $tmpl->addGlobalVar'option',                 $GLOBALS['option');
  527.         $tmpl->addGlobalVar'self',                 $_SERVER['PHP_SELF');
  528.         $tmpl->addGlobalVar'uri_query',             $_SERVER['QUERY_STRING');
  529.         $tmpl->addGlobalVar'itemid',                 $GLOBALS['Itemid');
  530.         $tmpl->addGlobalVar'REQUEST_URI',            JRequest::getURI() );
  531.  
  532.         return $tmpl;
  533.     }
  534.  
  535.     /**
  536.      * Create a language object
  537.      *
  538.      * @access private
  539.      * @return object JLanguage 
  540.      * @since 1.5
  541.      */
  542.     function &_createLanguage()
  543.     {
  544.         jimport('joomla.i18n.language');
  545.  
  546.         $conf    =JFactory::getConfig();
  547.         $locale    $conf->getValue('config.language');
  548.         $lang    =JLanguage::getInstance($locale);
  549.         $lang->setDebug($conf->getValue('config.debug_lang'));
  550.  
  551.         return $lang;
  552.     }
  553.  
  554.     /**
  555.      * Create a document object
  556.      *
  557.      * @access private
  558.      * @return object JDocument 
  559.      * @since 1.5
  560.      */
  561.     function &_createDocument()
  562.     {
  563.         jimport('joomla.document.document');
  564.         jimport('joomla.environment.request');
  565.  
  566.         $lang    =JFactory::getLanguage();
  567.  
  568.         //Keep backwards compatibility with Joomla! 1.0
  569.         $raw    JRequest::getVar'no_html'0'''int' );
  570.         $type    JRequest::getVar'format'$raw 'raw' 'html',  '''string'  );
  571.  
  572.         $attributes array (
  573.             'charset'    => 'utf-8',
  574.             'lineend'    => 'unix',
  575.             'tab'        => '  ',
  576.             'language'    => $lang->getTag(),
  577.             'direction'    => $lang->isRTL('rtl' 'ltr'
  578.         );
  579.  
  580.         $doc =JDocument::getInstance($type$attributes);
  581.         return $doc;
  582.     }
  583. }

Documentation generated on Mon, 05 Mar 2007 20:58:07 +0000 by phpDocumentor 1.3.1