BitrixInstaller.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Composer\Installers;
  3. use Composer\Util\Filesystem;
  4. /**
  5. * Installer for Bitrix Framework. Supported types of extensions:
  6. * - `bitrix-d7-module` — copy the module to directory `bitrix/modules/<vendor>.<name>`.
  7. * - `bitrix-d7-component` — copy the component to directory `bitrix/components/<vendor>/<name>`.
  8. * - `bitrix-d7-template` — copy the template to directory `bitrix/templates/<vendor>_<name>`.
  9. *
  10. * You can set custom path to directory with Bitrix kernel in `composer.json`:
  11. *
  12. * ```json
  13. * {
  14. * "extra": {
  15. * "bitrix-dir": "s1/bitrix"
  16. * }
  17. * }
  18. * ```
  19. *
  20. * @author Nik Samokhvalov <nik@samokhvalov.info>
  21. * @author Denis Kulichkin <onexhovia@gmail.com>
  22. */
  23. class BitrixInstaller extends BaseInstaller
  24. {
  25. protected $locations = array(
  26. 'module' => '{$bitrix_dir}/modules/{$name}/', // deprecated, remove on the major release (Backward compatibility will be broken)
  27. 'component' => '{$bitrix_dir}/components/{$name}/', // deprecated, remove on the major release (Backward compatibility will be broken)
  28. 'theme' => '{$bitrix_dir}/templates/{$name}/', // deprecated, remove on the major release (Backward compatibility will be broken)
  29. 'd7-module' => '{$bitrix_dir}/modules/{$vendor}.{$name}/',
  30. 'd7-component' => '{$bitrix_dir}/components/{$vendor}/{$name}/',
  31. 'd7-template' => '{$bitrix_dir}/templates/{$vendor}_{$name}/',
  32. );
  33. /**
  34. * @var array Storage for informations about duplicates at all the time of installation packages.
  35. */
  36. private static $checkedDuplicates = array();
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function inflectPackageVars($vars)
  41. {
  42. if ($this->composer->getPackage()) {
  43. $extra = $this->composer->getPackage()->getExtra();
  44. if (isset($extra['bitrix-dir'])) {
  45. $vars['bitrix_dir'] = $extra['bitrix-dir'];
  46. }
  47. }
  48. if (!isset($vars['bitrix_dir'])) {
  49. $vars['bitrix_dir'] = 'bitrix';
  50. }
  51. return parent::inflectPackageVars($vars);
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function templatePath($path, array $vars = array())
  57. {
  58. $templatePath = parent::templatePath($path, $vars);
  59. $this->checkDuplicates($templatePath, $vars);
  60. return $templatePath;
  61. }
  62. /**
  63. * Duplicates search packages.
  64. *
  65. * @param string $path
  66. * @param array $vars
  67. */
  68. protected function checkDuplicates($path, array $vars = array())
  69. {
  70. $packageType = substr($vars['type'], strlen('bitrix') + 1);
  71. $localDir = explode('/', $vars['bitrix_dir']);
  72. array_pop($localDir);
  73. $localDir[] = 'local';
  74. $localDir = implode('/', $localDir);
  75. $oldPath = str_replace(
  76. array('{$bitrix_dir}', '{$name}'),
  77. array($localDir, $vars['name']),
  78. $this->locations[$packageType]
  79. );
  80. if (in_array($oldPath, static::$checkedDuplicates)) {
  81. return;
  82. }
  83. if ($oldPath !== $path && file_exists($oldPath) && $this->io && $this->io->isInteractive()) {
  84. $this->io->writeError(' <error>Duplication of packages:</error>');
  85. $this->io->writeError(' <info>Package ' . $oldPath . ' will be called instead package ' . $path . '</info>');
  86. while (true) {
  87. switch ($this->io->ask(' <info>Delete ' . $oldPath . ' [y,n,?]?</info> ', '?')) {
  88. case 'y':
  89. $fs = new Filesystem();
  90. $fs->removeDirectory($oldPath);
  91. break 2;
  92. case 'n':
  93. break 2;
  94. case '?':
  95. default:
  96. $this->io->writeError(array(
  97. ' y - delete package ' . $oldPath . ' and to continue with the installation',
  98. ' n - don\'t delete and to continue with the installation',
  99. ));
  100. $this->io->writeError(' ? - print help');
  101. break;
  102. }
  103. }
  104. }
  105. static::$checkedDuplicates[] = $oldPath;
  106. }
  107. }