PathRepository.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Repository;
  12. use Composer\Json\JsonFile;
  13. use Composer\Package\Loader\ArrayLoader;
  14. use Symfony\Component\Filesystem\Filesystem;
  15. /**
  16. * This repository allows installing local packages that are not necessarily under their own VCS.
  17. *
  18. * The local packages will be symlinked when possible, else they will be copied.
  19. *
  20. * @code
  21. * "require": {
  22. * "<vendor>/<local-package>": "*"
  23. * },
  24. * "repositories": [
  25. * {
  26. * "type": "path",
  27. * "url": "../../relative/path/to/package/"
  28. * },
  29. * {
  30. * "type": "path",
  31. * "url": "/absolute/path/to/package/"
  32. * }
  33. * ]
  34. * @endcode
  35. *
  36. * @author Samuel Roze <samuel.roze@gmail.com>
  37. * @author Johann Reinke <johann.reinke@gmail.com>
  38. */
  39. class PathRepository extends ArrayRepository
  40. {
  41. /**
  42. * @var Filesystem
  43. */
  44. private $fileSystem;
  45. /**
  46. * @var ArrayLoader
  47. */
  48. private $loader;
  49. /**
  50. * @var string
  51. */
  52. private $path;
  53. /**
  54. * Initializes path repository.
  55. *
  56. * @param array $config package definition
  57. */
  58. public function __construct(array $config)
  59. {
  60. if (!isset($config['url'])) {
  61. throw new \RuntimeException('You must specify the `url` configuration for the path repository');
  62. }
  63. $this->fileSystem = new Filesystem();
  64. $this->loader = new ArrayLoader();
  65. $this->path = realpath(rtrim($config['url'], '/')) . '/';
  66. }
  67. /**
  68. * Initializes path repository.
  69. *
  70. * This method will basically read the folder and add the found package.
  71. *
  72. */
  73. protected function initialize()
  74. {
  75. parent::initialize();
  76. $composerFilePath = $this->path.'composer.json';
  77. if (!$this->fileSystem->exists($composerFilePath)) {
  78. throw new \RuntimeException(sprintf('No `composer.json` file found in path repository "%s"', $this->path));
  79. }
  80. $json = file_get_contents($composerFilePath);
  81. $package = JsonFile::parseJson($json, $composerFilePath);
  82. $package['dist'] = array(
  83. 'type' => 'folder',
  84. 'url' => $this->path,
  85. );
  86. if (!isset($package['version'])) {
  87. $package['version'] = 'dev-master';
  88. }
  89. $package = $this->loader->load($package);
  90. $this->addPackage($package);
  91. }
  92. }