lithium\core\Libraries::locate PHP Method

locate() public static method

Libraries::locate('models', 'File'); Order of precedence is usually based on the order in which the library was registered (via Libraries::add()), unless the library was registered with the 'defer' option set to true. All libraries with the 'defer' option set will be searched in registration-order **after** searching all libraries **without** 'defer' set. This means that in the above example, if an app and a plugin both have a model named File, then the model from the app will be returned first, assuming the app was registered first (and assuming the default settings). If $name is not specified, locate() returns an array with all classes of the specified type which can be found. By default, locate() searches all registered libraries. Libraries::locate('models'); For example, the above will return an array of all model classes in all registered plugins and libraries (including the app itself). To learn more about adding and modifying the class paths used with locate(), see the documentation for the paths() method.
See also: lithium\core\Libraries::paths()
See also: lithium\core\Libraries::add()
See also: lithium\core\Libraries::_locateDeferred()
public static locate ( string $type, string $name = null, array $options = [] ) : mixed
$type string The type of class to search for. Typically follows the name of the directory in which the class is stored, i.e. `'models'`, `'controllers'` or `'adapter'`. Some classes types, such as adapters, will require a greater degree of specificity when looking up the desired class. In this case, the dot syntax is used, as in this example when looking up cache adapters: `'adapter.storage.cache'`, or this example, when looking up authentication adapters: `'adapter.security.auth'`.
$name string The base name (without namespace) of the class you wish to locate. If unspecified, `locate()` will attempt to find all classes of the type specified in `$type`. If you only wish to search for classes within a single plugin or library, you may use the dot syntax to prefix the class name with the library name, i.e. `'app.Post'`, which will only look for a `Post` model within the app itself.
$options array The options to use when searching and returning class names. - `'type'` _string_: Defaults to `'class'`. If set to `'file'`, returns file names instead of class names. - `'library'` _string_: When specified, only the given library/plugin name will be searched.
return mixed If `$name` is specified, returns the name of the first class found that matches `$name` and `$type`, or returns `null` if no matching classes were found in any registered library. If `$name` is not specified, returns an array of all classes found which match `$type`.
    public static function locate($type, $name = null, array $options = array())
    {
        if (is_object($name) || strpos($name, '\\') !== false) {
            return $name;
        }
        $ident = $name ? $type . '.' . $name : $type . '.*';
        $ident .= $options ? '.' . md5(serialize($options)) : null;
        if (isset(static::$_cachedPaths[$ident])) {
            return static::$_cachedPaths[$ident];
        }
        $params = static::_params($type, $name);
        $defaults = array('type' => 'class', 'library' => $params['library'] !== '*' ? $params['library'] : null);
        $options += $defaults;
        unset($params['library']);
        $paths = static::paths($params['type']);
        if (!isset($paths)) {
            return null;
        }
        if ($params['name'] === '*') {
            $result = static::_locateAll($params, $options);
            return static::$_cachedPaths[$ident] = $result;
        }
        if ($options['library']) {
            $result = static::_locateDeferred(null, $paths, $params, $options);
            return static::$_cachedPaths[$ident] = $result;
        }
        foreach (array(false, true) as $defer) {
            if ($result = static::_locateDeferred($defer, $paths, $params, $options)) {
                return static::$_cachedPaths[$ident] = $result;
            }
        }
    }

Usage Example

Example #1
0
 /**
  * Read the configuration or access the connections you have set up.
  *
  * Usage:
  * {{{
  * // Gets the names of all available configurations
  * $configurations = Connections::get();
  *
  * // Gets the configuration array for the connection named 'db'
  * $config = Connections::get('db', array('config' => true));
  *
  * // Gets the instance of the connection object, configured with the settings defined for
  * // this object in Connections::add()
  * $dbConnection = Connections::get('db');
  *
  * // Gets the connection object, but only if it has already been built.
  * // Otherwise returns null.
  * $dbConnection = Connections::get('db', array('autoCreate' => false));
  * }}}
  *
  * @param string $name The name of the connection to get, as defined in the first parameter of
  *        `add()`, when the connection was initially created.
  * @param array $options Options to use when returning the connection:
  *        - `'autoCreate'`: If `false`, the connection object is only returned if it has
  *          already been instantiated by a previous call.
  *        - `'config'`: If `true`, returns an array representing the connection's internal
  *          configuration, instead of the connection itself.
  * @return mixed A configured instance of the connection, or an array of the configuration used.
  */
 public static function get($name = null, array $options = array())
 {
     static $mockAdapter;
     $defaults = array('config' => false, 'autoCreate' => true);
     $options += $defaults;
     if ($name === false) {
         if (!$mockAdapter) {
             $class = Libraries::locate('data.source', 'Mock');
             $mockAdapter = new $class();
         }
         return $mockAdapter;
     }
     if (!$name) {
         return array_keys(static::$_configurations);
     }
     if (!isset(static::$_configurations[$name])) {
         return null;
     }
     if ($options['config']) {
         return static::_config($name);
     }
     $settings = static::$_configurations[$name];
     if (!isset($settings[0]['object'])) {
         if (!$options['autoCreate']) {
             return null;
         }
     }
     return static::adapter($name);
 }
All Usage Examples Of lithium\core\Libraries::locate