/** * Creates a new package. * * @param PackageFile|null $packageFile The package file or `null` if the * package file could not be loaded. * @param string $installPath The absolute install path. * @param InstallInfo|null $installInfo The install info of this package. * @param Exception[] $loadErrors The errors that happened during * loading of the package, if any. */ public function __construct(PackageFile $packageFile = null, $installPath, InstallInfo $installInfo = null, array $loadErrors = array()) { Assert::absoluteSystemPath($installPath); Assert::true($packageFile || $loadErrors, 'The load errors must be passed if the package file is null.'); Assert::allIsInstanceOf($loadErrors, 'Exception'); // If a package name was set during installation, that name wins over // the predefined name in the puli.json file (if any) $this->name = $installInfo && null !== $installInfo->getPackageName() ? $installInfo->getPackageName() : ($packageFile ? $packageFile->getPackageName() : null); if (null === $this->name) { $this->name = $this->getDefaultName(); } // The path is stored both here and in the install info. While the // install info contains the path as it is stored in the install file // (i.e. relative or absolute), the install path of the package is // always an absolute path. $this->installPath = $installPath; $this->installInfo = $installInfo; $this->packageFile = $packageFile; $this->loadErrors = $loadErrors; if (!file_exists($installPath)) { $this->state = PackageState::NOT_FOUND; } elseif (count($loadErrors) > 0) { $this->state = PackageState::NOT_LOADABLE; } else { $this->state = PackageState::ENABLED; } }
/** * {@inheritdoc} */ public function rollback() { if ($this->wasDisabled) { $this->installInfo->addDisabledBindingUuid($this->uuid); } else { $this->installInfo->removeEnabledBindingUuid($this->uuid); } }
public function testRenameNonRootPackage() { $this->initDefaultManager(); $this->packageFileStorage->expects($this->once())->method('saveRootPackageFile')->with($this->rootPackageFile)->will($this->returnCallback(function (RootPackageFile $rootPackageFile) { PHPUnit_Framework_Assert::assertTrue($rootPackageFile->hasInstallInfo('vendor/new')); PHPUnit_Framework_Assert::assertFalse($rootPackageFile->hasInstallInfo('vendor/package1')); })); $this->installInfo1->addDisabledBindingUuid($uuid = Uuid::uuid4()); $this->assertSame('vendor/package1', $this->installInfo1->getPackageName()); $this->assertTrue($this->manager->hasPackage('vendor/package1')); $this->manager->renamePackage('vendor/package1', 'vendor/new'); $this->assertTrue($this->rootPackageFile->hasInstallInfo('vendor/new')); $this->assertFalse($this->rootPackageFile->hasInstallInfo('vendor/package1')); $this->assertFalse($this->manager->hasPackage('vendor/package1')); $this->assertTrue($this->manager->hasPackage('vendor/new')); $package = $this->manager->getPackage('vendor/new'); $installInfo = $this->rootPackageFile->getInstallInfo('vendor/new'); $this->assertInstanceOf('Puli\\Manager\\Api\\Package\\Package', $package); $this->assertSame('vendor/new', $package->getName()); $this->assertSame($this->packageDir1, $package->getInstallPath()); $this->assertSame($installInfo, $package->getInstallInfo()); $this->assertSame('vendor/new', $installInfo->getPackageName()); $this->assertSame('../package1', $installInfo->getInstallPath()); $this->assertSame(array($uuid), $installInfo->getDisabledBindingUuids()); }
public function testBuildDiscoveryOnlyIncludesEnabledBindingsOfInstalledPackages() { $this->initDefaultManager(); $this->packageFile1->addBindingDescriptor($binding1 = new BindingDescriptor('/path1', 'my/type')); $this->packageFile1->addBindingDescriptor($binding2 = new BindingDescriptor('/path2', 'my/type')); $this->installInfo1->addDisabledBindingUuid($binding2->getUuid()); $this->packageFile1->addTypeDescriptor($bindingType = new BindingTypeDescriptor('my/type')); $this->discovery->expects($this->once())->method('defineType')->with($bindingType->toBindingType()); $this->discovery->expects($this->once())->method('bind')->with('/path1', 'my/type', array(), 'glob'); $this->manager->buildDiscovery(); }
public function testReadFullRootPackageFile() { $packageFile = $this->reader->readRootPackageFile(__DIR__ . '/Fixtures/json/full-root.json', $this->baseConfig); $installInfo1 = new InstallInfo('vendor/package1', '/path/to/package1'); $installInfo1->setInstallerName('composer'); $installInfo1->addEnabledBindingUuid(Uuid::fromString('a54e5668-2b36-43f4-a32c-2d175092b77d')); $installInfo1->addDisabledBindingUuid(Uuid::fromString('4d02ee67-d845-4789-a9c1-8301351c6f5a')); $installInfo2 = new InstallInfo('vendor/package2', '/path/to/package2'); $this->assertInstanceOf('Puli\\Manager\\Api\\Package\\RootPackageFile', $packageFile); $this->assertSame(__DIR__ . '/Fixtures/json/full-root.json', $packageFile->getPath()); $this->assertFullConfig($packageFile); $this->assertSame(array('acme/blog-extension1', 'acme/blog-extension2'), $packageFile->getOverrideOrder()); $this->assertEquals(array($installInfo1, $installInfo2), $packageFile->getInstallInfos()); $config = $packageFile->getConfig(); $this->assertSame('puli-dir', $config->get(Config::PULI_DIR)); $this->assertSame('Puli\\MyFactory', $config->get(Config::FACTORY_OUT_CLASS)); $this->assertSame('puli-dir/MyFactory.php', $config->get(Config::FACTORY_OUT_FILE)); $this->assertSame('my-type', $config->get(Config::REPOSITORY_TYPE)); $this->assertSame('puli-dir/my-repo', $config->get(Config::REPOSITORY_PATH)); $this->assertSame('my-store-type', $config->get(Config::DISCOVERY_STORE_TYPE)); }
/** * Adds install info for an installed package. * * @param InstallInfo $installInfo The install info. */ public function addInstallInfo(InstallInfo $installInfo) { $this->installInfos[$installInfo->getPackageName()] = $installInfo; }
/** * Returns whether the package matches the given expression. * * @param Expression $expr The search criteria. You can use the fields * {@link NAME}, {@link INSTALL_PATH} and * {@link STATE} in the expression. * * @return bool Returns `true` if the package matches the expression and * `false` otherwise. */ public function match(Expression $expr) { return $expr->evaluate(array(self::NAME => $this->name, self::INSTALL_PATH => $this->installPath, self::STATE => $this->state, self::INSTALLER => $this->installInfo ? $this->installInfo->getInstallerName() : null)); }
public function testWriteRootPackageFileSortsPackageBindings() { $installInfo = new InstallInfo('vendor/package1', '/path/to/package1'); $installInfo->addEnabledBindingUuid(Uuid::fromString('c54e5668-2b36-43f4-a32c-2d175092b77d')); $installInfo->addEnabledBindingUuid(Uuid::fromString('a54e5668-2b36-43f4-a32c-2d175092b77d')); $installInfo->addEnabledBindingUuid(Uuid::fromString('b54e5668-2b36-43f4-a32c-2d175092b77d')); $installInfo->addDisabledBindingUuid(Uuid::fromString('6d02ee67-d845-4789-a9c1-8301351c6f5a')); $installInfo->addDisabledBindingUuid(Uuid::fromString('4d02ee67-d845-4789-a9c1-8301351c6f5a')); $installInfo->addDisabledBindingUuid(Uuid::fromString('5d02ee67-d845-4789-a9c1-8301351c6f5a')); $packageFile = new RootPackageFile(); $packageFile->addInstallInfo($installInfo); $this->writer->writePackageFile($packageFile, $this->tempFile); $this->assertFileExists($this->tempFile); $this->assertJsonFileEquals(__DIR__ . '/Fixtures/json/sorted-package-bindings.json', $this->tempFile); }
protected function setUp() { parent::setUp(); $this->environment = $this->getMockBuilder('Puli\\Manager\\Api\\Environment\\ProjectEnvironment')->disableOriginalConstructor()->getMock(); $this->packageManager = $this->getMock('Puli\\Manager\\Api\\Package\\PackageManager'); $this->handler = new PackageCommandHandler($this->packageManager); $this->environment->expects($this->any())->method('getRootDirectory')->willReturn(__DIR__ . '/Fixtures/root'); $this->packageManager->expects($this->any())->method('getEnvironment')->willReturn($this->environment); $installInfo1 = new InstallInfo('vendor/package1', 'packages/package1'); $installInfo2 = new InstallInfo('vendor/package2', 'packages/package2'); $installInfo3 = new InstallInfo('vendor/package3', 'packages/package3'); $installInfo4 = new InstallInfo('vendor/package4', 'packages/package4'); $installInfo1->setInstallerName('spock'); $installInfo2->setInstallerName('spock'); $installInfo3->setInstallerName('kirk'); $installInfo4->setInstallerName('spock'); $rootPackage = new RootPackage(new RootPackageFile('vendor/root'), __DIR__ . '/Fixtures/root'); $package1 = new Package(new PackageFile('vendor/package1'), __DIR__ . '/Fixtures/root/packages/package1', $installInfo1); $package2 = new Package(new PackageFile('vendor/package2'), __DIR__ . '/Fixtures/root/packages/package2', $installInfo2); $package3 = new Package(new PackageFile('vendor/package3'), __DIR__ . '/Fixtures/root/packages/package3', $installInfo3); $package4 = new Package(null, __DIR__ . '/Fixtures/root/packages/package4', $installInfo4, array(new RuntimeException('Load error'))); $this->packageManager->expects($this->any())->method('findPackages')->willReturnCallback($this->returnFromMap(array(array($this->all(), new PackageCollection(array($rootPackage, $package1, $package2, $package3, $package4))), array($this->installer('spock'), new PackageCollection(array($package1, $package2, $package4))), array($this->state(PackageState::ENABLED), new PackageCollection(array($rootPackage, $package1, $package2))), array($this->state(PackageState::NOT_FOUND), new PackageCollection(array($package3))), array($this->state(PackageState::NOT_LOADABLE), new PackageCollection(array($package4))), array($this->states(array(PackageState::ENABLED, PackageState::NOT_FOUND)), new PackageCollection(array($rootPackage, $package1, $package2, $package3))), array($this->installerAndState('spock', PackageState::ENABLED), new PackageCollection(array($package1, $package2))), array($this->installerAndState('spock', PackageState::NOT_FOUND), new PackageCollection(array())), array($this->installerAndState('spock', PackageState::NOT_LOADABLE), new PackageCollection(array($package4)))))); $this->previousWd = getcwd(); $this->wd = __DIR__; chdir($this->wd); }
/** * @expectedException \InvalidArgumentException */ public function testFailIfInvalidEnvironment() { $installInfo = new InstallInfo('vendor/package', '/path'); $installInfo->setEnvironment('foo'); }
public function testAddDisabledBindingUuidRemovesEnabledMapping() { $installInfo = new InstallInfo('vendor/package', '/path'); $uuid = Uuid::uuid4(); $installInfo->addEnabledBindingUuid($uuid); $installInfo->addDisabledBindingUuid($uuid); $this->assertSame(array(), $installInfo->getEnabledBindingUuids()); $this->assertSame(array($uuid), $installInfo->getDisabledBindingUuids()); }
public function testUnserializeFullRootPackageFile() { $packageFile = $this->serializer->unserializeRootPackageFile(self::FULL_ROOT_JSON, '/path', $this->baseConfig); $installInfo1 = new InstallInfo('vendor/package1', '/path/to/package1'); $installInfo1->setInstallerName('composer'); $installInfo1->addDisabledBindingUuid(Uuid::fromString('4d02ee67-d845-4789-a9c1-8301351c6f5a')); $installInfo2 = new InstallInfo('vendor/package2', '/path/to/package2'); $installInfo2->setEnvironment(Environment::DEV); $this->assertInstanceOf('Puli\\Manager\\Api\\Package\\RootPackageFile', $packageFile); $this->assertSame('/path', $packageFile->getPath()); $this->assertFullConfig($packageFile); $this->assertSame(array('acme/blog-extension1', 'acme/blog-extension2'), $packageFile->getOverrideOrder()); $this->assertEquals(array($installInfo1, $installInfo2), $packageFile->getInstallInfos()); $config = $packageFile->getConfig(); $this->assertSame('puli-dir', $config->get(Config::PULI_DIR)); $this->assertSame('Puli\\MyFactory', $config->get(Config::FACTORY_OUT_CLASS)); $this->assertSame('puli-dir/MyFactory.php', $config->get(Config::FACTORY_OUT_FILE)); $this->assertSame('my-type', $config->get(Config::REPOSITORY_TYPE)); $this->assertSame('puli-dir/my-repo', $config->get(Config::REPOSITORY_PATH)); $this->assertSame('my-store-type', $config->get(Config::DISCOVERY_STORE_TYPE)); }
public function testMatch() { $packageFile = new PackageFile('vendor/name'); $package = new Package($packageFile, __DIR__); $this->assertFalse($package->match(Expr::same('foobar', Package::NAME))); $this->assertTrue($package->match(Expr::same('vendor/name', Package::NAME))); $this->assertFalse($package->match(Expr::same('/path/foo', Package::INSTALL_PATH))); $this->assertTrue($package->match(Expr::same(__DIR__, Package::INSTALL_PATH))); $this->assertFalse($package->match(Expr::same(PackageState::NOT_LOADABLE, Package::STATE))); $this->assertTrue($package->match(Expr::same(PackageState::ENABLED, Package::STATE))); $this->assertFalse($package->match(Expr::same('webmozart', Package::INSTALLER))); // Packages without install info (= the root package) are assumed to be // installed in the production environment $this->assertTrue($package->match(Expr::same(Environment::PROD, Package::ENVIRONMENT))); $installInfo = new InstallInfo('vendor/install-info', '/path'); $installInfo->setInstallerName('webmozart'); $packageWithInstallInfo = new Package($packageFile, __DIR__, $installInfo); $this->assertFalse($packageWithInstallInfo->match(Expr::same('foobar', Package::INSTALLER))); $this->assertTrue($packageWithInstallInfo->match(Expr::same('webmozart', Package::INSTALLER))); $this->assertTrue($packageWithInstallInfo->match(Expr::notsame(Environment::DEV, Package::ENVIRONMENT))); $installInfo2 = new InstallInfo('vendor/install-info', '/path'); $installInfo2->setEnvironment(Environment::DEV); $packageWithInstallInfo2 = new Package($packageFile, __DIR__, $installInfo2); $this->assertTrue($packageWithInstallInfo2->match(Expr::same(Environment::DEV, Package::ENVIRONMENT))); }
private function populateRootConfig(RootPackageFile $packageFile, \stdClass $jsonData) { if (isset($jsonData->{'override-order'})) { $packageFile->setOverrideOrder((array) $jsonData->{'override-order'}); } if (isset($jsonData->plugins)) { $packageFile->setPluginClasses($jsonData->plugins); } if (isset($jsonData->config)) { $config = $packageFile->getConfig(); foreach ($this->objectsToArrays($jsonData->config) as $key => $value) { $config->set($key, $value); } } if (isset($jsonData->packages)) { foreach ($jsonData->packages as $packageName => $packageData) { $installInfo = new InstallInfo($packageName, $packageData->{'install-path'}); if (isset($packageData->installer)) { $installInfo->setInstallerName($packageData->installer); } if (isset($packageData->{'enabled-bindings'})) { foreach ($packageData->{'enabled-bindings'} as $uuid) { $installInfo->addEnabledBindingUuid(Uuid::fromString($uuid)); } } if (isset($packageData->{'disabled-bindings'})) { foreach ($packageData->{'disabled-bindings'} as $uuid) { $installInfo->addDisabledBindingUuid(Uuid::fromString($uuid)); } } $packageFile->addInstallInfo($installInfo); } } }
private function renameNonRootPackage(Package $package, $newName) { $previousInstallInfo = $package->getInstallInfo(); $installInfo = new InstallInfo($newName, $previousInstallInfo->getInstallPath()); $installInfo->setInstallerName($previousInstallInfo->getInstallerName()); foreach ($previousInstallInfo->getEnabledBindingUuids() as $uuid) { $installInfo->addEnabledBindingUuid($uuid); } foreach ($previousInstallInfo->getDisabledBindingUuids() as $uuid) { $installInfo->addDisabledBindingUuid($uuid); } $this->rootPackageFile->removeInstallInfo($package->getName()); $this->rootPackageFile->addInstallInfo($installInfo); try { $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); } catch (Exception $e) { $this->rootPackageFile->removeInstallInfo($newName); $this->rootPackageFile->addInstallInfo($previousInstallInfo); throw $e; } $this->packages->remove($package->getName()); $this->packages->add(new Package($package->getPackageFile(), $package->getInstallPath(), $installInfo, $package->getLoadErrors())); }
public function testMatch() { $packageFile = new PackageFile('vendor/name'); $package = new Package($packageFile, __DIR__); $this->assertFalse($package->match(Expr::same('foobar', Package::NAME))); $this->assertTrue($package->match(Expr::same('vendor/name', Package::NAME))); $this->assertFalse($package->match(Expr::same('/path/foo', Package::INSTALL_PATH))); $this->assertTrue($package->match(Expr::same(__DIR__, Package::INSTALL_PATH))); $this->assertFalse($package->match(Expr::same(PackageState::NOT_LOADABLE, Package::STATE))); $this->assertTrue($package->match(Expr::same(PackageState::ENABLED, Package::STATE))); $this->assertFalse($package->match(Expr::same('webmozart', Package::INSTALLER))); $installInfo = new InstallInfo('vendor/install-info', '/path'); $installInfo->setInstallerName('webmozart'); $packageWithInstallInfo = new Package($packageFile, __DIR__, $installInfo); $this->assertFalse($packageWithInstallInfo->match(Expr::same('foobar', Package::INSTALLER))); $this->assertTrue($packageWithInstallInfo->match(Expr::same('webmozart', Package::INSTALLER))); }
/** * Returns whether the package matches the given expression. * * @param Expression $expr The search criteria. You can use the fields * {@link NAME}, {@link INSTALL_PATH} and * {@link STATE} in the expression. * * @return bool Returns `true` if the package matches the expression and * `false` otherwise. */ public function match(Expression $expr) { return $expr->evaluate(array(self::NAME => $this->name, self::INSTALL_PATH => $this->installPath, self::STATE => $this->state, self::INSTALLER => $this->installInfo ? $this->installInfo->getInstallerName() : null, self::ENVIRONMENT => $this->installInfo ? $this->installInfo->getEnvironment() : Environment::PROD)); }