method() public static method

Check that the result of a method call matches an expression.
public static method ( string $methodName, $args ) : Method
$methodName string The name of the method to call.
return Webmozart\Expression\Selector\Method The created expression.
 public function testRemoveBindingsWithTypeAndParameterWorksOnLoadedDiscovery()
 {
     $binding1 = new StringBinding('string1', Foo::clazz, array('param2' => 'bar'));
     $binding2 = new StringBinding('string2', Foo::clazz);
     $binding3 = new StringBinding('string3', Foo::clazz, array('param1' => 'bar'));
     $discovery = $this->createDiscovery();
     $discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING, array(new BindingParameter('param1', BindingParameter::OPTIONAL, 'foo'), new BindingParameter('param2'))));
     $discovery->addBinding($binding1);
     $discovery->addBinding($binding2);
     $discovery->addBinding($binding3);
     $discovery = $this->loadDiscoveryFromStorage($discovery);
     // Bindings need to be initialized for this to work
     $discovery->removeBindings(Foo::clazz, Expr::method('getParameterValue', 'param1', Expr::same('foo')));
     $this->assertEquals(array($binding3), $discovery->findBindings(Foo::clazz));
     $this->assertEquals(array($binding3), $discovery->getBindings());
 }
Ejemplo n.º 2
0
 /**
  * Handles the "puli type --list" command.
  *
  * @param Args $args The console arguments.
  * @param IO   $io   The I/O.
  *
  * @return int The status code.
  */
 public function handleList(Args $args, IO $io)
 {
     $packageNames = ArgsUtil::getPackageNames($args, $this->packages);
     $states = $this->getBindingTypeStates($args);
     $printStates = count($states) > 1;
     $printPackageName = count($packageNames) > 1;
     $printHeaders = $printStates || $printPackageName;
     $printTypeAdvice = true;
     $printBindAdvice = false;
     $indentation = $printStates && $printPackageName ? 8 : ($printStates || $printPackageName ? 4 : 0);
     foreach ($states as $state) {
         $statePrinted = !$printStates;
         foreach ($packageNames as $packageName) {
             $expr = Expr::method('getContainingPackage', Expr::method('getName', Expr::same($packageName)))->andMethod('getState', Expr::same($state));
             $bindingTypes = $this->discoveryManager->findTypeDescriptors($expr);
             if (!$bindingTypes) {
                 continue;
             }
             $printTypeAdvice = false;
             if (!$statePrinted) {
                 $this->printBindingTypeState($io, $state);
                 $statePrinted = true;
                 // Only print the advice if at least one type was printed
                 $printBindAdvice = true;
             }
             if ($printPackageName) {
                 $prefix = $printStates ? '    ' : '';
                 $io->writeLine("{$prefix}Package: {$packageName}");
                 $io->writeLine('');
             }
             $styleTag = BindingTypeState::ENABLED === $state ? null : 'bad';
             $this->printTypeTable($io, $bindingTypes, $styleTag, $indentation);
             if ($printHeaders) {
                 $io->writeLine('');
             }
         }
     }
     if ($printTypeAdvice) {
         $io->writeLine('No types defined. Use "puli type --define <name>" to define a type.');
     }
     if ($printBindAdvice) {
         $io->writeLine('Use "puli bind <resource> <type>" to bind a resource to a type.');
     }
     return 0;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function hasRootBindingDescriptors(Expression $expr = null)
 {
     $expr2 = Expr::method('getContainingModule', Expr::same($this->rootModule));
     if ($expr) {
         $expr2 = $expr2->andX($expr);
     }
     return $this->hasBindingDescriptors($expr2);
 }
 /**
  * {@inheritdoc}
  */
 public function leaveExpression(Expression $expr)
 {
     if ($expr instanceof Method) {
         switch ($expr->getMethodName()) {
             case 'getUuid':
                 return Expr::method('getUuid', $expr->getExpression());
             case 'getGlob':
                 $queryExpr = $expr->getExpression();
                 if ($queryExpr instanceof Same) {
                     $queryExpr = Expr::same($queryExpr->getComparedValue() . '{,/**/*}');
                 } elseif ($queryExpr instanceof Equals) {
                     $queryExpr = Expr::equals($queryExpr->getComparedValue() . '{,/**/*}');
                 } elseif ($queryExpr instanceof NotSame) {
                     $queryExpr = Expr::notSame($queryExpr->getComparedValue() . '{,/**/*}');
                 } elseif ($queryExpr instanceof NotEquals) {
                     $queryExpr = Expr::notEquals($queryExpr->getComparedValue() . '{,/**/*}');
                 } elseif ($queryExpr instanceof EndsWith) {
                     $queryExpr = Expr::endsWith($queryExpr->getAcceptedSuffix() . '{,/**/*}');
                 }
                 return Expr::method('getBinding', Expr::method('getQuery', $queryExpr));
             case 'getServerName':
                 return Expr::method('getParameterValue', DiscoveryUrlGenerator::SERVER_PARAMETER, $expr->getExpression());
             case 'getServerPath':
                 return Expr::method('getParameterValue', DiscoveryUrlGenerator::PATH_PARAMETER, $expr->getExpression());
         }
     }
     return $expr;
 }
Ejemplo n.º 5
0
 /**
  * Returns the packages that should be displayed for the given console
  * arguments.
  *
  * @param Args $args The console arguments.
  *
  * @return PackageCollection The packages.
  */
 private function getSelectedPackages(Args $args)
 {
     $states = $this->getSelectedStates($args);
     $expr = Expr::true();
     $envs = array();
     if ($states !== PackageState::all()) {
         $expr = $expr->andMethod('getState', Expr::in($states));
     }
     if ($args->isOptionSet('installer')) {
         $expr = $expr->andMethod('getInstallInfo', Expr::method('getInstallerName', Expr::same($args->getOption('installer'))));
     }
     if ($args->isOptionSet('prod')) {
         $envs[] = Environment::PROD;
     }
     if ($args->isOptionSet('dev')) {
         $envs[] = Environment::DEV;
     }
     if (count($envs) > 0) {
         $expr = $expr->andMethod('getInstallInfo', Expr::method('getEnvironment', Expr::in($envs)));
     }
     return $this->packageManager->findPackages($expr);
 }
Ejemplo n.º 6
0
 /**
  * Returns the resource discovery manager.
  *
  * @return DiscoveryManager The discovery manager.
  */
 public function getDiscoveryManager()
 {
     if (!$this->started) {
         throw new LogicException('Puli was not started');
     }
     if (!$this->discoveryManager && $this->context instanceof ProjectContext) {
         $this->discoveryManager = new DiscoveryManagerImpl($this->context, $this->getDiscovery(), $this->getPackageManager()->findPackages(Expr::method('isEnabled', Expr::same(true))), $this->getPackageFileStorage(), $this->logger);
     }
     return $this->discoveryManager;
 }
 /**
  * {@inheritdoc}
  */
 public function hasAssetMapping(Uuid $uuid)
 {
     $expr = Expr::method('getUuid', Expr::method('toString', Expr::same($uuid->toString())))->andX($this->exprBuilder->buildExpression());
     return $this->discoveryManager->hasBindingDescriptors($expr);
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function hasRootPathMappings(Expression $expr = null)
 {
     $expr2 = Expr::method('getContainingModule', Expr::same($this->rootModule));
     if ($expr) {
         $expr2 = $expr2->andX($expr);
     }
     return $this->hasPathMappings($expr2);
 }
Ejemplo n.º 9
0
 /**
  * @param string $uuidPrefix
  *
  * @return BindingDescriptor
  */
 private function getBindingByUuidPrefix($uuidPrefix)
 {
     $expr = Expr::method('getUuid', Expr::startsWith($uuidPrefix));
     $descriptors = $this->discoveryManager->findBindingDescriptors($expr);
     if (0 === count($descriptors)) {
         throw new RuntimeException(sprintf('The binding "%s" does not exist.', $uuidPrefix));
     }
     if (count($descriptors) > 1) {
         throw new RuntimeException(sprintf('More than one binding matches the UUID prefix "%s".', $uuidPrefix));
     }
     return reset($descriptors);
 }
 public function testRemoveBindingsWithTypeAndParametersDoesNothingIfTypeNotFound()
 {
     $discovery = $this->createDiscovery();
     $discovery->removeBindings(Foo::clazz, Expr::method('getParameterValue', 'param1', Expr::same('foo')));
     $discovery = $this->loadDiscoveryFromStorage($discovery);
     $this->assertCount(0, $discovery->getBindings());
 }
Ejemplo n.º 11
0
 /**
  * @param string $uuidPrefix
  *
  * @return AssetMapping
  */
 private function getMappingByUuidPrefix($uuidPrefix)
 {
     $expr = Expr::method('getUuid', Expr::startsWith($uuidPrefix));
     $mappings = $this->assetManager->findAssetMappings($expr);
     if (!$mappings) {
         throw new RuntimeException(sprintf('The mapping with the UUID prefix "%s" does not exist.', $uuidPrefix));
     }
     if (count($mappings) > 1) {
         throw new RuntimeException(sprintf('More than one mapping matches the UUID prefix "%s".', $uuidPrefix));
     }
     return reset($mappings);
 }
Ejemplo n.º 12
0
 /**
  * Handles the "puli map --list" command.
  *
  * @param Args $args The console arguments.
  * @param IO   $io   The I/O.
  *
  * @return int The status code.
  */
 public function handleList(Args $args, IO $io)
 {
     $packageNames = ArgsUtil::getPackageNames($args, $this->packages);
     $states = $this->getPathMappingStates($args);
     $printState = count($states) > 1;
     $printPackageName = count($packageNames) > 1;
     $printHeaders = $printState || $printPackageName;
     $printAdvice = true;
     $indentation = $printState && $printPackageName ? 8 : ($printState || $printPackageName ? 4 : 0);
     foreach ($states as $state) {
         $statePrinted = !$printState;
         if (PathMappingState::CONFLICT === $state) {
             $expr = Expr::method('getContainingPackage', Expr::method('getName', Expr::in($packageNames)))->andMethod('getState', Expr::same($state));
             $mappings = $this->repoManager->findPathMappings($expr);
             if (!$mappings) {
                 continue;
             }
             $printAdvice = false;
             if ($printState) {
                 $this->printPathMappingStateHeader($io, $state);
             }
             $this->printConflictTable($io, $mappings, $printState ? 4 : 0);
             if ($printHeaders) {
                 $io->writeLine('');
             }
             continue;
         }
         foreach ($packageNames as $packageName) {
             $expr = Expr::method('getContainingPackage', Expr::method('getName', Expr::same($packageName)))->andMethod('getState', Expr::same($state));
             $mappings = $this->repoManager->findPathMappings($expr);
             if (!$mappings) {
                 continue;
             }
             $printAdvice = false;
             if (!$statePrinted) {
                 $this->printPathMappingStateHeader($io, $state);
                 $statePrinted = true;
             }
             if ($printPackageName) {
                 $prefix = $printState ? '    ' : '';
                 $io->writeLine(sprintf('%sPackage: %s', $prefix, $packageName));
                 $io->writeLine('');
             }
             $this->printMappingTable($io, $mappings, $indentation, PathMappingState::ENABLED === $state);
             if ($printHeaders) {
                 $io->writeLine('');
             }
         }
     }
     if ($printAdvice) {
         $io->writeLine('No path mappings. Use "puli map <path> <file>" to map a Puli path to a file or directory.');
     }
     return 0;
 }
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage stdClass
  */
 public function testHasBindingsWithTypeAndParametersFailsIfInvalidType()
 {
     $discovery = $this->createLoadedDiscovery();
     $discovery->hasBindings(new stdClass(), Expr::method('getParameterValue', 'param', Expr::same('foo')));
 }
Ejemplo n.º 14
0
 /**
  * Transform the expression to Webmozarts' Expression.
  *
  * @param Expression $expression
  *
  * @return \Webmozart\Expression\Expression
  */
 protected function transform(Expression $expression)
 {
     $constraint = $expression->getConstraint();
     $getter = 'get' . ucfirst($expression->getSelector());
     return Expr::method($getter, new $constraint($expression->getValue()));
 }
Ejemplo n.º 15
0
 /**
  * {@inheritdoc}
  */
 public function removeServer($serverName)
 {
     $this->removeServers(Expr::method('getName', Expr::same($serverName)));
 }