Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

110 lines
3.1KB

  1. <?php
  2. /*
  3. * This file is part of the Fxp Composer Asset Plugin package.
  4. *
  5. * (c) François Pluchino <francois.pluchino@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Fxp\Composer\AssetPlugin\Repository\Vcs;
  11. use Composer\Cache;
  12. use Composer\Json\JsonFile;
  13. use Composer\Repository\Vcs\VcsDriverInterface;
  14. /**
  15. * Helper for VCS driver.
  16. *
  17. * @author François Pluchino <francois.pluchino@gmail.com>
  18. */
  19. class Util
  20. {
  21. /**
  22. * Check if the identifier is an SHA.
  23. *
  24. * @param string $identifier The identifier
  25. *
  26. * @return bool
  27. */
  28. public static function isSha($identifier)
  29. {
  30. return (bool) preg_match('{[a-f0-9]{40}}i', $identifier);
  31. }
  32. /**
  33. * @param array $cacheCode The cache code
  34. * @param Cache $cache The cache filesystem
  35. * @param string $type The asset type
  36. * @param string $identifier The identifier
  37. * @param bool $force Force the read
  38. *
  39. * @return array|null
  40. */
  41. public static function readCache(array $cacheCode, Cache $cache, $type, $identifier, $force = false)
  42. {
  43. if (array_key_exists($identifier, $cacheCode)) {
  44. return $cacheCode[$identifier];
  45. }
  46. $data = null;
  47. if (self::isSha($identifier) || $force) {
  48. $res = $cache->read($type.'-'.$identifier);
  49. if ($res) {
  50. $data = JsonFile::parseJson($res);
  51. }
  52. }
  53. return $data;
  54. }
  55. /**
  56. * @param Cache $cache The cache
  57. * @param string $type The asset type
  58. * @param string $identifier The identifier
  59. * @param array $composer The data composer
  60. * @param bool $force Force the write
  61. */
  62. public static function writeCache(Cache $cache, $type, $identifier, array $composer, $force = false)
  63. {
  64. if (self::isSha($identifier) || $force) {
  65. $cache->write($type.'-'.$identifier, json_encode($composer));
  66. }
  67. }
  68. /**
  69. * Add time in composer.
  70. *
  71. * @param array $composer The composer
  72. * @param string $resourceKey The composer key
  73. * @param string $resource The resource url
  74. * @param VcsDriverInterface $driver The vcs driver
  75. * @param string $method The method for get content
  76. *
  77. * @return array The composer
  78. */
  79. public static function addComposerTime(array $composer, $resourceKey, $resource, VcsDriverInterface $driver, $method = 'getContents')
  80. {
  81. if (!isset($composer['time'])) {
  82. $ref = new \ReflectionClass($driver);
  83. $meth = $ref->getMethod($method);
  84. $meth->setAccessible(true);
  85. $commit = JsonFile::parseJson((string) $meth->invoke($driver, $resource), $resource);
  86. $keys = explode('.', $resourceKey);
  87. while (!empty($keys)) {
  88. $commit = $commit[$keys[0]];
  89. array_shift($keys);
  90. }
  91. $composer['time'] = $commit;
  92. }
  93. return $composer;
  94. }
  95. }