package.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://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. foreach ($package as $pkg => $path)
  47. {
  48. if (is_numeric($pkg))
  49. {
  50. $pkg = $path;
  51. $path = null;
  52. }
  53. static::load($pkg, $path);
  54. }
  55. return false;
  56. }
  57. if (static::loaded($package))
  58. {
  59. return;
  60. }
  61. // if no path is given, try to locate the package
  62. if ($path === null)
  63. {
  64. $paths = \Config::get('package_paths', array());
  65. empty($paths) and $paths = array(PKGPATH);
  66. if ( ! empty($paths))
  67. {
  68. foreach ($paths as $modpath)
  69. {
  70. if (is_dir($path = $modpath.strtolower($package).DS))
  71. {
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. if ( ! is_dir($path))
  78. {
  79. throw new \PackageNotFoundException("Package '$package' could not be found at '".\Fuel::clean_path($path)."'");
  80. }
  81. \Finder::instance()->add_path($path, 1);
  82. \Fuel::load($path.'bootstrap.php');
  83. static::$packages[$package] = $path;
  84. return true;
  85. }
  86. /**
  87. * Unloads a package from the stack.
  88. *
  89. * @param string $pacakge The package name
  90. * @return void
  91. */
  92. public static function unload($package)
  93. {
  94. \Finder::instance()->remove_path(static::$packages[$package]);
  95. unset(static::$packages[$package]);
  96. }
  97. /**
  98. * Checks if the given package is loaded, if no package is given then
  99. * all loaded packages are returned.
  100. *
  101. * @param string|null $package The package name or null
  102. * @return bool|array Whether the package is loaded, or all packages
  103. */
  104. public static function loaded($package = null)
  105. {
  106. if ($package === null)
  107. {
  108. return static::$packages;
  109. }
  110. return array_key_exists($package, static::$packages);
  111. }
  112. /**
  113. * Checks if the given package exists.
  114. *
  115. * @param string $package The package name
  116. * @return bool|string Path to the package found, or false if not found
  117. */
  118. public static function exists($package)
  119. {
  120. if (array_key_exists($package, static::$packages))
  121. {
  122. return static::$packages[$package];
  123. }
  124. else
  125. {
  126. $paths = \Config::get('package_paths', array());
  127. empty($paths) and $paths = array(PKGPATH);
  128. foreach ($paths as $path)
  129. {
  130. if (is_dir($path.$package))
  131. {
  132. return $path.$package.DS;
  133. }
  134. }
  135. }
  136. return false;
  137. }
  138. }