Source for file platform.php

Documentation is available at platform.php

  1. <?php
  2. /**
  3.  * @package     FrameworkOnFramework
  4.  * @subpackage  platform
  5.  * @copyright   Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  6.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8. // Protect from unauthorized access
  9. defined('_JEXEC'or die;
  10.  
  11. /**
  12.  * Part of the FOF Platform Abstraction Layer. It implements everything that
  13.  * depends on the platform FOF is running under, e.g. the Joomla! CMS front-end,
  14.  * the Joomla! CMS back-end, a CLI Joomla! Platform app, a bespoke Joomla!
  15.  * Platform / Framework web application and so on.
  16.  *
  17.  * This is the abstract class implementing some basic housekeeping functionality
  18.  * and provides the static interface to get the appropriate Platform object for
  19.  * use in the rest of the framework.
  20.  *
  21.  * @package  FrameworkOnFramework
  22.  * @since    2.1
  23.  */
  24. abstract class FOFPlatform implements FOFPlatformInterface
  25. {
  26.     /**
  27.      * The ordering for this platform class. The lower this number is, the more
  28.      * important this class becomes. Most important enabled class ends up being
  29.      * used.
  30.      *
  31.      * @var  integer 
  32.      */
  33.     public $ordering = 100;
  34.  
  35.     /**
  36.      * Caches the enabled status of this platform class.
  37.      *
  38.      * @var  boolean 
  39.      */
  40.     protected $isEnabled = null;
  41.  
  42.     /**
  43.      * The list of paths where platform class files will be looked for
  44.      *
  45.      * @var  array 
  46.      */
  47.     protected static $paths array();
  48.  
  49.     /**
  50.      * The platform class instance which will be returned by getInstance
  51.      *
  52.      * @var  FOFPlatformInterface 
  53.      */
  54.     protected static $instance null;
  55.  
  56.     /**
  57.      * Set the error Handling, if possible
  58.      *
  59.      * @param   integer  $level      PHP error level (E_ALL)
  60.      * @param   string   $log_level  What to do with the error (ignore, callback)
  61.      * @param   array    $options    Options for the error handler
  62.      *
  63.      * @return  void 
  64.      */
  65.     public function setErrorHandling($level$log_level$options array())
  66.     {
  67.         if ($this->checkVersion(JVERSION'3.0''lt') )
  68.         {
  69.             return JError::setErrorHandling($level$log_level$options);
  70.         }
  71.     }
  72.  
  73.     /**
  74.      * Register a path where platform files will be looked for. These take
  75.      * precedence over the built-in platform files.
  76.      *
  77.      * @param   string  $path  The path to add
  78.      *
  79.      * @return  void 
  80.      */
  81.     public static function registerPlatformPath($path)
  82.     {
  83.         if (!in_array($pathself::$paths))
  84.         {
  85.             self::$paths[$path;
  86.             self::$instance null;
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * Unregister a path where platform files will be looked for.
  92.      *
  93.      * @param   string  $path  The path to remove
  94.      *
  95.      * @return  void 
  96.      */
  97.     public static function unregisterPlatformPath($path)
  98.     {
  99.         $pos array_search($pathself::$paths);
  100.  
  101.         if ($pos !== false)
  102.         {
  103.             unset(self::$paths[$pos]);
  104.             self::$instance null;
  105.         }
  106.     }
  107.  
  108.     /**
  109.      * Force a specific platform object to be used. If null, nukes the cache
  110.      *
  111.      * @param   FOFPlatformInterface|null $instance  The Platform object to be used
  112.      *
  113.      * @return  void 
  114.      */
  115.     public static function forceInstance($instance)
  116.     {
  117.         if ($instance instanceof FOFPlatformInterface || is_null($instance))
  118.         {
  119.             self::$instance $instance;
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * Find and return the most relevant platform object
  125.      *
  126.      * @return  FOFPlatformInterface 
  127.      */
  128.     public static function getInstance()
  129.     {
  130.         if (!is_object(self::$instance))
  131.         {
  132.             // Get the paths to look into
  133.             $paths array(__DIR__);
  134.  
  135.             if (is_array(self::$paths))
  136.             {
  137.                 $paths array_merge(array(__DIR__)self::$paths);
  138.             }
  139.  
  140.             $paths array_unique($paths);
  141.  
  142.             // Loop all paths
  143.             JLoader::import('joomla.filesystem.folder');
  144.  
  145.             foreach ($paths as $path)
  146.             {
  147.                 // Get the .php files containing platform classes
  148.                 $files JFolder::files($path'[a-z0-9]\.php$'falsetruearray('interface.php''platform.php'));
  149.  
  150.                 if (!empty($files))
  151.                 {
  152.                     foreach ($files as $file)
  153.                     {
  154.                         // Get the class name for this platform class
  155.                         $base_name basename($file'.php');
  156.                         $class_name 'FOFPlatform' ucfirst($base_name);
  157.  
  158.                         // Load the file if the class doesn't exist
  159.  
  160.                         if (!class_exists($class_name))
  161.                         {
  162.                             @include_once $file;
  163.                         }
  164.  
  165.                         // If the class still doesn't exist this file didn't
  166.                         // actually contain a platform class; skip it
  167.  
  168.                         if (!class_exists($class_name))
  169.                         {
  170.                             continue;
  171.                         }
  172.  
  173.                         // If it doesn't implement FOFPlatformInterface, skip it
  174.                         if (!class_implements($class_name'FOFPlatformInterface'))
  175.                         {
  176.                             continue;
  177.                         }
  178.  
  179.                         // Get an object of this platform
  180.                         $o new $class_name;
  181.  
  182.                         // If it's not enabled, skip it
  183.                         if (!$o->isEnabled())
  184.                         {
  185.                             continue;
  186.                         }
  187.  
  188.                         if (is_object(self::$instance))
  189.                         {
  190.                             // Replace self::$instance if this object has a
  191.                             // lower order number
  192.                             $current_order self::$instance->getOrdering();
  193.                             $new_order $o->getOrdering();
  194.  
  195.                             if ($new_order $current_order)
  196.                             {
  197.                                 self::$instance null;
  198.                                 self::$instance $o;
  199.                             }
  200.                         }
  201.                         else
  202.                         {
  203.                             // There is no self::$instance already, so use the
  204.                             // object we just created.
  205.                             self::$instance $o;
  206.                         }
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.  
  212.         return self::$instance;
  213.     }
  214.  
  215.     /**
  216.      * Returns the ordering of the platform class.
  217.      *
  218.      * @see FOFPlatformInterface::getOrdering()
  219.      *
  220.      * @return  integer 
  221.      */
  222.     public function getOrdering()
  223.     {
  224.         return $this->ordering;
  225.     }
  226.  
  227.     /**
  228.      * Is this platform enabled?
  229.      *
  230.      * @see FOFPlatformInterface::isEnabled()
  231.      *
  232.      * @return  boolean 
  233.      */
  234.     public function isEnabled()
  235.     {
  236.         if (is_null($this->isEnabled))
  237.         {
  238.             $this->isEnabled = false;
  239.         }
  240.  
  241.         return $this->isEnabled;
  242.     }
  243.  
  244.     /**
  245.      * Returns the base (root) directories for a given component.
  246.      *
  247.      * @param   string  $component  The name of the component. For Joomla! this
  248.      *                               is something like "com_example"
  249.      *
  250.      * @see FOFPlatformInterface::getComponentBaseDirs()
  251.      *
  252.      * @return  array  A hash array with keys main, alt, site and admin.
  253.      */
  254.     public function getComponentBaseDirs($component)
  255.     {
  256.         return array(
  257.             'main'    => '',
  258.             'alt'    => '',
  259.             'site'    => '',
  260.             'admin'    => '',
  261.         );
  262.     }
  263.  
  264.     /**
  265.      * Return a list of the view template directories for this component.
  266.      *
  267.      * @param   string   $component  The name of the component. For Joomla! this
  268.      *                                is something like "com_example"
  269.      * @param   string   $view       The name of the view you're looking a
  270.      *                                template for
  271.      * @param   string   $layout     The layout name to load, e.g. 'default'
  272.      * @param   string   $tpl        The sub-template name to load (null by default)
  273.      * @param   boolean  $strict     If true, only the specified layout will be
  274.      *                                searched for. Otherwise we'll fall back to
  275.      *                                the 'default' layout if the specified layout
  276.      *                                is not found.
  277.      *
  278.      * @see FOFPlatformInterface::getViewTemplateDirs()
  279.      *
  280.      * @return  array 
  281.      */
  282.     public function getViewTemplatePaths($component$view$layout 'default'$tpl null$strict false)
  283.     {
  284.         return array();
  285.     }
  286.  
  287.     /**
  288.      * Get application-specific suffixes to use with template paths. This allows
  289.      * you to look for view template overrides based on the application version.
  290.      *
  291.      * @return  array  A plain array of suffixes to try in template names
  292.      */
  293.     public function getTemplateSuffixes()
  294.     {
  295.         return array();
  296.     }
  297.  
  298.     /**
  299.      * Return the absolute path to the application's template overrides
  300.      * directory for a specific component. We will use it to look for template
  301.      * files instead of the regular component directorues. If the application
  302.      * does not have such a thing as template overrides return an empty string.
  303.      *
  304.      * @param   string   $component  The name of the component for which to fetch the overrides
  305.      * @param   boolean  $absolute   Should I return an absolute or relative path?
  306.      *
  307.      * @return  string  The path to the template overrides directory
  308.      */
  309.     public function getTemplateOverridePath($component$absolute true)
  310.     {
  311.         return '';
  312.     }
  313.  
  314.     /**
  315.      * Load the translation files for a given component.
  316.      *
  317.      * @param   string  $component  The name of the component. For Joomla! this
  318.      *                               is something like "com_example"
  319.      *
  320.      * @see FOFPlatformInterface::loadTranslations()
  321.      *
  322.      * @return  void 
  323.      */
  324.     public function loadTranslations($component)
  325.     {
  326.         return null;
  327.     }
  328.  
  329.     /**
  330.      * Authorise access to the component in the back-end.
  331.      *
  332.      * @param   string  $component  The name of the component.
  333.      *
  334.      * @see FOFPlatformInterface::authorizeAdmin()
  335.      *
  336.      * @return  boolean  True to allow loading the component, false to halt loading
  337.      */
  338.     public function authorizeAdmin($component)
  339.     {
  340.         return true;
  341.     }
  342.  
  343.     /**
  344.      * Returns the JUser object for the current user
  345.      *
  346.      * @param   integer  $id  The ID of the user to fetch
  347.      *
  348.      * @see FOFPlatformInterface::getUser()
  349.      *
  350.      * @return  JDocument 
  351.      */
  352.     public function getUser($id null)
  353.     {
  354.         return null;
  355.     }
  356.  
  357.     /**
  358.      * Returns the JDocument object which handles this component's response.
  359.      *
  360.      * @see FOFPlatformInterface::getDocument()
  361.      *
  362.      * @return  JDocument 
  363.      */
  364.     public function getDocument()
  365.     {
  366.         return null;
  367.     }
  368.  
  369.     /**
  370.      * This method will try retrieving a variable from the request (input) data.
  371.      *
  372.      * @param   string    $key           The user state key for the variable
  373.      * @param   string    $request       The request variable name for the variable
  374.      * @param   FOFInput  $input         The FOFInput object with the request (input) data
  375.      * @param   mixed     $default       The default value. Default: null
  376.      * @param   string    $type          The filter type for the variable data. Default: none (no filtering)
  377.      * @param   boolean   $setUserState  Should I set the user state with the fetched value?
  378.      *
  379.      * @see FOFPlatformInterface::getUserStateFromRequest()
  380.      *
  381.      * @return  mixed  The value of the variable
  382.      */
  383.     public function getUserStateFromRequest($key$request$input$default null$type 'none'$setUserState true)
  384.     {
  385.         return $input->get($request$default$type);
  386.     }
  387.  
  388.     /**
  389.      * Load plugins of a specific type. Obviously this seems to only be required
  390.      * in the Joomla! CMS.
  391.      *
  392.      * @param   string  $type  The type of the plugins to be loaded
  393.      *
  394.      * @see FOFPlatformInterface::importPlugin()
  395.      *
  396.      * @return void 
  397.      */
  398.     public function importPlugin($type)
  399.     {
  400.     }
  401.  
  402.     /**
  403.      * Execute plugins (system-level triggers) and fetch back an array with
  404.      * their return values.
  405.      *
  406.      * @param   string  $event  The event (trigger) name, e.g. onBeforeScratchMyEar
  407.      * @param   array   $data   A hash array of data sent to the plugins as part of the trigger
  408.      *
  409.      * @see FOFPlatformInterface::runPlugins()
  410.      *
  411.      * @return  array  A simple array containing the resutls of the plugins triggered
  412.      */
  413.     public function runPlugins($event$data)
  414.     {
  415.         return array();
  416.     }
  417.  
  418.     /**
  419.      * Perform an ACL check.
  420.      *
  421.      * @param   string  $action     The ACL privilege to check, e.g. core.edit
  422.      * @param   string  $assetname  The asset name to check, typically the component's name
  423.      *
  424.      * @see FOFPlatformInterface::authorise()
  425.      *
  426.      * @return  boolean  True if the user is allowed this action
  427.      */
  428.     public function authorise($action$assetname)
  429.     {
  430.         return true;
  431.     }
  432.  
  433.     /**
  434.      * Is this the administrative section of the component?
  435.      *
  436.      * @see FOFPlatformInterface::isBackend()
  437.      *
  438.      * @return  boolean 
  439.      */
  440.     public function isBackend()
  441.     {
  442.         return true;
  443.     }
  444.  
  445.     /**
  446.      * Is this the public section of the component?
  447.      *
  448.      * @see FOFPlatformInterface::isFrontend()
  449.      *
  450.      * @return  boolean 
  451.      */
  452.     public function isFrontend()
  453.     {
  454.         return true;
  455.     }
  456.  
  457.     /**
  458.      * Is this a component running in a CLI application?
  459.      *
  460.      * @see FOFPlatformInterface::isCli()
  461.      *
  462.      * @return  boolean 
  463.      */
  464.     public function isCli()
  465.     {
  466.         return true;
  467.     }
  468.  
  469.     /**
  470.      * Is AJAX re-ordering supported? This is 100% Joomla!-CMS specific. All
  471.      * other platforms should return false and never ask why.
  472.      *
  473.      * @see FOFPlatformInterface::supportsAjaxOrdering()
  474.      *
  475.      * @return  boolean 
  476.      */
  477.     public function supportsAjaxOrdering()
  478.     {
  479.         return true;
  480.     }
  481.  
  482.     /**
  483.      * Performs a check between two versions. Use this function instead of PHP version_compare
  484.      * so we can mock it while testing
  485.      *
  486.      * @param   string  $version1  First version number
  487.      * @param   string  $version2  Second version number
  488.      * @param   string  $operator  Operator (see version_compare for valid operators)
  489.      *
  490.      * @return  boolean 
  491.      */
  492.     public function checkVersion($version1$version2$operator)
  493.     {
  494.         return version_compare($version1$version2$operator);
  495.     }
  496.  
  497.     /**
  498.      * Saves something to the cache. This is supposed to be used for system-wide
  499.      * FOF data, not application data.
  500.      *
  501.      * @param   string  $key      The key of the data to save
  502.      * @param   string  $content  The actual data to save
  503.      *
  504.      * @return  boolean  True on success
  505.      */
  506.     public function setCache($key$content)
  507.     {
  508.         return false;
  509.     }
  510.  
  511.     /**
  512.      * Retrieves data from the cache. This is supposed to be used for system-side
  513.      * FOF data, not application data.
  514.      *
  515.      * @param   string  $key      The key of the data to retrieve
  516.      * @param   string  $default  The default value to return if the key is not found or the cache is not populated
  517.      *
  518.      * @return  string  The cached value
  519.      */
  520.     public function getCache($key$default null)
  521.     {
  522.         return false;
  523.     }
  524.  
  525.     /**
  526.      * Is the global FOF cache enabled?
  527.      *
  528.      * @return  boolean 
  529.      */
  530.     public function isGlobalFOFCacheEnabled()
  531.     {
  532.         return true;
  533.     }
  534.  
  535.     /**
  536.      * Clears the cache of system-wide FOF data. You are supposed to call this in
  537.      * your components' installation script post-installation and post-upgrade
  538.      * methods or whenever you are modifying the structure of database tables
  539.      * accessed by FOF. Please note that FOF's cache never expires and is not
  540.      * purged by Joomla!. You MUST use this method to manually purge the cache.
  541.      *
  542.      * @return  boolean  True on success
  543.      */
  544.     public function clearCache()
  545.     {
  546.         return false;
  547.     }
  548.  
  549.     /**
  550.      * logs in a user
  551.      *
  552.      * @param   array  $authInfo  authentification information
  553.      *
  554.      * @return  boolean  True on success
  555.      */
  556.     public function loginUser($authInfo)
  557.     {
  558.         return true;
  559.     }
  560.  
  561.     /**
  562.      * logs out a user
  563.      *
  564.      * @return  boolean  True on success
  565.      */
  566.     public function logoutUser()
  567.     {
  568.         return true;
  569.     }
  570. }

Documentation generated on Tue, 19 Nov 2013 15:10:42 +0100 by phpDocumentor 1.4.3