PageRenderTime 89ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/package.php

http://github.com/fuel/core
PHP | 159 lines | 85 code | 17 blank | 57 comment | 10 complexity | 22f5c5070377a3e1029c7a02c5c9a369 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP 5.4+ framework.
  4. *
  5. * @package Fuel
  6. * @version 1.9-dev
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2019 Fuel Development Team
  10. * @link https://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * This exception is thrown when a package cannot be found.
  15. *
  16. * @package Core
  17. * @subpackage Packages
  18. */
  19. class PackageNotFoundException extends \FuelException { }
  20. /**
  21. * Handles all the loading, unloading and management of packages.
  22. *
  23. * @package Core
  24. * @subpackage Packages
  25. */
  26. class Package
  27. {
  28. /**
  29. * @var array $packages Holds all the loaded package information.
  30. */
  31. protected static $packages = array();
  32. /**
  33. * Loads the given package. If a path is not given, if will search through
  34. * the defined package_paths. If not defined, then PKGPATH is used.
  35. * It also accepts an array of packages as the first parameter.
  36. *
  37. * @param string|array $package The package name or array of packages.
  38. * @param string|null $path The path to the package
  39. * @return bool True on success
  40. * @throws \PackageNotFoundException
  41. */
  42. public static function load($package, $path = null)
  43. {
  44. if (is_array($package))
  45. {
  46. $result = true;
  47. foreach ($package as $pkg => $path)
  48. {
  49. if (is_numeric($pkg))
  50. {
  51. $pkg = $path;
  52. $path = null;
  53. }
  54. // MUST use external brackets due to prio('and') < prio('=') < prio('&&') -
  55. // don't remove them xor replace 'and' with '&&'
  56. $result = (static::load($pkg, $path) and $result);
  57. }
  58. return $result;
  59. }
  60. if (static::loaded($package))
  61. {
  62. return false;
  63. }
  64. // if no path is given, try to locate the package
  65. if ($path === null)
  66. {
  67. $paths = \Config::get('package_paths', array());
  68. empty($paths) and $paths = array(PKGPATH);
  69. if ( ! empty($paths))
  70. {
  71. foreach ($paths as $modpath)
  72. {
  73. if (is_dir($path = $modpath.strtolower($package).DS))
  74. {
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. if ( ! is_dir($path))
  81. {
  82. throw new \PackageNotFoundException("Package '$package' could not be found at '".\Fuel::clean_path($path)."'");
  83. }
  84. \Finder::instance()->add_path($path, 1);
  85. \Fuel::load($path.'bootstrap.php');
  86. static::$packages[$package] = $path;
  87. return true;
  88. }
  89. /**
  90. * Unloads a package from the stack.
  91. *
  92. * @param string $package The package name
  93. * @return void
  94. */
  95. public static function unload($package)
  96. {
  97. \Finder::instance()->remove_path(static::$packages[$package]);
  98. unset(static::$packages[$package]);
  99. }
  100. /**
  101. * Checks if the given package is loaded, if no package is given then
  102. * all loaded packages are returned.
  103. *
  104. * @param string|null $package The package name or null
  105. * @return bool|array Whether the package is loaded, or all packages
  106. */
  107. public static function loaded($package = null)
  108. {
  109. if ($package === null)
  110. {
  111. return static::$packages;
  112. }
  113. return array_key_exists($package, static::$packages);
  114. }
  115. /**
  116. * Checks if the given package exists.
  117. *
  118. * @param string $package The package name
  119. * @return bool|string Path to the package found, or false if not found
  120. */
  121. public static function exists($package)
  122. {
  123. if (array_key_exists($package, static::$packages))
  124. {
  125. return static::$packages[$package];
  126. }
  127. else
  128. {
  129. $paths = \Config::get('package_paths', array());
  130. empty($paths) and $paths = array(PKGPATH);
  131. $package = strtolower($package);
  132. foreach ($paths as $path)
  133. {
  134. if (is_dir($path.$package))
  135. {
  136. return $path.$package.DS;
  137. }
  138. }
  139. }
  140. return false;
  141. }
  142. }