Libraries.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\core;
  9. use RuntimeException;
  10. use lithium\util\String;
  11. use lithium\util\collection\Filters;
  12. use lithium\core\ConfigException;
  13. use lithium\core\ClassNotFoundException;
  14. /**
  15. * Manages all aspects of class and file location, naming and mapping. Implements auto-loading for
  16. * the Lithium core, as well as all applications, plugins and vendor libraries registered.
  17. * Typically, libraries and plugins are registered in `config/bootstrap/libraries.php`.
  18. *
  19. * By convention, plugins and vendor libraries are typically located in `app-path/libraries` or
  20. * `/libraries` (the former may override the latter). By default, `Libraries` will use its own
  21. * autoloader for all plugins and vendor libraries, but can be configured to use others on a
  22. * per-library basis.
  23. *
  24. * `Libraries` also handles service location. Various _types_ of classes can be defined by name,
  25. * using _class patterns_, which define conventions for organizing classes, i.e. `'models'`, which
  26. * maps to `'{:library}\models\{:name}'`, which will find a model class in any registered app,
  27. * plugin or vendor library that follows that path (namespace) convention. You can find classes by
  28. * name (see the `locate()` method for more information on class-locating precedence), or find all
  29. * models in all registered libraries (apps / plugins / vendor libraries, etc). For more information
  30. * on modifying the default class organization, or defining your own class types, see the `paths()`
  31. * method.
  32. *
  33. * #### Auto-loading classes
  34. *
  35. * Lithium defines several rules, conventions and recommendations for naming and organizing classes.
  36. * The autoloader itself conforms to the [PHP Interoperability Group's draft
  37. * specification](http://groups.google.com/group/php-standards/web/psr-0-final-proposal). While the
  38. * autoloader will load any classes conforming to that specification, Lithium itself follows
  39. * additional constraints, which are also recommended for Lithium applications, libraries and
  40. * extensions, and are as follows:
  41. *
  42. * - Each library must exist in a top-level vendor namespace
  43. * - Each top-level vendor namespace must define a set of sub-packages, and should not directly
  44. * contain classes
  45. * - Namespace names must be lowercased and under_scored
  46. * - Class names must be CamelCased
  47. *
  48. * Any libraries registered by calling `Libraries::add()` which follow the autoloader's will have
  49. * their classes automatically loaded when referenced.
  50. *
  51. * @see lithium\core\Libraries::add()
  52. * @see lithium\core\Libraries::locate()
  53. * @see lithium\core\Libraries::paths()
  54. */
  55. class Libraries {
  56. /**
  57. * Stores the closures that represent the method filters. They are indexed by method name.
  58. *
  59. * @var array
  60. */
  61. protected static $_methodFilters = array();
  62. /**
  63. * The list of class libraries registered with the class loader.
  64. *
  65. * @var array
  66. */
  67. protected static $_configurations = array();
  68. /**
  69. * Contains a cascading list of search path templates, indexed by base object type.
  70. *
  71. * Used by `Libraries::locate()` to perform service location. This allows new types of
  72. * objects (i.e. models, helpers, cache adapters and data sources) to be automatically
  73. * 'discovered' when you register a new vendor library or plugin (using `Libraries::add()`).
  74. *
  75. * Because paths are checked in the order in which they appear, path templates should be
  76. * specified from most-specific to least-specific. See the `locate()` method for usage examples.
  77. *
  78. * @see lithium\core\Libraries::locate()
  79. * @see lithium\core\Libraries::paths()
  80. * @var array
  81. */
  82. protected static $_paths = array(
  83. 'adapter' => array(
  84. '{:library}\extensions\adapter\{:namespace}\{:class}\{:name}',
  85. '{:library}\{:namespace}\{:class}\adapter\{:name}' => array('libraries' => 'lithium')
  86. ),
  87. 'command' => array(
  88. '{:library}\extensions\command\{:namespace}\{:class}\{:name}',
  89. '{:library}\console\command\{:namespace}\{:class}\{:name}' => array(
  90. 'libraries' => 'lithium'
  91. )
  92. ),
  93. 'controllers' => array(
  94. '{:library}\controllers\{:namespace}\{:class}\{:name}Controller'
  95. ),
  96. 'data' => array(
  97. '{:library}\extensions\data\{:namespace}\{:class}\{:name}',
  98. '{:library}\data\{:namespace}\{:class}\adapter\{:name}' => array(
  99. 'libraries' => 'lithium'
  100. ),
  101. '{:library}\data\{:namespace}\{:class}\{:name}' => array('libraries' => 'lithium'),
  102. '{:library}\data\{:class}\adapter\{:name}' => array('libraries' => 'lithium')
  103. ),
  104. 'helper' => array(
  105. '{:library}\extensions\helper\{:name}',
  106. '{:library}\template\helper\{:name}' => array('libraries' => 'lithium')
  107. ),
  108. 'libraries' => array(
  109. '{:app}/libraries/{:name}',
  110. '{:root}/{:name}'
  111. ),
  112. 'models' => array(
  113. '{:library}\models\{:name}'
  114. ),
  115. 'strategy' => array(
  116. '{:library}\extensions\strategy\{:namespace}\{:class}\{:name}',
  117. '{:library}\extensions\strategy\{:class}\{:name}',
  118. '{:library}\{:namespace}\{:class}\strategy\{:name}' => array('libraries' => 'lithium')
  119. ),
  120. 'socket' => array(
  121. '{:library}\extensions\net\socket\{:name}',
  122. '{:library}\extensions\socket\{:name}',
  123. '{:library}\net\socket\{:name}'
  124. ),
  125. 'test' => array(
  126. '{:library}\extensions\test\{:namespace}\{:class}\{:name}',
  127. '{:library}\test\{:namespace}\{:class}\{:name}' => array('libraries' => 'lithium')
  128. ),
  129. 'tests' => array(
  130. '{:library}\tests\{:namespace}\{:class}\{:name}Test'
  131. )
  132. );
  133. /**
  134. * Stores the name of the default library. When adding a library configuration to the
  135. * application, if the `'default'` option flag is set to `true`, the name of the library will
  136. * be assigned. To retrieve the default library's configuration, use `Libraries::get(true)`.
  137. *
  138. * @see lithium\core\Libraries::add()
  139. * @see lithium\core\Libraries::get()
  140. * @var string
  141. */
  142. protected static $_default;
  143. /**
  144. * Holds cached class paths generated and used by `lithium\core\Libraries::load()`.
  145. *
  146. * @var array
  147. * @see lithium\core\Libraries::load()
  148. */
  149. protected static $_cachedPaths = array();
  150. /**
  151. * Holds associations between fully-namespaced class names and file's paths mapped
  152. * with `lithium\core\Libraries::map()`.
  153. *
  154. * @var array
  155. * @see lithium\core\Libraries::map()
  156. * @see lithium\core\Libraries::unmap()
  157. */
  158. protected static $_map = array();
  159. /**
  160. * Accessor method for the class path templates which `Libraries` uses to look up and load
  161. * classes. Using this method, you can define your own types of classes, or modify the default
  162. * organization of built-in class types.
  163. *
  164. * For example, in a queuing application, you can define a class type called `'job'`:
  165. * {{{
  166. * Libraries::paths(array('job' => '{:library}\extensions\job\{:name}'));
  167. * }}}
  168. *
  169. * Then, any classes you add to the `extensions/job` directory in your application will be
  170. * automatically detected when calling `Libraries::locate('job')`. Additionally, any matching
  171. * classes in the `extensions/job` directory of any plugin or vendor library you add to your
  172. * application will also be detected.
  173. *
  174. * Supposing you wanted to have the option of further organizing jobs by class type (some jobs
  175. * are related to updating caches, others to sending notifications, etc.), you can specify
  176. * multiple paths per class type, with varying levels of specificity:
  177. * {{{
  178. * Libraries::paths(array('job' => array(
  179. * '{:library}\extensions\job\{:class}\{:name}',
  180. * '{:library}\extensions\job\{:name}'
  181. * )));
  182. * }}}
  183. *
  184. * This allows you to, for example, have two different classes called `Cleanup`. One may be
  185. * located in `app\extensions\job\Cleanup`, while the other is in
  186. * `app\extensions\job\cache\Cleanup`. Calling: {{{Libraries::locate('job');}}} will find
  187. * both classes, while {{{Libraries::locate('job.cache');}}} will only find the second. You can
  188. * also find individual jobs by name: {{{Libraries::locate('job', 'Cleanup');}}}
  189. *
  190. * See `Libraries::locate()` for more information on using built-in and user-defined paths to
  191. * look up classes.
  192. *
  193. * In addition to adding custom class types, `paths()` allows you to redefine the naming and
  194. * organization of existing types. For example, if you wished to reference your model classes
  195. * as `app\models\PostModel` instead of `app\models\Post`, you can do the following:
  196. * {{{Libraries::paths(array('models' => '{:library}\models\{:name}Model'));}}} Note, however,
  197. * that this is a destructive, not an additive operation, and will replace any existing paths
  198. * defined for that type. If you wish to add a search path for an existing type, you must do
  199. * the following:
  200. * {{{
  201. * $existing = Libraries::paths('controllers');
  202. * Libraries::paths(array('controller' => array_merge(
  203. * array('{:library}\extensions\controllers\{:name}Controller'), (array) $existing
  204. * )));
  205. * }}}
  206. *
  207. * @see lithium\core\Libraries::locate()
  208. * @see lithium\core\Libraries::$_paths
  209. * @param mixed $path If `$path` is a string, returns the path(s) associated with that path
  210. * type, or `null` if no paths are defined for that type.
  211. * @return mixed
  212. */
  213. public static function paths($path = null) {
  214. if (empty($path)) {
  215. return static::$_paths;
  216. }
  217. if (is_string($path)) {
  218. return isset(static::$_paths[$path]) ? static::$_paths[$path] : null;
  219. }
  220. static::$_paths = array_filter(array_merge(static::$_paths, (array) $path));
  221. }
  222. /**
  223. * Adds a class library from which files can be loaded.
  224. *
  225. * The `add()` method registers a named library configuration to your application, and is used
  226. * to allow the framework to auto-load classes on an as-needed basis.
  227. *
  228. * ### Adding libraries to your application
  229. *
  230. * In Lithium, libraries represent the broadest unit of class organization in an application,
  231. * and _everything_ is a library; this includes your application, and the Lithium framework
  232. * itself. Libraries can also be other frameworks, like Solar, Zend Framework or PEAR, or
  233. * Lithium plugins, which are simply libraries that follow the same organizational standards
  234. * as Lithium applications.
  235. *
  236. * By convention, libraries are placed in the `libraries` directory inside your application, or
  237. * the root `libraries` directory at the top level of the default distribution (i.e. the one
  238. * that contains the `lithium` directory), however, you can change this on a case-by-case basis
  239. * using the `'path'` key to specify an absolute path to the library's directory.
  240. *
  241. * @param string $name Library name, i.e. `'app'`, `'lithium'`, `'pear'` or `'aura'`.
  242. * @param array $config Specifies where the library is in the filesystem, and how classes
  243. * should be loaded from it. Allowed keys are:
  244. * - `'bootstrap'` _mixed_: A file path (relative to `'path'`) to a bootstrap script that
  245. * should be run when the library is added, or `true` to use the default bootstrap
  246. * path, i.e. `config/bootstrap.php`.
  247. * - `'defer'` _boolean_: If `true`, indicates that, when locating classes, this library
  248. * should defer to other libraries in order of preference.
  249. * - `'includePath'` _mixed_: If `true`, appends the absolutely-resolved value of
  250. * `'path'` to the PHP include path. If a string, the value is appended to PHP's.
  251. * - `'loader'`: An auto-loader method associated with the library, if any.
  252. * - `'path'`: The directory containing the library.
  253. * - `'prefix'` _string_: The class prefix this library uses, i.e. `'lithium\'`,
  254. * `'Zend_'` or `'Solar_'`. If the library has no global prefix, set to `false`.
  255. * - `'suffix'` _string_: Gets appended to the end of the file name. For example, most
  256. * libraries end classes in `'.php'`, but some use `'.class.php'`, or `'.inc.php'`.
  257. * - `'transform'` _closure_: Defines a custom way to transform a class name into its
  258. * corresponding file path. Accepts either an array of two strings which are
  259. * interpreted as the pattern and replacement for a regex, or an anonymous function,
  260. * which receives the class name and library configuration arrays as parameters, and
  261. * returns the full physical file path as output.
  262. * - `'resources'` _string_: If this is the default library, this maybe set to the
  263. * absolute path to the write-enabled application resources directory, which is used
  264. * for caching, log files, uploads, etc.
  265. * @return array Returns the resulting set of options created for this library.
  266. */
  267. public static function add($name, array $config = array()) {
  268. $defaults = array(
  269. 'path' => null,
  270. 'prefix' => $name . "\\",
  271. 'suffix' => '.php',
  272. 'loader' => null,
  273. 'includePath' => false,
  274. 'transform' => null,
  275. 'bootstrap' => true,
  276. 'defer' => false,
  277. 'default' => false
  278. );
  279. if ($name === 'lithium') {
  280. $defaults['defer'] = true;
  281. $defaults['bootstrap'] = false;
  282. $defaults['path'] = dirname(__DIR__);
  283. $defaults['loader'] = 'lithium\core\Libraries::load';
  284. }
  285. if (isset($config['default']) && $config['default']) {
  286. static::$_default = $name;
  287. $defaults['path'] = LITHIUM_APP_PATH;
  288. $defaults['bootstrap'] = false;
  289. $defaults['resources'] = LITHIUM_APP_PATH . '/resources';
  290. }
  291. $config += $defaults;
  292. if (!$config['path']) {
  293. if (!$config['path'] = static::_locatePath('libraries', compact('name'))) {
  294. throw new ConfigException("Library `{$name}` not found.");
  295. }
  296. }
  297. $config['path'] = str_replace('\\', '/', $config['path']);
  298. static::_configure(static::$_configurations[$name] = $config);
  299. return $config;
  300. }
  301. /**
  302. * Configures the application environment based on a library's settings, including appending to
  303. * the include path, loading a bootstrap file, and registering a loader with SPL's autoloading
  304. * system.
  305. *
  306. * @param array $config The new library's configuration array.
  307. * @return void
  308. */
  309. protected static function _configure($config) {
  310. if ($config['includePath']) {
  311. $path = ($config['includePath'] === true) ? $config['path'] : $config['includePath'];
  312. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  313. }
  314. if ($config['bootstrap'] === true) {
  315. $path = "{$config['path']}/config/bootstrap.php";
  316. $config['bootstrap'] = file_exists($path) ? 'config/bootstrap.php' : false;
  317. }
  318. if ($config['bootstrap']) {
  319. require "{$config['path']}/{$config['bootstrap']}";
  320. }
  321. if (!empty($config['loader'])) {
  322. spl_autoload_register($config['loader']);
  323. }
  324. }
  325. /**
  326. * Allows library information to be retrieved in various ways, including:
  327. *
  328. * By name:
  329. * {{{ embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(1-1) }}}
  330. *
  331. * With no parameters, to return all configuration for all libraries:
  332. * {{{ embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(22-22) }}}
  333. *
  334. * By list of names with a key to extract:
  335. * {{{ embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(34-34) }}}
  336. *
  337. * With no name, and a key to extract, to return a key/value array, where the library name is
  338. * the key, and the `$key` value is the value:
  339. * {{{ embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(37-37) }}}
  340. *
  341. * By containing class name:
  342. * {{{ embed:lithium\tests\cases\core\LibrariesTest::testLibraryConfigAccess(45-45) }}}
  343. *
  344. * @param mixed $name Either the name of a library added in `Libraries::add()`, an array of
  345. * library names, or a fully-namespaced class name (see usage examples above).
  346. * @param string $key Optional key name. If `$name` is set and is the name of a valid library
  347. * (or an array of valid libraries), returns the given named configuration key,
  348. * i.e. `'path'`, `'webroot'` or `'resources'`.
  349. * @return mixed A configuation array for one or more libraries, or a string value if `$key` is
  350. * specified and `$name` is a string, or a library name (string) if `$name` is a
  351. * fully-namespaced class name.
  352. */
  353. public static function get($name = null, $key = null) {
  354. $configs = static::$_configurations;
  355. if (!$name && !$key) {
  356. return $configs;
  357. }
  358. if ($name === true) {
  359. $name = static::$_default;
  360. }
  361. if (is_array($name) || (!$name && $key)) {
  362. $name = $name ?: array_keys(static::$_configurations);
  363. $call = array(get_called_class(), 'get');
  364. return array_combine($name, array_map($call, $name, array_fill(0, count($name), $key)));
  365. }
  366. $config = isset($configs[$name]) ? $configs[$name] : null;
  367. if ($key) {
  368. return isset($config[$key]) ? $config[$key] : null;
  369. }
  370. if (strpos($name, '\\') === false) {
  371. return $config;
  372. }
  373. foreach (static::$_configurations as $library => $config) {
  374. if ($config['prefix'] && strpos($name, $config['prefix']) === 0) {
  375. return $library;
  376. }
  377. }
  378. }
  379. /**
  380. * Removes a registered library, and unregister's the library's autoloader, if it has one.
  381. *
  382. * @param mixed $name A string or array of library names indicating the libraries you wish to
  383. * remove, i.e. `'app'` or `'lithium'`. This can also be used to unload plugins by name.
  384. * @return void
  385. */
  386. public static function remove($name) {
  387. foreach ((array) $name as $library) {
  388. if (isset(static::$_configurations[$library])) {
  389. if (static::$_configurations[$library]['loader']) {
  390. spl_autoload_unregister(static::$_configurations[$library]['loader']);
  391. }
  392. unset(static::$_configurations[$library]);
  393. }
  394. }
  395. }
  396. /**
  397. * Finds the classes or namespaces belonging to a particular library. _Note_: This method
  398. * assumes loaded class libraries use a consistent class-to-file naming convention.
  399. *
  400. * @param mixed $library The name of a library added to the application with `Libraries::add()`,
  401. * or `true` to search all libraries.
  402. * @param array $options The options this method accepts:
  403. *
  404. * - `'path'` _string_: A physical filesystem path relative to the directory of the
  405. * library being searched. If provided, only the classes or namespaces within
  406. * this path will be returned.
  407. * - `'recursive'` _boolean_: If `true`, recursively searches all directories
  408. * (namespaces) in the given library. If `false` (the default), only searches the
  409. * top level of the given path.
  410. * - `'filter'` _string_: A regular expression applied to a class after it is
  411. * transformed into a fully-namespaced class name. The default regular expression
  412. * filters class names based on the
  413. * [PSR-0](http://groups.google.com/group/php-standards/web/psr-0-final-proposal)
  414. * PHP 5.3 naming standard.
  415. * - `'exclude'` _mixed_: Can be either a regular expression of classes/namespaces
  416. * to exclude, or a PHP callable to be used with `array_filter()`.
  417. * - `'namespaces'` _boolean_: Indicates whether namespaces should be included in
  418. * the search results. If `false` (the default), only classes are returned.
  419. * @return array Returns an array of fully-namespaced class names found in the given library or
  420. * libraries.
  421. * @todo Patch this to skip paths belonging to nested libraries in recursive searches.
  422. */
  423. public static function find($library, array $options = array()) {
  424. $format = function($file, $config) {
  425. $trim = array(strlen($config['path']) + 1, strlen($config['suffix']));
  426. $rTrim = strpos($file, $config['suffix']) !== false ? -$trim[1] : 9999;
  427. $file = preg_split('/[\/\\\\]/', substr($file, $trim[0], $rTrim));
  428. return $config['prefix'] . join('\\', $file);
  429. };
  430. $defaults = compact('format') + array(
  431. 'path' => '',
  432. 'recursive' => false,
  433. 'filter' => '/^(\w+)?(\\\\[a-z0-9_]+)+\\\\[A-Z][a-zA-Z0-9]+$/',
  434. 'exclude' => '',
  435. 'namespaces' => false
  436. );
  437. $options += $defaults;
  438. $libs = array();
  439. if ($options['namespaces'] && $options['filter'] === $defaults['filter']) {
  440. $options['format'] = function($class, $config) use ($format, $defaults) {
  441. if (is_dir($class)) {
  442. return $format($class, $config);
  443. }
  444. if (preg_match($defaults['filter'], $class = $format($class, $config))) {
  445. return $class;
  446. }
  447. };
  448. $options['filter'] = false;
  449. }
  450. if ($library === true) {
  451. foreach (static::$_configurations as $library => $config) {
  452. $libs = array_merge($libs, static::find($library, $options));
  453. }
  454. return $libs;
  455. }
  456. if (!isset(static::$_configurations[$library])) {
  457. return null;
  458. }
  459. $config = static::$_configurations[$library];
  460. $options['path'] = "{$config['path']}{$options['path']}/*";
  461. $libs = static::_search($config, $options);
  462. return array_values(array_filter($libs));
  463. }
  464. /**
  465. * Loads the class definition specified by `$class`. Also calls the `__init()` method on the
  466. * class, if defined. Looks through the list of libraries defined in `$_configurations`, which
  467. * are added through `lithium\core\Libraries::add()`.
  468. *
  469. * @see lithium\core\Libraries::add()
  470. * @see lithium\core\Libraries::path()
  471. * @param string $class The fully-namespaced (where applicable) name of the class to load.
  472. * @param boolean $require Specifies whether the class must be loaded or considered an
  473. * exception. Defaults to `false`.
  474. * @return void
  475. */
  476. public static function load($class, $require = false) {
  477. $path = isset(static::$_cachedPaths[$class]) ? static::$_cachedPaths[$class] : null;
  478. $path = $path ?: static::path($class);
  479. if ($path && include $path) {
  480. static::$_cachedPaths[$class] = $path;
  481. method_exists($class, '__init') ? $class::__init() : null;
  482. } elseif ($require) {
  483. throw new RuntimeException("Failed to load class `{$class}` from path `{$path}`.");
  484. }
  485. }
  486. /**
  487. * Associtates fully-namespaced class names to their corresponding paths on
  488. * the file system.
  489. *
  490. * Once a class is associtated to a path using `lithium\core\Libraries::map()`
  491. * the PSR-0 loader or custom class loader setted using the `transform` or `loader`
  492. * option of `lithium\core\Libraries::add()` are ignored and the associtated path
  493. * is used instead.
  494. *
  495. * @param array $classes An array of fully-namespaced class names (as keys) and
  496. * their correponding file's paths (as values).
  497. * @return void
  498. */
  499. public static function map(array $classes) {
  500. foreach ($classes as $key => $value) {
  501. unset(static::$_cachedPaths[$key]);
  502. }
  503. static::$_map = array_merge(static::$_map, $classes);
  504. }
  505. /**
  506. * Unmap fully-namespaced class names mapped using `lithium\core\Libraries::map()`.
  507. *
  508. * @see lithium\core\Libraries::map()
  509. * @param mixed $classes An array of fully-namespaced class names or
  510. * a string with a fully-namespaced class name.
  511. */
  512. public static function unmap($classes) {
  513. if (!is_array($classes)) {
  514. $classes = array($classes);
  515. }
  516. foreach ($classes as $value) {
  517. unset(static::$_map[$value]);
  518. }
  519. }
  520. /**
  521. * Get the corresponding physical file path for a class or namespace name.
  522. *
  523. * @param string $class The class name to locate the physical file for. If `$options['dirs']` is
  524. * set to `true`, `$class` may also be a namespace name, in which case the corresponding
  525. * directory will be located.
  526. * @param array $options Options for converting `$class` to a physical path:
  527. * - `'dirs'`: Defaults to `false`. If `true`, will attempt to case-sensitively look up
  528. * directories in addition to files (in which case `$class` is assumed to actually be a
  529. * namespace).
  530. * @return string Returns the absolute path to the file containing `$class`, or `null` if the
  531. * file cannot be found.
  532. */
  533. public static function path($class, array $options = array()) {
  534. $defaults = array('dirs' => false);
  535. $options += $defaults;
  536. $class = ltrim($class, '\\');
  537. if (isset(static::$_cachedPaths[$class]) && !$options['dirs']) {
  538. return static::$_cachedPaths[$class];
  539. }
  540. if (isset(static::$_map[$class]) && !$options['dirs']) {
  541. return static::$_map[$class];
  542. }
  543. foreach (static::$_configurations as $name => $config) {
  544. $params = $options + $config;
  545. $suffix = $params['suffix'];
  546. if ($params['prefix'] && strpos($class, $params['prefix']) !== 0) {
  547. continue;
  548. }
  549. if ($transform = $params['transform']) {
  550. if ($file = static::_transformPath($transform, $class, $params)) {
  551. return $file;
  552. }
  553. continue;
  554. }
  555. $path = str_replace("\\", '/', substr($class, strlen($params['prefix'])));
  556. $fullPath = "{$params['path']}/{$path}";
  557. if (!$options['dirs']) {
  558. return static::$_cachedPaths[$class] = static::realPath($fullPath . $suffix);
  559. }
  560. $list = glob(dirname($fullPath) . '/*');
  561. $list = array_map(function($i) { return str_replace('\\', '/', $i); }, $list);
  562. if (in_array($fullPath . $suffix, $list)) {
  563. return static::$_cachedPaths[$class] = static::realPath($fullPath . $suffix);
  564. }
  565. return is_dir($fullPath) ? static::realPath($fullPath) : null;
  566. }
  567. }
  568. /**
  569. * Wraps the PHP `realpath()` function to add support for finding paths to files inside Phar
  570. * archives.
  571. *
  572. * @param string $path An unresolved path to a file inside a Phar archive which may or may not
  573. * exist.
  574. * @return string If `$path` is a valid path to a file inside a Phar archive, returns a string
  575. * in the format `'phar://<path-to-phar>/<path-to-file>'`. Otherwise returns
  576. * `null`.
  577. */
  578. public static function realPath($path) {
  579. if (($absolutePath = realpath($path)) !== false) {
  580. return $absolutePath;
  581. }
  582. if (!preg_match('%^phar://([^.]+\.phar(?:\.gz)?)(.+)%', $path, $pathComponents)) {
  583. return;
  584. }
  585. list(, $relativePath, $pharPath) = $pathComponents;
  586. $pharPath = implode('/', array_reduce(explode('/', $pharPath), function ($parts, $value) {
  587. if ($value === '..') {
  588. array_pop($parts);
  589. } elseif ($value !== '.') {
  590. $parts[] = $value;
  591. }
  592. return $parts;
  593. }));
  594. if (($resolvedPath = realpath($relativePath)) !== false) {
  595. if (file_exists($absolutePath = "phar://{$resolvedPath}{$pharPath}")) {
  596. return $absolutePath;
  597. }
  598. }
  599. }
  600. /**
  601. * Handles the conversion of a class name to a file name using a custom transformation typically
  602. * defined in the `'transform'` key of a configuration defined through `Libraries::add()`.
  603. *
  604. * The transformation can either be a closure which receives two parameters (the class name
  605. * as a string, and the library configuration as an array), or an array with two values (one
  606. * being the pattern to match, the other being the replacement).
  607. *
  608. * @see lithium\core\Libraries::add()
  609. * @see lithium\core\Libraries::path()
  610. * @param mixed $transform Either a closure or an array containing a regular expression match
  611. * and replacement. If the closure returns an empty value, or the regular
  612. * expression fails to match, will return `null`.
  613. * @param string $class The class name which is attempting to be mapped to a file.
  614. * @param array $options The configuration of the library as passed to `Libraries::add()`, along
  615. * with any options specified in the call to `Libraries::path()`.
  616. * @return string Returns transformed path of a class to a file, or `null` if the transformation
  617. * did not match.
  618. */
  619. protected static function _transformPath($transform, $class, array $options = array()) {
  620. if ((is_callable($transform)) && $file = $transform($class, $options)) {
  621. return $file;
  622. }
  623. if (is_array($transform)) {
  624. list($match, $replace) = $transform;
  625. return preg_replace($match, $replace, $class) ?: null;
  626. }
  627. }
  628. /**
  629. * Uses service location (i.e. `Libraries::locate()`) to look up a named class of a particular
  630. * type, and creates an instance of it, and passes an array of parameters to the constructor.
  631. *
  632. * If the given class can't be found, an exception is thrown.
  633. *
  634. * @param string $type The type of class as defined by `Libraries::$_paths`.
  635. * @param string $name The un-namespaced name of the class to instantiate.
  636. * @param array $options An array of constructor parameters to pass to the class.
  637. * @return object If the class is found, returns an instance of it, otherwise throws an
  638. * exception.
  639. * @throws lithium\core\ClassNotFoundException Throws an exception if the class can't be found.
  640. * @filter
  641. */
  642. public static function instance($type, $name, array $options = array()) {
  643. $params = compact('type', 'name', 'options');
  644. $_paths =& static::$_paths;
  645. $implementation = function($self, $params) use (&$_paths) {
  646. $name = $params['name'];
  647. $type = $params['type'];
  648. if (!$name && !$type) {
  649. $message = "Invalid class lookup: `\$name` and `\$type` are empty.";
  650. throw new ClassNotFoundException($message);
  651. }
  652. if (!is_string($type) && $type !== null && !isset($_paths[$type])) {
  653. throw new ClassNotFoundException("Invalid class type `{$type}`.");
  654. }
  655. if (!$class = $self::locate($type, $name)) {
  656. throw new ClassNotFoundException("Class `{$name}` of type `{$type}` not found.");
  657. }
  658. if (is_object($class)) {
  659. return $class;
  660. }
  661. if (!(is_string($class) && class_exists($class))) {
  662. throw new ClassNotFoundException("Class `{$name}` of type `{$type}` not defined.");
  663. }
  664. return new $class($params['options']);
  665. };
  666. if (!isset(static::$_methodFilters[__FUNCTION__])) {
  667. return $implementation(get_called_class(), $params);
  668. }
  669. $class = get_called_class();
  670. $method = __FUNCTION__;
  671. $data = array_merge(static::$_methodFilters[__FUNCTION__], array($implementation));
  672. return Filters::run($class, $params, compact('data', 'class', 'method'));
  673. }
  674. /**
  675. * Apply a closure to a method in `Libraries`.
  676. *
  677. * @see lithium\util\collection\Filters
  678. * @param string $method The name of the method to apply the closure to.
  679. * @param closure $filter The closure that is used to filter the method.
  680. * @return void
  681. */
  682. public static function applyFilter($method, $filter = null) {
  683. if (!isset(static::$_methodFilters[$method])) {
  684. static::$_methodFilters[$method] = array();
  685. }
  686. static::$_methodFilters[$method][] = $filter;
  687. }
  688. /**
  689. * Performs service location for an object of a specific type. If `$name` is a string, finds the
  690. * first instance of a class with the given name in any registered library (i.e. apps, plugins
  691. * or vendor libraries registered via `Libraries::add()`), based on each library's order of
  692. * precedence. For example, this will find the first model called `File` in any plugin or class
  693. * library loaded into an application, including the application itself.
  694. *
  695. * {{{Libraries::locate('models', 'File');}}}
  696. *
  697. * Order of precedence is usually based on the order in which the library was registered (via
  698. * `Libraries::add()`), unless the library was registered with the `'defer'` option set to
  699. * `true`. All libraries with the `'defer'` option set will be searched in
  700. * registration-order **after** searching all libraries **without** `'defer'` set. This means
  701. * that in the above example, if an app and a plugin both have a model named `File`, then the
  702. * model from the app will be returned first, assuming the app was registered first (and
  703. * assuming the default settings).
  704. *
  705. * If `$name` is not specified, `locate()` returns an array with all classes of the specified
  706. * type which can be found. By default, `locate()` searches all registered libraries.
  707. *
  708. * {{{Libraries::locate('models');}}}
  709. *
  710. * For example, the above will return an array of all model classes in all registered plugins
  711. * and libraries (including the app itself).
  712. *
  713. * To learn more about adding and modifying the class paths used with `locate()`, see the
  714. * documentation for the `paths()` method.
  715. *
  716. * @see lithium\core\Libraries::paths()
  717. * @see lithium\core\Libraries::add()
  718. * @see lithium\core\Libraries::_locateDeferred()
  719. * @param string $type The type of class to search for. Typically follows the name of the
  720. * directory in which the class is stored, i.e. `'models'`, `'controllers'` or
  721. * `'adapter'`. Some classes types, such as adapters, will require a greater
  722. * degree of specificity when looking up the desired class. In this case, the dot
  723. * syntax is used, as in this example when looking up cache adapters:
  724. * `'adapter.storage.cache'`, or this example, when looking up authentication
  725. * adapters: `'adapter.security.auth'`.
  726. * @param string $name The base name (without namespace) of the class you wish to locate. If
  727. * unspecified, `locate()` will attempt to find all classes of the type specified
  728. * in `$type`. If you only wish to search for classes within a single plugin or
  729. * library, you may use the dot syntax to prefix the class name with the library
  730. * name, i.e. `'app.Post'`, which will only look for a `Post` model within the
  731. * app itself.
  732. * @param array $options The options to use when searching and returning class names.
  733. * - `'type'` _string_: Defaults to `'class'`. If set to `'file'`, returns file
  734. * names instead of class names.
  735. * - `'library'` _string_: When specified, only the given library/plugin name will
  736. * be searched.
  737. * @return mixed If `$name` is specified, returns the name of the first class found that matches
  738. * `$name` and `$type`, or returns `null` if no matching classes were found in any
  739. * registered library. If `$name` is not specified, returns an array of all classes
  740. * found which match `$type`.
  741. */
  742. public static function locate($type, $name = null, array $options = array()) {
  743. if (is_object($name) || strpos($name, '\\') !== false) {
  744. return $name;
  745. }
  746. $ident = $name ? ($type . '.' . $name) : ($type . '.*');
  747. $ident .= $options ? '.' . md5(serialize($options)) : null;
  748. if (isset(static::$_cachedPaths[$ident])) {
  749. return static::$_cachedPaths[$ident];
  750. }
  751. $params = static::_params($type, $name);
  752. $defaults = array(
  753. 'type' => 'class',
  754. 'library' => $params['library'] !== '*' ? $params['library'] : null
  755. );
  756. $options += $defaults;
  757. unset($params['library']);
  758. $paths = static::paths($params['type']);
  759. if (!isset($paths)) {
  760. return null;
  761. }
  762. if ($params['name'] === '*') {
  763. $result = static::_locateAll($params, $options);
  764. return (static::$_cachedPaths[$ident] = $result);
  765. }
  766. if ($options['library']) {
  767. $result = static::_locateDeferred(null, $paths, $params, $options);
  768. return static::$_cachedPaths[$ident] = $result;
  769. }
  770. foreach (array(false, true) as $defer) {
  771. if ($result = static::_locateDeferred($defer, $paths, $params, $options)) {
  772. return (static::$_cachedPaths[$ident] = $result);
  773. }
  774. }
  775. }
  776. /**
  777. * Returns or sets the the class path cache used for mapping class names to file paths, or
  778. * locating classes using `Libraries::locate()`.
  779. *
  780. * @param array $cache An array of keys and values to use when pre-populating the cache. Keys
  781. * are either class names (which match to file paths as values), or dot-separated
  782. * lookup paths used by `locate()` (which matches to either a single class or an
  783. * array of classes). If `false`, the cache is cleared.
  784. * @return array Returns an array of cached class lookups, formatted per the description for
  785. * `$cache`.
  786. */
  787. public static function cache($cache = null) {
  788. if ($cache === false) {
  789. static::$_cachedPaths = array();
  790. }
  791. if (is_array($cache)) {
  792. static::$_cachedPaths += $cache;
  793. }
  794. return static::$_cachedPaths;
  795. }
  796. /**
  797. * Performs service location lookups by library, based on the library's `'defer'` flag.
  798. * Libraries with `'defer'` set to `true` will be searched last when looking up services.
  799. *
  800. * @see lithium\core\Libraries::$_paths
  801. * @see lithium\core\Libraries::locate()
  802. * @param boolean $defer A boolean flag indicating which libraries to search, either the ones
  803. * with the `'defer'` flag set, or the ones without.
  804. * @param array $paths List of paths to be searched for the given service (class). These are
  805. * defined in `lithium\core\Libraries::$_paths`, and are organized by class type.
  806. * @param array $params The list of insert parameters to be injected into each path format
  807. * string when searching for classes.
  808. * @param array $options
  809. * @return string Returns a class path as a string if a given class is found, or null if no
  810. * class in any path matching any of the parameters is located.
  811. */
  812. protected static function _locateDeferred($defer, $paths, $params, array $options = array()) {
  813. $libraries = static::$_configurations;
  814. if (isset($options['library'])) {
  815. $libraries = static::get((array) $options['library']);
  816. }
  817. foreach ($libraries as $library => $config) {
  818. if ($config['defer'] !== $defer && $defer !== null) {
  819. continue;
  820. }
  821. foreach (static::_searchPaths($paths, $library, $params) as $tpl) {
  822. $params['library'] = rtrim($config['prefix'], '\\');
  823. $class = str_replace('\\*', '', String::insert($tpl, $params));
  824. if (file_exists($file = Libraries::path($class, $options))) {
  825. return ($options['type'] === 'file') ? $file : $class;
  826. }
  827. }
  828. }
  829. }
  830. /**
  831. * Returns the list of valid search path templates for the given service location lookup.
  832. *
  833. * @see lithium\core\Libraries::$_paths
  834. * @see lithium\core\Libraries::_search()
  835. * @param array $paths The list of all possible path templates from `Libraries::$_paths`.
  836. * @param string $library The name of the library being searched.
  837. * @param array $params The parameters used in the service location lookup.
  838. * @return array Returns an array of valid path template strings.
  839. */
  840. protected static function _searchPaths($paths, $library, $params) {
  841. $result = array();
  842. $params = array('library' => null, 'type' => null) + $params;
  843. foreach ($paths as $tpl => $opts) {
  844. if (is_int($tpl)) {
  845. $tpl = $opts;
  846. $opts = array();
  847. }
  848. if (isset($opts['libraries']) && !in_array($library, (array) $opts['libraries'])) {
  849. continue;
  850. }
  851. $result[] = $tpl;
  852. }
  853. return $result;
  854. }
  855. /**
  856. * Locates all possible classes for given set of parameters.
  857. *
  858. * @param array $params
  859. * @param array $options
  860. * @return array
  861. */
  862. protected static function _locateAll(array $params, array $options = array()) {
  863. $defaults = array('libraries' => null, 'recursive' => true, 'namespaces' => false);
  864. $options += $defaults;
  865. $paths = (array) static::$_paths[$params['type']];
  866. $libraries = $options['library'] ? $options['library'] : $options['libraries'];
  867. $libraries = static::get((array) $libraries);
  868. $flags = array('escape' => '/');
  869. $classes = array();
  870. foreach ($libraries as $library => $config) {
  871. $params['library'] = $config['path'];
  872. foreach (static::_searchPaths($paths, $library, $params) as $tpl) {
  873. $options['path'] = str_replace('\\', '/', String::insert($tpl, $params, $flags));
  874. $options['path'] = str_replace('*/', '', $options['path']);
  875. $classes = array_merge($classes, static::_search($config, $options));
  876. }
  877. }
  878. return array_unique($classes);
  879. }
  880. /**
  881. * Helper function for returning known paths given a certain type.
  882. *
  883. * @see lithium\core\Libraries::$_paths
  884. * @param string $type Path type (specified in `Libraries::$_paths`).
  885. * @param string $params Path parameters.
  886. * @return string Valid path name.
  887. */
  888. protected static function _locatePath($type, $params) {
  889. if (!isset(static::$_paths[$type])) {
  890. return;
  891. }
  892. $params += array('app' => LITHIUM_APP_PATH, 'root' => LITHIUM_LIBRARY_PATH);
  893. foreach (static::$_paths[$type] as $path) {
  894. if (is_dir($path = str_replace('\\', '/', String::insert($path, $params)))) {
  895. return $path;
  896. }
  897. }
  898. }
  899. /**
  900. * Search file system.
  901. *
  902. * @param string $config
  903. * @param string $options
  904. * @param string $name
  905. * @return array
  906. */
  907. protected static function _search($config, $options, $name = null) {
  908. $defaults = array(
  909. 'path' => null,
  910. 'suffix' => null,
  911. 'namespaces' => false,
  912. 'recursive' => false,
  913. 'preFilter' => '/[A-Z][A-Za-z0-9]+\./',
  914. 'filter' => false,
  915. 'exclude' => false,
  916. 'format' => function ($file, $config) {
  917. $trim = array(strlen($config['path']) + 1, strlen($config['suffix']));
  918. $file = substr($file, $trim[0], -$trim[1]);
  919. return $config['prefix'] . str_replace('/', '\\', $file);
  920. }
  921. );
  922. $options += $defaults;
  923. $path = $options['path'];
  924. $suffix = $options['namespaces'] ? '' : $config['suffix'];
  925. $suffix = ($options['suffix'] === null) ? $suffix : $options['suffix'];
  926. $dFlags = GLOB_ONLYDIR & GLOB_BRACE;
  927. $libs = (array) glob($path . $suffix, $options['namespaces'] ? $dFlags : GLOB_BRACE);
  928. if ($options['recursive']) {
  929. list($current, $match) = explode('/*', $path, 2);
  930. $dirs = $queue = array_diff((array) glob($current . '/*', $dFlags), $libs);
  931. $match = str_replace('##', '.+', preg_quote(str_replace('*', '##', $match), '/'));
  932. $match = '/' . $match . preg_quote($suffix, '/') . '$/';
  933. while ($queue) {
  934. if (!is_dir($dir = array_pop($queue))) {
  935. continue;
  936. }
  937. $libs = array_merge($libs, (array) glob("{$dir}/*{$suffix}"));
  938. $queue = array_merge($queue, array_diff((array) glob("{$dir}/*", $dFlags), $libs));
  939. }
  940. $libs = preg_grep($match, $libs);
  941. }
  942. if ($suffix) {
  943. $libs = $options['preFilter'] ? preg_grep($options['preFilter'], $libs) : $libs;
  944. }
  945. return static::_filter($libs, (array) $config, $options + compact('name'));
  946. }
  947. /**
  948. * Filters a list of library search results by the given set of options.
  949. *
  950. * @param array $libs List of found libraries.
  951. * @param array $config The configuration of the library currently being searched within.
  952. * @param array $options The options used to filter/format `$libs`.
  953. * @return array Returns a copy of `$libs`, filtered and transformed based on the configuration
  954. * provided in `$options`.
  955. */
  956. protected static function _filter($libs, array $config, array $options = array()) {
  957. if (is_callable($options['format'])) {
  958. foreach ($libs as $i => $file) {
  959. $libs[$i] = $options['format']($file, $config);
  960. }
  961. $libs = $options['name'] ? preg_grep("/{$options['name']}$/", $libs) : $libs;
  962. }
  963. if ($exclude = $options['exclude']) {
  964. if (is_string($exclude)) {
  965. $libs = preg_grep($exclude, $libs, PREG_GREP_INVERT);
  966. } elseif (is_callable($exclude)) {
  967. $libs = array_values(array_filter($libs, $exclude));
  968. }
  969. }
  970. if ($filter = $options['filter']) {
  971. if (is_string($filter)) {
  972. $libs = preg_grep($filter, $libs) ;
  973. } elseif (is_callable($filter)) {
  974. $libs = array_filter(array_map($filter, $libs));
  975. }
  976. }
  977. return $libs;
  978. }
  979. /**
  980. * Get params from type.
  981. *
  982. * @param string $type
  983. * @param string $name default: '*'
  984. * @return array type, namespace, class, name
  985. */
  986. protected static function _params($type, $name = "*") {
  987. if (!$name) {
  988. $name = '*';
  989. }
  990. $library = $namespace = $class = '*';
  991. if (strpos($type, '.') !== false) {
  992. $parts = explode('.', $type);
  993. $type = array_shift($parts);
  994. switch (count($parts)) {
  995. case 1:
  996. list($class) = $parts;
  997. break;
  998. case 2:
  999. list($namespace, $class) = $parts;
  1000. break;
  1001. default:
  1002. $class = array_pop($parts);
  1003. $namespace = join('\\', $parts);
  1004. break;
  1005. }
  1006. }
  1007. if (strpos($name, '.') !== false) {
  1008. $parts = explode('.', $name);
  1009. $library = array_shift($parts);
  1010. $name = array_pop($parts);
  1011. $namespace = $parts ? join('\\', $parts) : "*";
  1012. }
  1013. return compact('library', 'namespace', 'type', 'class', 'name');
  1014. }
  1015. }
  1016. ?>