Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.x bc layer #512

Merged
merged 10 commits into from
Nov 7, 2023
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@
"doctrine/mongodb-odm": "2.5.0"
},
"autoload": {
"psr-4": { "Zenstruck\\Foundry\\": "src/" },
"files": ["src/functions.php"]
"psr-4": {"Zenstruck\\Foundry\\": "src/"},
"files": [
"src/functions.php",
"src/deprecations.php",
"src/Persistence/functions.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
19 changes: 17 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,22 @@ parameters:
path: src/FactoryCollection.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\RepositoryProxy\\:\\:proxyResult\\(\\) should return array\\<int, Zenstruck\\\\Foundry\\\\Proxy\\<TProxiedObject of object\\>\\>\\|Zenstruck\\\\Foundry\\\\Proxy\\<TProxiedObject of object\\> but returns \\(TProxiedObject of object\\)\\|null\\.$#"
message: "#^Method Zenstruck\\\\Foundry\\\\Persistence\\\\RepositoryDecorator\\:\\:proxyResult\\(\\) should return array\\<int, Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TProxiedObject of object\\>\\>\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TProxiedObject of object\\> but returns \\(TProxiedObject of object\\)\\|null\\.$#"
count: 1
path: src/Persistence/RepositoryDecorator.php

-
message: "#^If condition is always false\\.$#"
count: 1
path: src/Proxy.php

-
message: "#^If condition is always false\\.$#"
count: 1
path: src/RepositoryAssertions.php

-
message: "#^If condition is always false\\.$#"
count: 1
path: src/RepositoryProxy.php

Expand Down Expand Up @@ -56,6 +71,6 @@ parameters:
path: src/ZenstruckFoundryBundle.php

-
message: "#^Function Zenstruck\\\\Foundry\\\\anonymous\\(\\) should return Zenstruck\\\\Foundry\\\\Factory\\<TObject of object\\> but returns class@anonymous/src/functions\\.php\\:45\\.$#"
message: "#^Function Zenstruck\\\\Foundry\\\\anonymous\\(\\) should return Zenstruck\\\\Foundry\\\\Factory\\<TObject of object\\> but returns class@anonymous/src/functions\\.php\\:47\\.$#"
count: 1
path: src/functions.php
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ parameters:
ignoreErrors:
-
message: '#Unsafe usage of new static\(\).*#'
path: ./src/ModelFactory.php
path: ./src/Persistence/PersistentProxyObjectFactory.php
2 changes: 1 addition & 1 deletion phpunit.dama.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ini name="error_reporting" value="-1"/>
<env name="KERNEL_CLASS" value="Zenstruck\Foundry\Tests\Fixtures\Kernel"/>
<env name="USE_DAMA_DOCTRINE_TEST_BUNDLE" value="1"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=9999&amp;max[direct]=0&amp;quiet[]=indirect&amp;quiet[]=other"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="/will be private in Foundry/"/>
<env name="SHELL_VERBOSITY" value="0"/>
</php>

Expand Down
45 changes: 28 additions & 17 deletions src/AnonymousFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace Zenstruck\Foundry;

use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Persistence\RepositoryDecorator;
use Zenstruck\Foundry\Persistence\RepositoryAssertions;

/**
* @template TModel of object
* @template-extends Factory<TModel>
Expand All @@ -21,11 +25,18 @@
*/
final class AnonymousFactory extends Factory implements \Countable, \IteratorAggregate
{
/**
* @var class-string<TModel>
*/
private string $class;

public function __construct(string $class, array|callable $defaultAttributes = [])
{
$this->class = $class;

trigger_deprecation('zenstruck\foundry', '1.30', 'Class "AnonymousFactory" is deprecated and will be removed in 2.0. Use the "anonymous()" or "repository()" functions instead.');

parent::__construct($class, $defaultAttributes);
parent::__construct($class, $defaultAttributes, calledInternally: true);
}

/**
Expand Down Expand Up @@ -55,35 +66,35 @@ public function findOrCreate(array $attributes): Proxy
}

/**
* @see RepositoryProxy::first()
* @see RepositoryDecorator::first()
*
* @throws \RuntimeException If no entities exist
*/
public function first(string $sortedField = 'id'): Proxy
{
if (null === $proxy = $this->repository()->first($sortedField)) {
throw new \RuntimeException(\sprintf('No "%s" objects persisted.', $this->class()));
throw new \RuntimeException(\sprintf('No "%s" objects persisted.', $this->class));
}

return $proxy;
}

/**
* @see RepositoryProxy::last()
* @see RepositoryDecorator::last()
*
* @throws \RuntimeException If no entities exist
*/
public function last(string $sortedField = 'id'): Proxy
{
if (null === $proxy = $this->repository()->last($sortedField)) {
throw new \RuntimeException(\sprintf('No "%s" objects persisted.', $this->class()));
throw new \RuntimeException(\sprintf('No "%s" objects persisted.', $this->class));
}

return $proxy;
}

/**
* @see RepositoryProxy::random()
* @see RepositoryDecorator::random()
*/
public function random(array $attributes = []): Proxy
{
Expand All @@ -106,7 +117,7 @@ public function randomOrCreate(array $attributes = []): Proxy
}

/**
* @see RepositoryProxy::randomSet()
* @see RepositoryDecorator::randomSet()
*
* @return object[]
*/
Expand All @@ -116,7 +127,7 @@ public function randomSet(int $number, array $attributes = []): array
}

/**
* @see RepositoryProxy::randomRange()
* @see RepositoryDecorator::randomRange()
*
* @return object[]
*/
Expand All @@ -126,7 +137,7 @@ public function randomRange(int $min, int $max, array $attributes = []): array
}

/**
* @see RepositoryProxy::count()
* @see RepositoryDecorator::count()
*/
public function count(): int
{
Expand All @@ -139,15 +150,15 @@ public function getIterator(): \ArrayIterator
}

/**
* @see RepositoryProxy::truncate()
* @see RepositoryDecorator::truncate()
*/
public function truncate(): void
{
$this->repository()->truncate();
}

/**
* @see RepositoryProxy::findAll()
* @see RepositoryDecorator::findAll()
*
* @return object[]
*/
Expand All @@ -157,7 +168,7 @@ public function all(): array
}

/**
* @see RepositoryProxy::find()
* @see RepositoryDecorator::find()
*
* @phpstan-param Proxy<TModel>|array|mixed $criteria
*
Expand All @@ -166,14 +177,14 @@ public function all(): array
public function find($criteria): Proxy
{
if (null === $proxy = $this->repository()->find($criteria)) {
throw new \RuntimeException(\sprintf('Could not find "%s" object.', $this->class()));
throw new \RuntimeException(\sprintf('Could not find "%s" object.', $this->class));
}

return $proxy;
}

/**
* @see RepositoryProxy::findBy()
* @see RepositoryDecorator::findBy()
*
* @return object[]
*/
Expand All @@ -188,10 +199,10 @@ public function assert(): RepositoryAssertions
}

/**
* @phpstan-return RepositoryProxy<TModel>
* @phpstan-return RepositoryDecorator<TModel>
*/
public function repository(): RepositoryProxy
public function repository(): RepositoryDecorator
{
return self::configuration()->repositoryFor($this->class());
return self::configuration()->repositoryFor($this->class);
}
}
7 changes: 4 additions & 3 deletions src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
use Zenstruck\Foundry\Bundle\Command\StubMakeFactory;
use Zenstruck\Foundry\Bundle\Command\StubMakeStory;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Instantiator;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Story;
use Zenstruck\Foundry\Test\ORMDatabaseResetter;

Expand All @@ -41,7 +42,7 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
->addTag('foundry.story')
;

$container->registerForAutoconfiguration(ModelFactory::class)
$container->registerForAutoconfiguration(PersistentProxyObjectFactory::class)
->addTag('foundry.factory')
;

Expand Down Expand Up @@ -87,7 +88,7 @@ private function configureDefaultInstantiator(array $config, ContainerBuilder $c
$definition = $container->getDefinition('.zenstruck_foundry.default_instantiator');

if ($config['without_constructor']) {
$definition->addMethodCall('withoutConstructor');
$definition->setFactory([Instantiator::class, 'withoutConstructor']);
}

if ($config['allow_extra_attributes']) {
Expand Down
8 changes: 4 additions & 4 deletions src/Bundle/Maker/Factory/FactoryClassMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Zenstruck\Foundry\Bundle\Maker\Factory;

use Zenstruck\Foundry\Bundle\Maker\Factory\Exception\FactoryClassAlreadyExistException;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;

/**
* @internal
Expand All @@ -24,15 +24,15 @@ final class FactoryClassMap
*/
private array $classesWithFactories;

/** @param \Traversable<ModelFactory> $factories */
/** @param \Traversable<PersistentProxyObjectFactory> $factories */
public function __construct(\Traversable $factories)
{
/** @phpstan-ignore-next-line */
$this->classesWithFactories = \array_unique(
\array_reduce(
\iterator_to_array($factories, preserve_keys: true),
static function(array $carry, ModelFactory $factory): array {
$carry[$factory::class] = $factory::getEntityClass();
static function(array $carry, PersistentProxyObjectFactory $factory): array {
$carry[$factory::class] = $factory::class();

return $carry;
},
Expand Down
10 changes: 5 additions & 5 deletions src/Bundle/Maker/Factory/MakeFactoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Persistence\RepositoryDecorator;

/**
* @internal
Expand All @@ -36,14 +36,14 @@ final class MakeFactoryData
public function __construct(private \ReflectionClass $object, private ClassNameDetails $factoryClassNameDetails, private ?\ReflectionClass $repository, private string $staticAnalysisTool, private bool $persisted)
{
$this->uses = [
ModelFactory::class,
PersistentProxyObjectFactory::class,
Proxy::class,
$object->getName(),
];

if ($repository) {
$this->uses[] = $repository->getName();
$this->uses[] = RepositoryProxy::class;
$this->uses[] = RepositoryDecorator::class;
}

$this->methodsInPHPDoc = MakeFactoryPHPDocMethod::createAll($this);
Expand Down
4 changes: 2 additions & 2 deletions src/Bundle/Maker/Factory/MakeFactoryPHPDocMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function toString(string|null $staticAnalysisTool = null): string

if ($this->repository) {
$returnType = match ((bool) $staticAnalysisTool) {
false => "{$this->repository}|RepositoryProxy",
true => "RepositoryProxy<{$this->objectName}>",
false => "{$this->repository}|RepositoryDecorator",
true => "RepositoryDecorator<{$this->objectName}>",
};
} else {
/** @phpstan-ignore-next-line */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function getAutocompleteValues(): array

$class = $this->toPSR4($rootPath, $phpFile, $namespacePrefix);

if (in_array($class, ['Zenstruck\Foundry\Proxy', 'Zenstruck\Foundry\RepositoryProxy'])) {
// do not load legacy Proxy: prevents deprecations in tests.
continue;
}

try {
// @phpstan-ignore-next-line $class is not always a class-string
$reflection = new \ReflectionClass($class);
Expand Down
4 changes: 3 additions & 1 deletion src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id=".zenstruck_foundry.default_instantiator" class="Zenstruck\Foundry\Instantiator" />
<service id=".zenstruck_foundry.default_instantiator" class="Zenstruck\Foundry\Instantiator">
<factory class="Zenstruck\Foundry\Instantiator" method="withConstructor" />
</service>
<service id=".zenstruck_foundry.faker" class="Faker\Generator">
<factory class="Faker\Factory" method="create" />
</service>
Expand Down
19 changes: 9 additions & 10 deletions src/Bundle/Resources/skeleton/Factory.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
?>

/**
* @extends ModelFactory<<?php echo $makeFactoryData->getObjectShortName(); ?>>
* @extends PersistentProxyObjectFactory<<?php echo $makeFactoryData->getObjectShortName(); ?>>
*
<?php
foreach ($makeFactoryData->getMethodsPHPDoc() as $methodPHPDoc) {
Expand All @@ -25,7 +25,7 @@
}
?>
*/
final class <?php echo $class_name; ?> extends ModelFactory
final class <?php echo $class_name; ?> extends PersistentProxyObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
Expand All @@ -34,15 +34,19 @@ final class <?php echo $class_name; ?> extends ModelFactory
*/
public function __construct()
{
parent::__construct();
}

public static function class(): string
{
return <?php echo $makeFactoryData->getObjectShortName() ?>::class;
}

/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
protected function getDefaults(): array
protected function defaults(): array|callable
{
return [
<?php
Expand All @@ -56,7 +60,7 @@ protected function getDefaults(): array
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
protected function initialize(): static
{
return $this
<?php if (!$makeFactoryData->isPersisted()) { ?>
Expand All @@ -65,9 +69,4 @@ protected function initialize(): self
// ->afterInstantiate(function(<?php echo $makeFactoryData->getObjectShortName(); ?> $<?php echo \lcfirst($makeFactoryData->getObjectShortName()); ?>): void {})
;
}

protected static function getClass(): string
{
return <?php echo $makeFactoryData->getObjectShortName(); ?>::class;
}
}
Loading