laravels 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * This autoloader is only used to pull laravel-s.
  5. * Class Psr4Autoloader
  6. */
  7. class Psr4Autoloader
  8. {
  9. /**
  10. * An associative array where the key is a namespace prefix and the value
  11. * is an array of base directories for classes in that namespace.
  12. *
  13. * @var array
  14. */
  15. protected $prefixes = array();
  16. /**
  17. * Register loader with SPL autoloader stack.
  18. *
  19. * @return void
  20. */
  21. public function register()
  22. {
  23. spl_autoload_register(array($this, 'loadClass'));
  24. }
  25. /**
  26. * Adds a base directory for a namespace prefix.
  27. *
  28. * @param string $prefix The namespace prefix.
  29. * @param string $base_dir A base directory for class files in the
  30. * namespace.
  31. * @param bool $prepend If true, prepend the base directory to the stack
  32. * instead of appending it; this causes it to be searched first rather
  33. * than last.
  34. * @return void
  35. */
  36. public function addNamespace($prefix, $base_dir, $prepend = false)
  37. {
  38. // normalize namespace prefix
  39. $prefix = trim($prefix, '\\') . '\\';
  40. // normalize the base directory with a trailing separator
  41. $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/';
  42. // initialize the namespace prefix array
  43. if (isset($this->prefixes[$prefix]) === false) {
  44. $this->prefixes[$prefix] = array();
  45. }
  46. // retain the base directory for the namespace prefix
  47. if ($prepend) {
  48. array_unshift($this->prefixes[$prefix], $base_dir);
  49. } else {
  50. array_push($this->prefixes[$prefix], $base_dir);
  51. }
  52. }
  53. /**
  54. * Loads the class file for a given class name.
  55. *
  56. * @param string $class The fully-qualified class name.
  57. * @return mixed The mapped file name on success, or boolean false on
  58. * failure.
  59. */
  60. public function loadClass($class)
  61. {
  62. // the current namespace prefix
  63. $prefix = $class;
  64. // work backwards through the namespace names of the fully-qualified
  65. // class name to find a mapped file name
  66. while (false !== $pos = strrpos($prefix, '\\')) {
  67. // retain the trailing namespace separator in the prefix
  68. $prefix = substr($class, 0, $pos + 1);
  69. // the rest is the relative class name
  70. $relative_class = substr($class, $pos + 1);
  71. // try to load a mapped file for the prefix and relative class
  72. $mapped_file = $this->loadMappedFile($prefix, $relative_class);
  73. if ($mapped_file) {
  74. return $mapped_file;
  75. }
  76. // remove the trailing namespace separator for the next iteration
  77. // of strrpos()
  78. $prefix = rtrim($prefix, '\\');
  79. }
  80. // never found a mapped file
  81. return false;
  82. }
  83. /**
  84. * Load the mapped file for a namespace prefix and relative class.
  85. *
  86. * @param string $prefix The namespace prefix.
  87. * @param string $relative_class The relative class name.
  88. * @return mixed Boolean false if no mapped file can be loaded, or the
  89. * name of the mapped file that was loaded.
  90. */
  91. protected function loadMappedFile($prefix, $relative_class)
  92. {
  93. // are there any base directories for this namespace prefix?
  94. if (isset($this->prefixes[$prefix]) === false) {
  95. return false;
  96. }
  97. // look through base directories for this namespace prefix
  98. foreach ($this->prefixes[$prefix] as $base_dir) {
  99. // replace the namespace prefix with the base directory,
  100. // replace namespace separators with directory separators
  101. // in the relative class name, append with .php
  102. $file = $base_dir
  103. . str_replace('\\', '/', $relative_class)
  104. . '.php';
  105. // if the mapped file exists, require it
  106. if ($this->requireFile($file)) {
  107. // yes, we're done
  108. return $file;
  109. }
  110. }
  111. // never found it
  112. return false;
  113. }
  114. /**
  115. * If a file exists, require it from the file system.
  116. *
  117. * @param string $file The file to require.
  118. * @return bool True if the file exists, false if not.
  119. */
  120. protected function requireFile($file)
  121. {
  122. if (file_exists($file)) {
  123. require $file;
  124. return true;
  125. }
  126. return false;
  127. }
  128. }
  129. $basePath = realpath(__DIR__ . '/../');
  130. $loader = new Psr4Autoloader();
  131. $loader->register();
  132. // Register laravel-s
  133. $loader->addNamespace('Hhxsv5\LaravelS', $basePath . '/vendor/hhxsv5/laravel-s/src');
  134. // Register laravel-s dependencies
  135. $loader->addNamespace('Symfony\Component\Console', $basePath . '/vendor/symfony/console');
  136. $loader->addNamespace('Symfony\Contracts\Service', $basePath . '/vendor/symfony/service-contracts');
  137. $loader->addNamespace('Symfony\Contracts', $basePath . '/vendor/symfony/contracts');
  138. $command = new Hhxsv5\LaravelS\Console\Portal($basePath);
  139. $input = new Symfony\Component\Console\Input\ArgvInput();
  140. $output = new Symfony\Component\Console\Output\ConsoleOutput();
  141. $code = $command->run($input, $output);
  142. exit($code);