PHP Class lithium\core\Libraries

Typically, libraries and plugins are registered in app/config/bootstrap/libraries.php. By convention, vendor libraries are typically located in app/libraries or /libraries, and plugins are located in app/libraries/plugins or /libraries/plugins. By default, Libraries will use its own autoloader for all plugins and vendor libraries, but can be configured to use others on a case-by-case basis. Libraries also handles service location. Various 'types' of classes can be defined by name, using _class patterns_, which define conventions for organizing classes, i.e. 'models' is '{:library}\models\{:name}', which will find a model class in any registered app, plugin or vendor library that follows that path (namespace) convention. You can find classes by name (see locate() for more information on class-locating precedence), or find all models in all registered libraries (apps / plugins / vendor libraries, etc). For more information on modifying the default class organization, or defining your own class types, see the paths() method.
See also: lithium\core\Libraries::add()
See also: lithium\core\Libraries::locate()
See also: lithium\core\Libraries::paths()
Show file Open project: unionofrad/lithium Class Usage Examples

Protected Properties

Property Type Description
$_cachedPaths array Holds cached class paths generated and used by lithium\core\Libraries::load().
$_configurations array The list of class libraries registered with the class loader.
$_default string Stores the name of the default library. When adding a library configuration to the application, if the 'default' option flag is set to true, the name of the library will be assigned. To retrieve the default library's configuration, use Libraries::get(true).
$_map array Holds associations between fully-namespaced class names and file's paths mapped with lithium\core\Libraries::map().
$_methodFilters array Stores the closures that represent the method filters. They are indexed by method name.
$_paths array Used by Libraries::locate() to perform service location. This allows new types of objects (i.e. models, helpers, cache adapters and data sources) to be automatically 'discovered' when you register a new vendor library or plugin (using Libraries::add()). Because paths are checked in the order in which they appear, path templates should be specified from most-specific to least-specific. See the locate() method for usage examples.

Public Methods

Method Description
add ( string $name, array $config = [] ) : array Adds a class library from which files can be loaded.
applyFilter ( string $method, Closure $filter = null ) : void Apply a closure to a method in Libraries.
cache ( array $cache = null ) : array Returns or sets the the class path cache used for mapping class names to file paths, or locating classes using Libraries::locate().
find ( mixed $library, array $options = [] ) : array Finds the classes or namespaces belonging to a particular library. _Note_: This method assumes loaded class libraries use a consistent class-to-file naming convention.
get ( mixed $name = null, string $key = null ) : mixed Allows library information to be retrieved in various ways, including:
instance ( string $type, string $name, array $options = [] ) : object Uses service location (i.e. Libraries::locate()) to look up a named class of a particular type, and creates an instance of it, and passes an array of parameters to the constructor.
load ( string $class, boolean $require = false ) : void Loads the class definition specified by $class. Looks through the list of libraries defined in $_configurations, which are added through lithium\core\Libraries::add().
locate ( string $type, string $name = null, array $options = [] ) : mixed Performs service location for an object of a specific type. If $name is a string, finds the first instance of a class with the given name in any registered library (i.e. apps, plugins or vendor libraries registered via Libraries::add()), based on each library's order of precedence. For example, this will find the first model called File in any plugin or class library loaded into an application, including the application itself.
map ( array $classes ) : void Associtates fully-namespaced class names to their corresponding paths on the file system.
path ( string $class, array $options = [] ) : string Get the corresponding physical file path for a class or namespace name.
paths ( mixed $path = null ) : mixed Accessor method for the class path templates which Libraries uses to look up and load classes. Using this method, you can define your own types of classes, or modify the default organization of built-in class types.
realPath ( string $path ) : string Wraps the PHP realpath() function to add support for finding paths to files inside Phar archives.
remove ( mixed $name ) Removes a registered library, and unregister's the library's autoloader, if it has one.
unmap ( mixed $classes ) Unmap fully-namespaced class names mapped using lithium\core\Libraries::map().

Protected Methods

Method Description
_configure ( array $config ) : void Configures the application environment based on a library's settings, including appending to the include path, loading a bootstrap file, and registering a loader with SPL's autoloading system.
_filter ( array $libs, array $config, array $options = [] ) : array Filters a list of library search results by the given set of options.
_locateAll ( array $params, array $options = [] ) : array Locates all possible classes for given set of parameters.
_locateDeferred ( boolean | null $defer, array $paths, array $params, array $options = [] ) : string Performs service location lookups by library, based on the library's 'defer' flag.
_locatePath ( string $type, array $params ) : string | null Helper function for returning known paths given a certain type.
_params ( string $type, string $name = "*" ) : array Get params from type.
_search ( string $config, array $options, string $name = null ) : array Search file system.
_searchPaths ( array $paths, string $library ) : array Returns the list of valid search path templates for the given service location lookup.
_transformPath ( mixed $transform, string $class, array $options = [] ) : string Handles the conversion of a class name to a file name using a custom transformation typically defined in the 'transform' key of a configuration defined through Libraries::add().

Method Details

_configure() protected static method

Configures the application environment based on a library's settings, including appending to the include path, loading a bootstrap file, and registering a loader with SPL's autoloading system.
protected static _configure ( array $config ) : void
$config array The new library's configuration array.
return void

_filter() protected static method

Filters a list of library search results by the given set of options.
protected static _filter ( array $libs, array $config, array $options = [] ) : array
$libs array List of found libraries.
$config array The configuration of the library currently being searched within.
$options array The options used to filter/format `$libs`.
return array Returns a copy of `$libs`, filtered and transformed based on the configuration provided in `$options`.

_locateAll() protected static method

Locates all possible classes for given set of parameters.
protected static _locateAll ( array $params, array $options = [] ) : array
$params array
$options array
return array

_locateDeferred() protected static method

Libraries with 'defer' set to true will be searched last when looking up services.
See also: lithium\core\Libraries::$_paths
See also: lithium\core\Libraries::locate()
protected static _locateDeferred ( boolean | null $defer, array $paths, array $params, array $options = [] ) : string
$defer boolean | null A boolean flag indicating which libraries to search, either the ones with the `'defer'` flag set, or the ones without. Providing `null` will cause the method to ignore the `'defer'` flag set on any library and perform a complete lookup.
$paths array List of paths to be searched for the given service (class). These are defined in `lithium\core\Libraries::$_paths`, and are organized by class type.
$params array The list of insert parameters to be injected into each path format string when searching for classes.
$options array
return string Returns a class path as a string if a given class is found, or null if no class in any path matching any of the parameters is located.

_locatePath() protected static method

Helper function for returning known paths given a certain type.
See also: lithium\core\Libraries::$_paths
protected static _locatePath ( string $type, array $params ) : string | null
$type string Path type (specified in `Libraries::$_paths`).
$params array Path parameters.
return string | null Valid path name or `null` when no of path of given tpe is set.

_params() protected static method

Get params from type.
protected static _params ( string $type, string $name = "*" ) : array
$type string
$name string default: '*'
return array type, namespace, class, name

_searchPaths() protected static method

Returns the list of valid search path templates for the given service location lookup.
See also: lithium\core\Libraries::$_paths
See also: lithium\core\Libraries::_search()
protected static _searchPaths ( array $paths, string $library ) : array
$paths array The list of all possible path templates from `Libraries::$_paths`.
$library string The name of the library being searched.
return array Returns an array of valid path template strings.

_transformPath() protected static method

The transformation can either be a closure which receives two parameters (the class name as a string, and the library configuration as an array), or an array with two values (one being the pattern to match, the other being the replacement).
See also: lithium\core\Libraries::add()
See also: lithium\core\Libraries::path()
protected static _transformPath ( mixed $transform, string $class, array $options = [] ) : string
$transform mixed Either a closure or an array containing a regular expression match and replacement. If the closure returns an empty value, or the regular expression fails to match, will return `null`.
$class string The class name which is attempting to be mapped to a file.
$options array The configuration of the library as passed to `Libraries::add()`, along with any options specified in the call to `Libraries::path()`.
return string Returns transformed path of a class to a file, or `null` if the transformation did not match.

add() public static method

The add() method registers a named library configuration to your application, and is used to allow the framework to auto-load classes on an as-needed basis. ### Adding libraries to your application In Lithium, libraries represent the broadest unit of class organization in an application, and _everything_ is a library; this includes your application, and the Lithium framework itself. Libraries can also be other frameworks, like Solar, Zend Framework or PEAR, or Lithium plugins, which are simply libraries that follow the same organizational standards as Lithium applications. By convention, libraries are placed in the libraries directory inside your application, or the root libraries directory at the top level of the default distribution (i.e. the one that contains the lithium directory), however, you can change this on a case-by-case basis using the 'path' key to specify an absolute path to the library's directory.
public static add ( string $name, array $config = [] ) : array
$name string Library name, i.e. `'app'`, `'lithium'`, `'pear'` or `'aura'`.
$config array Specifies where the library is in the filesystem, and how classes should be loaded from it. Allowed keys are: - `'bootstrap'` _mixed_: A file path (relative to `'path'`) to a bootstrap script that should be run when the library is added, or `true` to use the default bootstrap path, i.e. `config/bootstrap.php`. - `'defer'` _boolean_: If `true`, indicates that, when locating classes, this library should defer to other libraries in order of preference. - `'includePath'` _mixed_: If `true`, appends the absolutely-resolved value of `'path'` to the PHP include path. If a string, the value is appended to PHP's. - `'loader'`: An auto-loader method associated with the library, if any. - `'path'`: The directory containing the library. - `'prefix'` _string_: The class prefix this library uses, i.e. `'lithium\'`, `'Zend_'` or `'Solar_'`. If the library has no global prefix, set to `false`. - `'suffix'` _string_: Gets appended to the end of the file name. For example, most libraries end classes in `'.php'`, but some use `'.class.php'`, or `'.inc.php'`. - `'transform'` _\Closure_: Defines a custom way to transform a class name into its corresponding file path. Accepts either an array of two strings which are interpreted as the pattern and replacement for a regex, or an anonymous function, which receives the class name and library configuration arrays as parameters, and returns the full physical file path as output. - `'resources'` _string_: If this is the default library, this maybe set to the absolute path to the write-enabled application resources directory, which is used for caching, log files, uploads, etc.
return array Returns the resulting set of options created for this library.

applyFilter() public static method

Apply a closure to a method in Libraries.
See also: lithium\util\collection\Filters
public static applyFilter ( string $method, Closure $filter = null ) : void
$method string The name of the method to apply the closure to.
$filter Closure The closure that is used to filter the method.
return void

cache() public static method

Returns or sets the the class path cache used for mapping class names to file paths, or locating classes using Libraries::locate().
public static cache ( array $cache = null ) : array
$cache array An array of keys and values to use when pre-populating the cache. Keys are either class names (which match to file paths as values), or dot-separated lookup paths used by `locate()` (which matches to either a single class or an array of classes). If `false`, the cache is cleared.
return array Returns an array of cached class lookups, formatted per the description for `$cache`.

find() public static method

Finds the classes or namespaces belonging to a particular library. _Note_: This method assumes loaded class libraries use a consistent class-to-file naming convention.
public static find ( mixed $library, array $options = [] ) : array
$library mixed The name of a library added to the application with `Libraries::add()`, or `true` to search all libraries.
$options array The options this method accepts: - `'path'` _string_: A physical filesystem path relative to the directory of the library being searched. If provided, only the classes or namespaces within this path will be returned. - `'recursive'` _boolean_: If `true`, recursively searches all directories (namespaces) in the given library. If `false` (the default), only searches the top level of the given path. - `'filter'` _string_: A regular expression applied to a class after it is transformed into a fully-namespaced class name. The default regular expression filters class names based on the [PSR-0](http://groups.google.com/group/php-standards/web/psr-0-final-proposal) PHP 5.3 naming standard. - `'exclude'` _mixed_: Can be either a regular expression of classes/namespaces to exclude, or a PHP callable to be used with `array_filter()`. - `'namespaces'` _boolean_: Indicates whether namespaces should be included in the search results. If `false` (the default), only classes are returned.
return array Returns an array of fully-namespaced class names found in the given library or libraries.

get() public static method

By name: embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(1-1) With no parameters, to return all configuration for all libraries: embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(22-22) By list of names with a key to extract: embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(34-34) With no name, and a key to extract, to return a key/value array, where the library name is the key, and the $key value is the value: embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(37-37) By containing class name: embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(45-45)
public static get ( mixed $name = null, string $key = null ) : mixed
$name mixed Either the name of a library added in `Libraries::add()`, an array of library names, or a fully-namespaced class name (see usage examples above).
$key string Optional key name. If `$name` is set and is the name of a valid library (or an array of valid libraries), returns the given named configuration key, i.e. `'path'`, `'webroot'` or `'resources'`.
return mixed A configuation array for one or more libraries, or a string value if `$key` is specified and `$name` is a string, or a library name (string) if `$name` is a fully-namespaced class name.

instance() public static method

If the given class can't be found, an exception is thrown.
public static instance ( string $type, string $name, array $options = [] ) : object
$type string The type of class as defined by `Libraries::$_paths`.
$name string The un-namespaced name of the class to instantiate.
$options array An array of constructor parameters to pass to the class.
return object If the class is found, returns an instance of it, otherwise throws an exception.

load() public static method

Loads the class definition specified by $class. Looks through the list of libraries defined in $_configurations, which are added through lithium\core\Libraries::add().
See also: lithium\core\Libraries::add()
See also: lithium\core\Libraries::path()
public static load ( string $class, boolean $require = false ) : void
$class string The fully-namespaced (where applicable) name of the class to load.
$require boolean Specifies whether the class must be loaded or considered an exception. Defaults to `false`.
return void

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`.

map() public static method

Once a class is associtated to a path using lithium\core\Libraries::map() the PSR-0 loader or custom class loader setted using the transform or loader option of lithium\core\Libraries::add() are ignored and the associtated path is used instead.
public static map ( array $classes ) : void
$classes array An array of fully-namespaced class names (as keys) and their correponding file's paths (as values).
return void

path() public static method

Get the corresponding physical file path for a class or namespace name.
public static path ( string $class, array $options = [] ) : string
$class string The class name to locate the physical file for. If `$options['dirs']` is set to `true`, `$class` may also be a namespace name, in which case the corresponding directory will be located.
$options array Options for converting `$class` to a physical path: - `'dirs'`: Defaults to `false`. If `true`, will attempt to case-sensitively look up directories in addition to files (in which case `$class` is assumed to actually be a namespace).
return string Returns the absolute path to the file containing `$class`, or `null` if the file cannot be found.

paths() public static method

For example, in a queuing application, you can define a class type called 'job': Libraries::paths(array('job' => '{:library}\extensions\job\{:name}')); Then, any classes you add to the extensions/job directory in your application will be automatically detected when calling Libraries::locate('job'). Additionally, any matching classes in the extensions/job directory of any plugin or vendor library you add to your application will also be detected. Supposing you wanted to have the option of further organizing jobs by class type (some jobs are related to updating caches, others to sending notifications, etc.), you can specify multiple paths per class type, with varying levels of specificity: Libraries::paths(array('job' => array( '{:library}\extensions\job\{:class}\{:name}', '{:library}\extensions\job\{:name}' ))); This allows you to, for example, have two different classes called Cleanup. One may be located in app\extensions\job\Cleanup, while the other is in app\extensions\job\cache\Cleanup. Calling: Libraries::locate('job'); will find both classes, while Libraries::locate('job.cache'); will only find the second. You can also find individual jobs by name: Libraries::locate('job', 'Cleanup'); See Libraries::locate() for more information on using built-in and user-defined paths to look up classes. In addition to adding custom class types, paths() allows you to redefine the naming and organization of existing types. For example, if you wished to reference your model classes as app\models\PostModel instead of app\models\Post, you can do the following: Libraries::paths(array('models' => '{:library}\models\{:name}Model')); Note, however, that this is a destructive, not an additive operation, and will replace any existing paths defined for that type. If you wish to add a search path for an existing type, you must do the following: $existing = Libraries::paths('controllers'); Libraries::paths(array('controller' => array_merge( array('{:library}\extensions\controllers\{:name}Controller'), (array) $existing )));
See also: lithium\core\Libraries::locate()
See also: lithium\core\Libraries::$_paths
public static paths ( mixed $path = null ) : mixed
$path mixed If `$path` is a string, returns the path(s) associated with that path type, or `null` if no paths are defined for that type.
return mixed

realPath() public static method

Wraps the PHP realpath() function to add support for finding paths to files inside Phar archives.
public static realPath ( string $path ) : string
$path string An unresolved path to a file inside a Phar archive which may or may not exist.
return string If `$path` is a valid path to a file inside a Phar archive, returns a string in the format `'phar:///'`. Otherwise returns `null`.

remove() public static method

Removes a registered library, and unregister's the library's autoloader, if it has one.
public static remove ( mixed $name )
$name mixed A string or array of library names indicating the libraries you wish to remove, i.e. `'app'` or `'lithium'`. This can also be used to unload plugins by name.

unmap() public static method

Unmap fully-namespaced class names mapped using lithium\core\Libraries::map().
See also: lithium\core\Libraries::map()
public static unmap ( mixed $classes )
$classes mixed An array of fully-namespaced class names or a string with a fully-namespaced class name.

Property Details

$_cachedPaths protected static property

Holds cached class paths generated and used by lithium\core\Libraries::load().
See also: lithium\core\Libraries::load()
protected static array $_cachedPaths
return array

$_configurations protected static property

The list of class libraries registered with the class loader.
protected static array $_configurations
return array

$_default protected static property

Stores the name of the default library. When adding a library configuration to the application, if the 'default' option flag is set to true, the name of the library will be assigned. To retrieve the default library's configuration, use Libraries::get(true).
See also: lithium\core\Libraries::add()
See also: lithium\core\Libraries::get()
protected static string $_default
return string

$_map protected static property

Holds associations between fully-namespaced class names and file's paths mapped with lithium\core\Libraries::map().
See also: lithium\core\Libraries::map()
See also: lithium\core\Libraries::unmap()
protected static array $_map
return array

$_methodFilters protected static property

Stores the closures that represent the method filters. They are indexed by method name.
protected static array $_methodFilters
return array

$_paths protected static property

Used by Libraries::locate() to perform service location. This allows new types of objects (i.e. models, helpers, cache adapters and data sources) to be automatically 'discovered' when you register a new vendor library or plugin (using Libraries::add()). Because paths are checked in the order in which they appear, path templates should be specified from most-specific to least-specific. See the locate() method for usage examples.
See also: lithium\core\Libraries::locate()
See also: lithium\core\Libraries::paths()
protected static array $_paths
return array