lithium\core\Libraries::paths()
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.
For example, in a queuing application, you can define a class type called 'job'
:
Libraries::paths(['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(['job' => [
'{: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(['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(['controller' => array_merge(
['{:library}\extensions\controllers\{:name}Controller'], (array) $existing
)]);
Parameters
-
mixed
$path
If
$path
is a string, returns the path(s) associated with that path type, ornull
if no paths are defined for that type.
Returns
mixedSource
public static function paths($path = null) {
if (empty($path)) {
return static::$_paths;
}
if (is_string($path)) {
return isset(static::$_paths[$path]) ? static::$_paths[$path] : null;
}
static::$_paths = array_filter(array_merge(static::$_paths, (array) $path));
}