/** * Returns a reference to a cache adapter object, always creating it * * @param string $type The cache object type to instantiate; default is output. * @param array $options Array of options * * @return Cache A Cache object * * @since 11.1 * @throws RuntimeException */ public static function getInstance($type = 'output', $options = array()) { self::addIncludePath(JPATH_PLATFORM . '/joomla/cache/controller'); $type = strtolower(preg_replace('/[^A-Z0-9_\\.-]/i', '', $type)); $class = '\\Joomla\\Cache\\Controller'; if (!empty($type)) { $class .= '\\' . ucfirst($type); } if (!class_exists($class)) { // Search for the class file in the JCache include paths. if ($path = Path::find(self::addIncludePath(), strtolower($type) . '.php')) { include_once $path; } else { throw new RuntimeException('Unable to load Cache Controller: ' . $type, 500); } } return new $class($options); }
/** * Load a class for one of the form's entities of a particular type. * Currently, it makes sense to use this method for the "field" and "rule" entities * (but you can support more entities in your subclass). * * @param string $entity One of the form entities (field or rule). * @param string $type Type of an entity. * * @return mixed Class name on success or false otherwise. * * @since 1.0 */ protected static function loadClass($entity, $type) { if (strpos($type, '.')) { list($prefix, $type) = explode('.', $type); } else { $prefix = 'Joomla'; } $class = ucfirst($prefix) . '\\Form\\' . ucfirst($entity); if ($entity === 'field') { $class .= '_' . ucfirst($type); } else { $class .= '\\' . ucfirst($type); } if (class_exists($class)) { return $class; } // Get the field search path array. $paths = self::addPath($entity); // If the type is complex, add the base type to the paths. if ($pos = strpos($type, '_')) { // Add the complex type prefix to the paths. for ($i = 0, $n = count($paths); $i < $n; $i++) { // Derive the new path. $path = $paths[$i] . '/' . strtolower(substr($type, 0, $pos)); // If the path does not exist, add it. if (!in_array($path, $paths)) { $paths[] = $path; } } // Break off the end of the complex type. $type = substr($type, $pos + 1); } // Try to find the class file. $type = strtolower($type) . '.php'; foreach ($paths as $path) { if ($file = Path::find($path, $type)) { require_once $file; if (class_exists($class)) { break; } } } // Check for all if the class exists. return class_exists($class) ? $class : false; }
/** * Returns a cache storage handler object, only creating it * if it doesn't already exist. * * @param string $handler The cache storage handler to instantiate * @param array $options Array of handler options * * @return JCacheStorage A JCacheStorage instance * * @since 11.1 * @throws UnexpectedValueException * @throws RuntimeException */ public static function getInstance($handler = null, $options = array()) { static $now = null; self::addIncludePath(JPATH_PLATFORM . '/joomla/cache/storage'); if (!isset($handler)) { $conf = Factory::getConfig(); $handler = $conf->get('cache_handler'); if (empty($handler)) { throw new UnexpectedValueException('Cache Storage Handler not set.'); } } if (is_null($now)) { $now = time(); } $options['now'] = $now; // We can't cache this since options may change... $handler = strtolower(preg_replace('/[^A-Z0-9_\\.-]/i', '', $handler)); $class = '\\Joomla\\Cache\\Storage'; if (!empty($handler)) { $class .= '\\' . ucfirst($handler); } if (!class_exists($class)) { // Search for the class file in the JCacheStorage include paths. if ($path = Path::find(self::addIncludePath(), strtolower($handler) . '.php')) { include_once $path; } else { throw new RuntimeException(sprintf('Unable to load Cache Storage: %s', $handler)); } } return new $class($options); }
/** * Class loader method * * Additional arguments may be supplied and are passed to the sub-class. * Additional include paths are also able to be specified for third-party use * * @param string $key The name of helper method to load, (prefix).(class).function * prefix and class are optional and can be used to load custom * html helpers. * * @return mixed JHtml::call($function, $args) or False on error * * @since 11.1 * @throws InvalidArgumentException */ public static function _($key) { list($key, $prefix, $file, $func) = self::extract($key); if (array_key_exists($key, self::$registry)) { $function = self::$registry[$key]; $args = func_get_args(); // Remove function name from arguments array_shift($args); return self::call($function, $args); } $className = $prefix . ucfirst($file); if (!class_exists($className)) { $path = Path::find(self::$includePaths, strtolower($file) . '.php'); if ($path) { require_once $path; if (!class_exists($className)) { throw new InvalidArgumentException(sprintf('%s not found.', $className), 500); } } else { throw new InvalidArgumentException(sprintf('%s %s not found.', $prefix, $file), 500); } } $toCall = array($className, $func); if (is_callable($toCall)) { self::register($key, $toCall); $args = func_get_args(); // Remove function name from arguments array_shift($args); return self::call($toCall, $args); } else { throw new InvalidArgumentException(sprintf('%s::%s not found.', $className, $func), 500); } }
/** * Method to load the form description from an XML file. * * The reset option works on a group basis. If the XML file references * groups that have already been created they will be replaced with the * fields in the new XML file unless the $reset parameter has been set * to false. * * @param string $file The filesystem path of an XML file. * @param string $reset Flag to toggle whether form fields should be replaced if a field * already exists with the same group/name. * @param string $xpath An optional xpath to search for the fields. * * @return boolean True on success, false otherwise. * * @since 11.1 */ public function loadFile($file, $reset = true, $xpath = false) { // Check to see if the path is an absolute path. if (!is_file($file)) { // Not an absolute path so let's attempt to find one using JPath. $file = Path::find(self::addFormPath(), strtolower($file) . '.xml'); // If unable to find the file return false. if (!$file) { return false; } } // Attempt to load the XML file. $xml = simplexml_load_file($file); return $this->load($xml, $reset, $xpath); }
/** * Method to get the layout path. * * @param string $layout The base name of the layout file (excluding extension). * @param string $ext The extension of the layout file (default: "php"). * * @return mixed The layout file name if found, false otherwise. * * @since 1.0 */ public function getPath($layout, $ext = 'php') { // Get the layout file name. $file = Path::clean($layout . '.' . $ext); // Find the layout file path. $path = Path::find(clone $this->paths, $file); return $path; }
/** * Static method to get an instance of a JTable class if it can be found in * the table include paths. To add include paths for searching for JTable * classes @see JTable::addIncludePath(). * * @param string $type The type (name) of the JTable class to get an instance of. * @param string $prefix An optional prefix for the table class name. * @param array $config An optional array of configuration values for the JTable object. * * @return mixed A JTable object if found or boolean false if one could not be found. * * @link http://docs.joomla.org/JTable/getInstance * @since 11.1 */ public static function getInstance($type, $prefix = 'JTable', $config = array()) { // Sanitize and prepare the table class name. $type = preg_replace('/[^A-Z0-9_\\.-]/i', '', $type); $tableClass = $prefix . ucfirst($type); // Only try to load the class if it doesn't already exist. if (!class_exists($tableClass)) { // Search for the class file in the JTable include paths. $path = Path::find(self::addIncludePath(), strtolower($type) . '.php'); if ($path) { // Import the class file. include_once $path; // If we were unable to load the proper class, raise a warning and return false. if (!class_exists($tableClass)) { Log::add(Text::sprintf('JLIB_DATABASE_ERROR_CLASS_NOT_FOUND_IN_FILE', $tableClass), Log::WARNING, 'jerror'); return false; } } else { // If we were unable to find the class file in the JTable include paths, raise a warning and return false. Log::add(Text::sprintf('JLIB_DATABASE_ERROR_NOT_SUPPORTED_FILE_NOT_FOUND', $type), Log::WARNING, 'jerror'); return false; } } // If a database object was passed in the configuration array use it, otherwise get the global one from JFactory. $db = isset($config['dbo']) ? $config['dbo'] : Factory::getDbo(); // Instantiate a new table class and return it. return new $tableClass($db); }