sfneal /
dependencies
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Sfneal\Dependencies\Services; |
||||
| 4 | |||||
| 5 | use Illuminate\Support\Collection; |
||||
| 6 | use Illuminate\Support\Facades\Cache; |
||||
| 7 | use Sfneal\Caching\Traits\IsCacheable; |
||||
| 8 | use Sfneal\Dependencies\Utils\ComposerRequirements; |
||||
| 9 | use Sfneal\Helpers\Laravel\LaravelHelpers; |
||||
| 10 | |||||
| 11 | class DependenciesRepository |
||||
| 12 | { |
||||
| 13 | use IsCacheable; |
||||
| 14 | |||||
| 15 | /** |
||||
| 16 | * @var array Array of composer or Docker dependencies |
||||
| 17 | */ |
||||
| 18 | private array $dependencies; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * @var bool Use composer.json dependencies as source |
||||
| 22 | */ |
||||
| 23 | private bool $composerDependencies; |
||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * @var bool Include composer dev dependencies |
||||
| 27 | */ |
||||
| 28 | private bool $devComposerDependencies; |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * @var Collection|null Collection of dependencies retrieved by the `getDependencies()` method |
||||
| 32 | */ |
||||
| 33 | private ?Collection $dependenciesCollection = null; |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * @var array|null Array of global Img Shields params to be passed to SVG requests |
||||
| 37 | */ |
||||
| 38 | private ?array $imgShieldGlobalParams = null; |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * Include Img Shields global params in SVG requests. |
||||
| 42 | * |
||||
| 43 | * @param array|null $imgShieldGlobalParams |
||||
| 44 | * @return $this |
||||
| 45 | */ |
||||
| 46 | public function withImgShieldGlobalParams(?array $imgShieldGlobalParams = null): self |
||||
| 47 | { |
||||
| 48 | $this->imgShieldGlobalParams = $imgShieldGlobalParams; |
||||
| 49 | |||||
| 50 | return $this; |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | /** |
||||
| 54 | * Retrieve dependencies from the composer.json file & optionally include 'dev' dependencies. |
||||
| 55 | * |
||||
| 56 | * @param bool $devComposerDependencies |
||||
| 57 | * @return $this |
||||
| 58 | */ |
||||
| 59 | public function fromComposer(bool $devComposerDependencies = false): self |
||||
| 60 | { |
||||
| 61 | $this->composerDependencies = true; |
||||
| 62 | $this->devComposerDependencies = $devComposerDependencies; |
||||
| 63 | |||||
| 64 | return $this; |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | /** |
||||
| 68 | * Retrieve dependencies from the config file. |
||||
| 69 | * |
||||
| 70 | * @return $this |
||||
| 71 | */ |
||||
| 72 | public function fromConfig(): self |
||||
| 73 | { |
||||
| 74 | $this->composerDependencies = false; |
||||
| 75 | $this->dependencies = config('dependencies.dependencies'); |
||||
| 76 | |||||
| 77 | return $this; |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | /** |
||||
| 81 | * Retrieve dependencies from an array. |
||||
| 82 | * |
||||
| 83 | * @param array $dependencies |
||||
| 84 | * @return $this |
||||
| 85 | */ |
||||
| 86 | public function fromArray(array $dependencies): self |
||||
| 87 | { |
||||
| 88 | $this->composerDependencies = false; |
||||
| 89 | $this->dependencies = $dependencies; |
||||
| 90 | |||||
| 91 | return $this; |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | /** |
||||
| 95 | * Retrieve a Collection of Dependencies with GitHub, Packagist version & Travis CI build status URLs. |
||||
| 96 | * |
||||
| 97 | * @return Collection |
||||
| 98 | */ |
||||
| 99 | public function get(): Collection |
||||
| 100 | { |
||||
| 101 | return Cache::remember( |
||||
| 102 | $this->cacheKey(), |
||||
| 103 | config('dependencies.cache.ttl'), |
||||
| 104 | function () { |
||||
| 105 | $array = []; |
||||
| 106 | |||||
| 107 | foreach ($this->getDependencies()->toArray() as $type => $dependencies) { |
||||
| 108 | foreach ($dependencies as $dependency) { |
||||
| 109 | $array[] = new DependencyService($dependency, $type, $this->imgShieldGlobalParams); |
||||
| 110 | } |
||||
| 111 | } |
||||
| 112 | |||||
| 113 | return collect($array); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 114 | } |
||||
| 115 | ); |
||||
| 116 | } |
||||
| 117 | |||||
| 118 | /** |
||||
| 119 | * Retrieve a list of dependencies from the config file or by reading the composer.json 'requires' section. |
||||
| 120 | * |
||||
| 121 | * @return Collection |
||||
| 122 | */ |
||||
| 123 | private function getDependencies(): Collection |
||||
| 124 | { |
||||
| 125 | if (is_null($this->dependenciesCollection)) { |
||||
| 126 | if ($this->composerDependencies) { |
||||
| 127 | $this->dependenciesCollection = $this->getComposerRequirements(); |
||||
| 128 | } else { |
||||
| 129 | $this->dependenciesCollection = $this->getArrayDependencies() ?? $this->getComposerRequirements(); |
||||
| 130 | } |
||||
| 131 | } |
||||
| 132 | |||||
| 133 | return $this->dependenciesCollection; |
||||
| 134 | } |
||||
| 135 | |||||
| 136 | /** |
||||
| 137 | * Retrieve a list of dependencies set in the 'dependencies' config. |
||||
| 138 | * |
||||
| 139 | * @return Collection |
||||
| 140 | */ |
||||
| 141 | private function getArrayDependencies(): Collection |
||||
| 142 | { |
||||
| 143 | return collect($this->dependencies); |
||||
|
0 ignored issues
–
show
$this->dependencies of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 144 | } |
||||
| 145 | |||||
| 146 | /** |
||||
| 147 | * Retrieve an array of composer packages that are required by the composer.json. |
||||
| 148 | * |
||||
| 149 | * @return Collection |
||||
| 150 | */ |
||||
| 151 | private function getComposerRequirements(): Collection |
||||
| 152 | { |
||||
| 153 | return collect([ |
||||
|
0 ignored issues
–
show
array('composer' => new ...ies)->get()->toArray()) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 154 | 'composer' => (new ComposerRequirements($this->devComposerDependencies))->get()->toArray(), |
||||
| 155 | ]); |
||||
| 156 | } |
||||
| 157 | |||||
| 158 | /** |
||||
| 159 | * Retrieve the cache key. |
||||
| 160 | * |
||||
| 161 | * @return string |
||||
| 162 | */ |
||||
| 163 | public function cacheKey(): string |
||||
| 164 | { |
||||
| 165 | return config('dependencies.cache.prefix').':'.LaravelHelpers::serializeHash($this->getDependencies()->toArray()); |
||||
| 166 | } |
||||
| 167 | } |
||||
| 168 |