-
-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC: add support for immutable functions
- Loading branch information
Showing
18 changed files
with
420 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\Entity; | ||
|
||
class DummyWithImmutableFunction | ||
{ | ||
private $val; | ||
|
||
public function __construct(string $val) | ||
{ | ||
$this->val = $val; | ||
} | ||
|
||
public function withVal(string $val): self | ||
{ | ||
return new self($val); | ||
} | ||
|
||
public function getVal(): string | ||
{ | ||
return $this->val; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\Definition\Flag; | ||
|
||
use Nelmio\Alice\Definition\FlagInterface; | ||
|
||
final class ConfiguratorFlag implements FlagInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return 'configurator'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\Definition\MethodCall; | ||
|
||
use Nelmio\Alice\Definition\Flag\OptionalFlag; | ||
use Nelmio\Alice\Definition\MethodCallInterface; | ||
|
||
/** | ||
* Represents a method call that is a configurator, i.e. for which the results should be kept. | ||
*/ | ||
final class ConfiguratorMethodCall implements MethodCallInterface | ||
{ | ||
/** | ||
* @var MethodCallInterface | ||
*/ | ||
private $methodCall; | ||
|
||
public function __construct(MethodCallInterface $methodCall) | ||
{ | ||
$this->methodCall = $methodCall; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function withArguments(array $arguments = null): self | ||
{ | ||
$clone = clone $this; | ||
$clone->methodCall = $clone->methodCall->withArguments($arguments); | ||
|
||
return $clone; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getCaller() | ||
{ | ||
return $this->methodCall->getCaller(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getMethod(): string | ||
{ | ||
return $this->methodCall->getMethod(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getArguments() | ||
{ | ||
return $this->methodCall->getArguments(); | ||
} | ||
|
||
public function getOriginalMethodCall(): MethodCallInterface | ||
{ | ||
return $this->methodCall; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return $this->methodCall->__toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
.../Fixture/SpecificationBagDenormalizer/Calls/MethodFlagHandler/ConfiguratorFlagHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\Calls\MethodFlagHandler; | ||
|
||
use Nelmio\Alice\Definition\Flag\ConfiguratorFlag; | ||
use Nelmio\Alice\Definition\Flag\OptionalFlag; | ||
use Nelmio\Alice\Definition\FlagInterface; | ||
use Nelmio\Alice\Definition\MethodCall\ConfiguratorMethodCall; | ||
use Nelmio\Alice\Definition\MethodCall\OptionalMethodCall; | ||
use Nelmio\Alice\Definition\MethodCallInterface; | ||
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\Calls\MethodFlagHandler; | ||
use Nelmio\Alice\IsAServiceTrait; | ||
|
||
final class ConfiguratorFlagHandler implements MethodFlagHandler | ||
{ | ||
use IsAServiceTrait; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function handleMethodFlags(MethodCallInterface $methodCall, FlagInterface $flag): MethodCallInterface | ||
{ | ||
if ($flag instanceof ConfiguratorFlag) { | ||
return new ConfiguratorMethodCall($methodCall); | ||
} | ||
|
||
return $methodCall; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/FixtureBuilder/Denormalizer/FlagParser/Chainable/ConfiguratorFlagParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParser\Chainable; | ||
|
||
use Nelmio\Alice\Definition\Flag\ConfiguratorFlag; | ||
use Nelmio\Alice\Definition\Flag\UniqueFlag; | ||
use Nelmio\Alice\Definition\FlagBag; | ||
use Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParser\ChainableFlagParserInterface; | ||
use Nelmio\Alice\IsAServiceTrait; | ||
|
||
final class ConfiguratorFlagParser implements ChainableFlagParserInterface | ||
{ | ||
use IsAServiceTrait; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function canParse(string $element): bool | ||
{ | ||
return 'configurator' === $element; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function parse(string $element): FlagBag | ||
{ | ||
return (new FlagBag(''))->withFlag(new ConfiguratorFlag()); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/Generator/Caller/Chainable/ConfiguratorMethodCallProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\Generator\Caller\Chainable; | ||
|
||
use Nelmio\Alice\Definition\MethodCall\ConfiguratorMethodCall; | ||
use Nelmio\Alice\Definition\MethodCall\SimpleMethodCall; | ||
use Nelmio\Alice\Definition\MethodCallInterface; | ||
use Nelmio\Alice\Definition\Object\SimpleObject; | ||
use Nelmio\Alice\Definition\ValueInterface; | ||
use Nelmio\Alice\FixtureInterface; | ||
use Nelmio\Alice\Generator\Caller\CallProcessorAwareInterface; | ||
use Nelmio\Alice\Generator\Caller\CallProcessorInterface; | ||
use Nelmio\Alice\Generator\Caller\ChainableCallProcessorInterface; | ||
use Nelmio\Alice\Generator\CallerInterface; | ||
use Nelmio\Alice\Generator\GenerationContext; | ||
use Nelmio\Alice\Generator\ResolvedFixtureSet; | ||
use Nelmio\Alice\Generator\ValueResolverAwareInterface; | ||
use Nelmio\Alice\Generator\ValueResolverInterface; | ||
use Nelmio\Alice\IsAServiceTrait; | ||
use Nelmio\Alice\ObjectInterface; | ||
use Nelmio\Alice\Throwable\Exception\Generator\Resolver\ResolverNotFoundExceptionFactory; | ||
use Nelmio\Alice\Throwable\Exception\Generator\Resolver\UnresolvableValueDuringGenerationExceptionFactory; | ||
use Nelmio\Alice\Throwable\InstantiationThrowable; | ||
use Nelmio\Alice\Throwable\ResolutionThrowable; | ||
|
||
final class ConfiguratorMethodCallProcessor implements ChainableCallProcessorInterface, CallProcessorAwareInterface | ||
{ | ||
use IsAServiceTrait; | ||
|
||
/** | ||
* @var CallProcessorInterface|null | ||
*/ | ||
private $processor; | ||
|
||
public function __construct(CallProcessorInterface $processor = null) | ||
{ | ||
$this->processor = $processor; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function withProcessor(CallProcessorInterface $processor): self | ||
{ | ||
return new self($processor); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function canProcess(MethodCallInterface $methodCall): bool | ||
{ | ||
return $methodCall instanceof ConfiguratorMethodCall; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function process( | ||
ObjectInterface $object, | ||
ResolvedFixtureSet $fixtureSet, | ||
GenerationContext $context, | ||
MethodCallInterface $methodCall | ||
): ResolvedFixtureSet | ||
{ | ||
if (null === $this->processor) { | ||
throw new \LogicException('TODO'); | ||
} | ||
|
||
$context->markRetrieveCallResult(); | ||
|
||
$fixtureSet = $this->processor->process( | ||
$object, | ||
$fixtureSet, | ||
$context, | ||
$methodCall->getOriginalMethodCall() | ||
); | ||
|
||
$context->unmarkRetrieveCallResult(); | ||
|
||
return $fixtureSet; | ||
} | ||
} |
Oops, something went wrong.