Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 3a30d27

Browse files
authored
fixes #32 (#39)
| Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #32 | License | MIT | Doc PR | -
1 parent d890a41 commit 3a30d27

12 files changed

+263
-103
lines changed

src/Automatic/Automatic.php

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Composer\IO\IOInterface;
2222
use Composer\IO\NullIO;
2323
use Composer\Json\JsonFile;
24-
use Composer\Package\Comparer\Comparer;
2524
use Composer\Package\Locker;
2625
use Composer\Plugin\PluginEvents;
2726
use Composer\Plugin\PluginInterface;

src/Automatic/Container.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ public function __construct(Composer $composer, IOInterface $io)
106106
);
107107
},
108108
OperationsResolver::class => static function (Container $container) {
109-
return new OperationsResolver($container->get(Lock::class));
109+
return new OperationsResolver(
110+
$container->get(Lock::class),
111+
$container->get('vendor-dir')
112+
);
110113
},
111114
InstallationManager::class => static function (Container $container) {
112115
return new InstallationManager(

src/Automatic/Installer/AbstractInstaller.php

+3-22
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,7 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $
109109

110110
$this->removeFromLock($package, static::LOCK_KEY);
111111

112-
$lockClassmap = (array) $this->lock->get(Automatic::LOCK_CLASSMAP);
113-
$name = $package->getName();
114-
115-
if (isset($lockClassmap[$name])) {
116-
unset($lockClassmap[$name]);
117-
}
118-
119-
$this->lock->add(Automatic::LOCK_CLASSMAP, $lockClassmap);
112+
$this->lock->remove(Automatic::LOCK_CLASSMAP, $package->getName());
120113
}
121114

122115
/**
@@ -166,13 +159,7 @@ protected function saveToLockFile(array $autoload, PackageInterface $package, st
166159
return false;
167160
}
168161

169-
$this->lock->add(
170-
$key,
171-
\array_merge(
172-
(array) $this->lock->get($key),
173-
[$package->getName() => $classes]
174-
)
175-
);
162+
$this->lock->addSub($key, $package->getName(), $classes);
176163

177164
return true;
178165
}
@@ -200,12 +187,6 @@ protected function addToClassMap(PackageInterface $package): void
200187
return \str_replace($this->vendorDir, '%vendor_path%', $value);
201188
}, $this->loader->getAll());
202189

203-
$this->lock->add(
204-
Automatic::LOCK_CLASSMAP,
205-
\array_merge(
206-
(array) $this->lock->get(Automatic::LOCK_CLASSMAP),
207-
[$package->getName() => $classMap]
208-
)
209-
);
190+
$this->lock->addSub(Automatic::LOCK_CLASSMAP, $package->getName(), $classMap);
210191
}
211192
}

src/Automatic/Installer/ConfiguratorInstaller.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ final class ConfiguratorInstaller extends AbstractInstaller
2121
*/
2222
protected function removeFromLock(PackageInterface $package, string $key): void
2323
{
24-
$lockKeyArray = (array) $this->lock->get($key);
25-
$name = $package->getName();
26-
27-
if (isset($lockKeyArray[$name])) {
28-
unset($lockKeyArray[$name]);
29-
}
30-
31-
$this->lock->add($key, $lockKeyArray);
24+
$this->lock->remove($key, $package->getName());
3225
}
3326
}

src/Automatic/Lock.php

+58-11
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,95 @@ public function __construct(string $lockFile)
3737
/**
3838
* Check if key exists in lock file.
3939
*
40-
* @param string $name
40+
* @param string $mainKey
41+
* @param null|string $name
4142
*
4243
* @return bool
4344
*/
44-
public function has(string $name): bool
45+
public function has(string $mainKey, ?string $name = null): bool
4546
{
46-
return \array_key_exists($name, $this->lock);
47+
$mainCheck = \array_key_exists($mainKey, $this->lock);
48+
49+
if ($name === null) {
50+
return $mainCheck;
51+
}
52+
53+
if ($mainCheck === true && \is_array($this->lock[$mainKey])) {
54+
return \array_key_exists($name, $this->lock[$mainKey]);
55+
}
56+
57+
return false;
4758
}
4859

4960
/**
5061
* Add a value to the lock file.
5162
*
63+
* @param string $mainKey
64+
* @param null|array|string $data
65+
*
66+
* @return void
67+
*/
68+
public function add(string $mainKey, $data): void
69+
{
70+
$this->lock[$mainKey] = $data;
71+
}
72+
73+
/**
74+
* Add sub value to the lock file.
75+
*
76+
* @param string $mainKey
5277
* @param string $name
5378
* @param null|array|string $data
5479
*
5580
* @return void
5681
*/
57-
public function add(string $name, $data): void
82+
public function addSub(string $mainKey, string $name, $data): void
5883
{
59-
$this->lock[$name] = $data;
84+
if (! \array_key_exists($mainKey, $this->lock)) {
85+
$this->lock[$mainKey] = [];
86+
}
87+
88+
$this->lock[$mainKey][$name] = $data;
6089
}
6190

6291
/**
6392
* Get package data found in the lock file.
6493
*
65-
* @param string $name
94+
* @param string $mainKey
95+
* @param null|string $name
6696
*
6797
* @return null|array|string
6898
*/
69-
public function get(string $name)
99+
public function get(string $mainKey, ?string $name = null)
70100
{
71-
return $this->lock[$name] ?? null;
101+
if (\array_key_exists($mainKey, $this->lock)) {
102+
if ($name === null) {
103+
return $this->lock[$mainKey];
104+
}
105+
106+
if (\is_array($this->lock[$mainKey]) && \array_key_exists($name, $this->lock[$mainKey])) {
107+
return $this->lock[$mainKey][$name];
108+
}
109+
}
110+
111+
return null;
72112
}
73113

74114
/**
75115
* Remove a package from lock file.
76116
*
77-
* @param string $name
117+
* @param string $mainKey
118+
* @param null|string $name
78119
*/
79-
public function remove(string $name): void
120+
public function remove(string $mainKey, ?string $name = null): void
80121
{
81-
unset($this->lock[$name]);
122+
if ($name === null) {
123+
unset($this->lock[$mainKey]);
124+
}
125+
126+
if (\array_key_exists($mainKey, $this->lock)) {
127+
unset($this->lock[$mainKey][$name]);
128+
}
82129
}
83130

84131
/**

src/Automatic/OperationsResolver.php

+21-29
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use Composer\DependencyResolver\Operation\UninstallOperation;
66
use Composer\DependencyResolver\Operation\UpdateOperation;
7+
use Composer\Json\JsonFile;
78
use Composer\Package\PackageInterface;
89
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
910
use Narrowspark\Automatic\Common\Package;
1011

11-
class OperationsResolver
12+
final class OperationsResolver
1213
{
1314
/**
1415
* A lock instance.
@@ -18,33 +19,22 @@ class OperationsResolver
1819
private $lock;
1920

2021
/**
21-
* Name of the parent package.
22+
* The composer vendor dir.
2223
*
2324
* @var string
2425
*/
25-
private $parentName;
26+
private $vendorDir;
2627

2728
/**
2829
* Create a new OperationsResolver instance.
2930
*
3031
* @param \Narrowspark\Automatic\Lock $lock
32+
* @param string $vendorDir
3133
*/
32-
public function __construct(Lock $lock)
34+
public function __construct(Lock $lock, string $vendorDir)
3335
{
34-
$this->lock = $lock;
35-
}
36-
37-
/**
38-
* Set the parent package name.
39-
* This is used for the "extraDependencyOf" key.
40-
*
41-
* @param string $name
42-
*
43-
* @return void
44-
*/
45-
public function setParentPackageName(string $name): void
46-
{
47-
$this->parentName = $name;
36+
$this->lock = $lock;
37+
$this->vendorDir = $vendorDir;
4838
}
4939

5040
/**
@@ -72,16 +62,17 @@ public function resolve(array $operations): array
7262
$composerPackage = $operation->getPackage();
7363
}
7464

75-
if (! isset($composerPackage->getExtra()['automatic'])) {
65+
$name = $composerPackage->getName();
66+
$automaticFile = $this->vendorDir . \DIRECTORY_SEPARATOR . $name . \DIRECTORY_SEPARATOR . 'automatic.json';
67+
68+
if (! \file_exists($automaticFile) && ! isset($composerPackage->getExtra()['automatic'])) {
7669
continue;
7770
}
7871

79-
$name = $composerPackage->getName();
80-
81-
if ($operation instanceof UninstallOperation && $this->lock->has($name)) {
82-
$package = Package::createFromLock($name, (array) $this->lock->get($name));
72+
if ($operation instanceof UninstallOperation && $this->lock->has(Automatic::LOCK_PACKAGES, $name)) {
73+
$package = Package::createFromLock($name, (array) $this->lock->get(Automatic::LOCK_PACKAGES, $name));
8374
} else {
84-
$package = $this->createAutomaticPackage($composerPackage);
75+
$package = $this->createAutomaticPackage($composerPackage, $automaticFile);
8576
}
8677

8778
$package->setOperation($o);
@@ -122,10 +113,11 @@ private function getPackageVersion(PackageInterface $package): string
122113
* Create a automatic package with the composer package data.
123114
*
124115
* @param \Composer\Package\PackageInterface $composerPackage
116+
* @param string $automaticFile
125117
*
126118
* @return \Narrowspark\Automatic\Common\Contract\Package
127119
*/
128-
private function createAutomaticPackage(PackageInterface $composerPackage): PackageContract
120+
private function createAutomaticPackage(PackageInterface $composerPackage, string $automaticFile): PackageContract
129121
{
130122
$package = new Package($composerPackage->getName(), $this->getPackageVersion($composerPackage));
131123
$requires = [];
@@ -152,12 +144,12 @@ private function createAutomaticPackage(PackageInterface $composerPackage): Pack
152144
$package->setUrl($url);
153145
}
154146

155-
if ($this->parentName !== null) {
156-
$package->setParentName($this->parentName);
147+
if (\file_exists($automaticFile)) {
148+
$package->setConfig(JsonFile::parseJson((string) \file_get_contents($automaticFile)));
149+
} else {
150+
$package->setConfig($composerPackage->getExtra()['automatic']);
157151
}
158152

159-
$package->setConfig($composerPackage->getExtra()['automatic']);
160-
161153
return $package;
162154
}
163155
}

tests/Automatic/ConfiguratorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function allowMockingNonExistentMethods($allow = false): void
9797
*/
9898
protected function arrangeCopyPackage(): Package
9999
{
100-
$package = new Package('Fixture/stub', '1.0');
100+
$package = new Package('Fixture/copy', '1.0');
101101
$package->setConfig([
102102
'copy' => [
103103
'copy.txt' => $this->copyFileName,
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"providers": {
3+
"Viserio\\Component\\Console\\Provider\\ConsoleServiceProvider": [
4+
"global"
5+
]
6+
}
7+
}

tests/Automatic/Installer/AbstractInstallerTest.php

+12-23
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ protected function setUp(): void
9595
->once()
9696
->andReturn($this->downloadManagerMock);
9797

98-
$this->lockMock->shouldReceive('has')
99-
->with($this->installerClass::LOCK_KEY)
100-
->andReturn(true);
101-
$this->lockMock->shouldReceive('add')
102-
->with($this->installerClass::LOCK_KEY, []);
103-
10498
$this->configuratorInstaller = new $this->installerClass($this->ioMock, $this->composerMock, $this->lockMock, new PathClassLoader());
10599

106100
$this->repositoryMock = $this->mock(InstalledRepositoryInterface::class);
@@ -154,7 +148,10 @@ public function testInstall(): void
154148

155149
$this->downloadManagerMock->shouldReceive('download');
156150

157-
$this->arrangeAddToLock();
151+
$this->lockMock->shouldReceive('addSub')
152+
->with($this->installerClass::LOCK_KEY, $name, \Mockery::type('array'));
153+
$this->lockMock->shouldReceive('addSub')
154+
->with(Automatic::LOCK_CLASSMAP, $name, \Mockery::type('array'));
158155

159156
$this->configuratorInstaller->install($this->repositoryMock, $this->packageMock);
160157
}
@@ -191,7 +188,10 @@ public function testUpdate(): void
191188
$this->targetPackageMock->shouldReceive('getAutoload')
192189
->andReturn(['psr-4' => ['Test\\' => '']]);
193190

194-
$this->arrangeAddToLock();
191+
$this->lockMock->shouldReceive('addSub')
192+
->with($this->installerClass::LOCK_KEY, $name, \Mockery::type('array'));
193+
$this->lockMock->shouldReceive('addSub')
194+
->with(Automatic::LOCK_CLASSMAP, $name, \Mockery::type('array'));
195195

196196
$this->configuratorInstaller->update($this->repositoryMock, $this->packageMock, $this->targetPackageMock);
197197
}
@@ -231,7 +231,10 @@ public function testInstallWithNotFoundClasses(): void
231231
$this->repositoryMock->shouldReceive('removePackage')
232232
->once();
233233

234-
$this->arrangeAddToLock();
234+
$this->lockMock->shouldReceive('remove')
235+
->with($this->installerClass::LOCK_KEY, $name);
236+
$this->lockMock->shouldReceive('remove')
237+
->with(Automatic::LOCK_CLASSMAP, $name);
235238

236239
$this->configuratorInstaller->install($this->repositoryMock, $this->packageMock);
237240
}
@@ -243,18 +246,4 @@ protected function allowMockingNonExistentMethods($allow = false): void
243246
{
244247
parent::allowMockingNonExistentMethods(true);
245248
}
246-
247-
private function arrangeAddToLock(): void
248-
{
249-
$this->lockMock->shouldReceive('get')
250-
->with($this->installerClass::LOCK_KEY)
251-
->andReturn([]);
252-
$this->lockMock->shouldReceive('get')
253-
->with(Automatic::LOCK_CLASSMAP)
254-
->andReturn([]);
255-
$this->lockMock->shouldReceive('add')
256-
->with($this->installerClass::LOCK_KEY, \Mockery::type('array'));
257-
$this->lockMock->shouldReceive('add')
258-
->with(Automatic::LOCK_CLASSMAP, \Mockery::type('array'));
259-
}
260249
}

0 commit comments

Comments
 (0)