Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

361 linhas
12KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\composer;
  8. use Composer\Package\PackageInterface;
  9. use Composer\Installer\LibraryInstaller;
  10. use Composer\Repository\InstalledRepositoryInterface;
  11. use Composer\Script\CommandEvent;
  12. use Composer\Script\Event;
  13. use Composer\Util\Filesystem;
  14. /**
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @since 2.0
  17. */
  18. class Installer extends LibraryInstaller
  19. {
  20. const EXTRA_BOOTSTRAP = 'bootstrap';
  21. const EXTENSION_FILE = 'yiisoft/extensions.php';
  22. /**
  23. * @inheritdoc
  24. */
  25. public function supports($packageType)
  26. {
  27. return $packageType === 'yii2-extension';
  28. }
  29. /**
  30. * @inheritdoc
  31. */
  32. public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
  33. {
  34. // install the package the normal composer way
  35. parent::install($repo, $package);
  36. // add the package to yiisoft/extensions.php
  37. $this->addPackage($package);
  38. // ensure the yii2-dev package also provides Yii.php in the same place as yii2 does
  39. if ($package->getName() == 'yiisoft/yii2-dev') {
  40. $this->linkBaseYiiFiles();
  41. }
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  47. {
  48. parent::update($repo, $initial, $target);
  49. $this->removePackage($initial);
  50. $this->addPackage($target);
  51. // ensure the yii2-dev package also provides Yii.php in the same place as yii2 does
  52. if ($initial->getName() == 'yiisoft/yii2-dev') {
  53. $this->linkBaseYiiFiles();
  54. }
  55. }
  56. /**
  57. * @inheritdoc
  58. */
  59. public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
  60. {
  61. // uninstall the package the normal composer way
  62. parent::uninstall($repo, $package);
  63. // remove the package from yiisoft/extensions.php
  64. $this->removePackage($package);
  65. // remove links for Yii.php
  66. if ($package->getName() == 'yiisoft/yii2-dev') {
  67. $this->removeBaseYiiFiles();
  68. }
  69. }
  70. protected function addPackage(PackageInterface $package)
  71. {
  72. $extension = [
  73. 'name' => $package->getName(),
  74. 'version' => $package->getVersion(),
  75. ];
  76. $alias = $this->generateDefaultAlias($package);
  77. if (!empty($alias)) {
  78. $extension['alias'] = $alias;
  79. }
  80. $extra = $package->getExtra();
  81. if (isset($extra[self::EXTRA_BOOTSTRAP])) {
  82. $extension['bootstrap'] = $extra[self::EXTRA_BOOTSTRAP];
  83. }
  84. $extensions = $this->loadExtensions();
  85. $extensions[$package->getName()] = $extension;
  86. $this->saveExtensions($extensions);
  87. }
  88. protected function generateDefaultAlias(PackageInterface $package)
  89. {
  90. $fs = new Filesystem;
  91. $vendorDir = $fs->normalizePath($this->vendorDir);
  92. $autoload = $package->getAutoload();
  93. $aliases = [];
  94. if (!empty($autoload['psr-0'])) {
  95. foreach ($autoload['psr-0'] as $name => $path) {
  96. $name = str_replace('\\', '/', trim($name, '\\'));
  97. if (!$fs->isAbsolutePath($path)) {
  98. $path = $this->vendorDir . '/' . $package->getPrettyName() . '/' . $path;
  99. }
  100. $path = $fs->normalizePath($path);
  101. if (strpos($path . '/', $vendorDir . '/') === 0) {
  102. $aliases["@$name"] = '<vendor-dir>' . substr($path, strlen($vendorDir)) . '/' . $name;
  103. } else {
  104. $aliases["@$name"] = $path . '/' . $name;
  105. }
  106. }
  107. }
  108. if (!empty($autoload['psr-4'])) {
  109. foreach ($autoload['psr-4'] as $name => $path) {
  110. if (is_array($path)) {
  111. // ignore psr-4 autoload specifications with multiple search paths
  112. // we can not convert them into aliases as they are ambiguous
  113. continue;
  114. }
  115. $name = str_replace('\\', '/', trim($name, '\\'));
  116. if (!$fs->isAbsolutePath($path)) {
  117. $path = $this->vendorDir . '/' . $package->getPrettyName() . '/' . $path;
  118. }
  119. $path = $fs->normalizePath($path);
  120. if (strpos($path . '/', $vendorDir . '/') === 0) {
  121. $aliases["@$name"] = '<vendor-dir>' . substr($path, strlen($vendorDir));
  122. } else {
  123. $aliases["@$name"] = $path;
  124. }
  125. }
  126. }
  127. return $aliases;
  128. }
  129. protected function removePackage(PackageInterface $package)
  130. {
  131. $packages = $this->loadExtensions();
  132. unset($packages[$package->getName()]);
  133. $this->saveExtensions($packages);
  134. }
  135. protected function loadExtensions()
  136. {
  137. $file = $this->vendorDir . '/' . static::EXTENSION_FILE;
  138. if (!is_file($file)) {
  139. return [];
  140. }
  141. // invalidate opcache of extensions.php if exists
  142. if (function_exists('opcache_invalidate')) {
  143. opcache_invalidate($file, true);
  144. }
  145. $extensions = require($file);
  146. $vendorDir = str_replace('\\', '/', $this->vendorDir);
  147. $n = strlen($vendorDir);
  148. foreach ($extensions as &$extension) {
  149. if (isset($extension['alias'])) {
  150. foreach ($extension['alias'] as $alias => $path) {
  151. $path = str_replace('\\', '/', $path);
  152. if (strpos($path . '/', $vendorDir . '/') === 0) {
  153. $extension['alias'][$alias] = '<vendor-dir>' . substr($path, $n);
  154. }
  155. }
  156. }
  157. }
  158. return $extensions;
  159. }
  160. protected function saveExtensions(array $extensions)
  161. {
  162. $file = $this->vendorDir . '/' . static::EXTENSION_FILE;
  163. if (!file_exists(dirname($file))) {
  164. mkdir(dirname($file), 0777, true);
  165. }
  166. $array = str_replace("'<vendor-dir>", '$vendorDir . \'', var_export($extensions, true));
  167. file_put_contents($file, "<?php\n\n\$vendorDir = dirname(__DIR__);\n\nreturn $array;\n");
  168. // invalidate opcache of extensions.php if exists
  169. if (function_exists('opcache_invalidate')) {
  170. opcache_invalidate($file, true);
  171. }
  172. }
  173. protected function linkBaseYiiFiles()
  174. {
  175. $yiiDir = $this->vendorDir . '/yiisoft/yii2';
  176. if (!file_exists($yiiDir)) {
  177. mkdir($yiiDir, 0777, true);
  178. }
  179. foreach (['Yii.php', 'BaseYii.php', 'classes.php'] as $file) {
  180. file_put_contents($yiiDir . '/' . $file, <<<EOF
  181. <?php
  182. /**
  183. * This is a link provided by the yiisoft/yii2-dev package via yii2-composer plugin.
  184. *
  185. * @link http://www.yiiframework.com/
  186. * @copyright Copyright (c) 2008 Yii Software LLC
  187. * @license http://www.yiiframework.com/license/
  188. */
  189. return require(__DIR__ . '/../yii2-dev/framework/$file');
  190. EOF
  191. );
  192. }
  193. }
  194. protected function removeBaseYiiFiles()
  195. {
  196. $yiiDir = $this->vendorDir . '/yiisoft/yii2';
  197. foreach (['Yii.php', 'BaseYii.php', 'classes.php'] as $file) {
  198. if (file_exists($yiiDir . '/' . $file)) {
  199. unlink($yiiDir . '/' . $file);
  200. }
  201. }
  202. if (file_exists($yiiDir)) {
  203. rmdir($yiiDir);
  204. }
  205. }
  206. /**
  207. * Special method to run tasks defined in `[extra][yii\composer\Installer::postCreateProject]` key in `composer.json`
  208. *
  209. * @param Event $event
  210. */
  211. public static function postCreateProject($event)
  212. {
  213. static::runCommands($event, __METHOD__);
  214. }
  215. /**
  216. * Special method to run tasks defined in `[extra][yii\composer\Installer::postInstall]` key in `composer.json`
  217. *
  218. * @param Event $event
  219. * @since 2.0.5
  220. */
  221. public static function postInstall($event)
  222. {
  223. static::runCommands($event, __METHOD__);
  224. }
  225. /**
  226. * Special method to run tasks defined in `[extra][$extraKey]` key in `composer.json`
  227. *
  228. * @param Event $event
  229. * @param string $extraKey
  230. * @since 2.0.5
  231. */
  232. protected static function runCommands($event, $extraKey)
  233. {
  234. $params = $event->getComposer()->getPackage()->getExtra();
  235. if (isset($params[$extraKey]) && is_array($params[$extraKey])) {
  236. foreach ($params[$extraKey] as $method => $args) {
  237. call_user_func_array([__CLASS__, $method], (array) $args);
  238. }
  239. }
  240. }
  241. /**
  242. * Sets the correct permission for the files and directories listed in the extra section.
  243. * @param array $paths the paths (keys) and the corresponding permission octal strings (values)
  244. */
  245. public static function setPermission(array $paths)
  246. {
  247. foreach ($paths as $path => $permission) {
  248. echo "chmod('$path', $permission)...";
  249. if (is_dir($path) || is_file($path)) {
  250. try {
  251. if (chmod($path, octdec($permission))) {
  252. echo "done.\n";
  253. };
  254. } catch (\Exception $e) {
  255. echo $e->getMessage() . "\n";
  256. }
  257. } else {
  258. echo "file not found.\n";
  259. }
  260. }
  261. }
  262. /**
  263. * Generates a cookie validation key for every app config listed in "config" in extra section.
  264. * You can provide one or multiple parameters as the configuration files which need to have validation key inserted.
  265. */
  266. public static function generateCookieValidationKey()
  267. {
  268. $configs = func_get_args();
  269. $key = self::generateRandomString();
  270. foreach ($configs as $config) {
  271. if (is_file($config)) {
  272. $content = preg_replace('/(("|\')cookieValidationKey("|\')\s*=>\s*)(""|\'\')/', "\\1'$key'", file_get_contents($config), -1, $count);
  273. if ($count > 0) {
  274. file_put_contents($config, $content);
  275. }
  276. }
  277. }
  278. }
  279. protected static function generateRandomString()
  280. {
  281. if (!extension_loaded('openssl')) {
  282. throw new \Exception('The OpenSSL PHP extension is required by Yii2.');
  283. }
  284. $length = 32;
  285. $bytes = openssl_random_pseudo_bytes($length);
  286. return strtr(substr(base64_encode($bytes), 0, $length), '+/=', '_-.');
  287. }
  288. /**
  289. * Copy files to specified locations.
  290. * @param array $paths The source files paths (keys) and the corresponding target locations
  291. * for copied files (values). Location can be specified as an array - first element is target
  292. * location, second defines whether file can be overwritten (by default method don't overwrite
  293. * existing files).
  294. * @since 2.0.5
  295. */
  296. public static function copyFiles(array $paths)
  297. {
  298. foreach ($paths as $source => $target) {
  299. // handle file target as array [path, overwrite]
  300. $target = (array) $target;
  301. echo "Copying file $source to $target[0] - ";
  302. if (!is_file($source)) {
  303. echo "source file not found.\n";
  304. continue;
  305. }
  306. if (is_file($target[0]) && empty($target[1])) {
  307. echo "target file exists - skip.\n";
  308. continue;
  309. } elseif (is_file($target[0]) && !empty($target[1])) {
  310. echo "target file exists - overwrite - ";
  311. }
  312. try {
  313. if (!is_dir(dirname($target[0]))) {
  314. mkdir(dirname($target[0]), 0777, true);
  315. }
  316. if (copy($source, $target[0])) {
  317. echo "done.\n";
  318. }
  319. } catch (\Exception $e) {
  320. echo $e->getMessage() . "\n";
  321. }
  322. }
  323. }
  324. }