cycle / proxy-factory Goto Github PK
View Code? Open in Web Editor NEWProxy Factory implementation for cycle ORM
License: MIT License
Proxy Factory implementation for cycle ORM
License: MIT License
Here's a reproduction case for the bug.
src/Entity.php
<?php
declare(strict_types=1);
namespace App;
use App\Entity\Child;
class Entity
{
public function updateChild(Child $child): void {}
}
src/Entity/Child.php
<?php
declare(strict_types=1);
namespace App\Entity;
class Child {}
Materializing App\Entity
proxy will give us the following code (truncated for clarity):
<?php
declare(strict_types=1);
namespace App;
use Cycle\ORM\Promise\PromiseInterface;
use Cycle\ORM\Promise\Resolver;
use Cycle\ORM\Promise\Exception\ProxyFactoryException;
use Cycle\ORM\ORMInterface;
class EntityProxy_075f7896d56248c10e30d6df26d213297e38b30cdafa676bc55cbbdb7c1804a5 extends Entity implements PromiseInterface
{
public function updateChild(App\Entity\Child $child) : void
{
if ($this->resolver->__resolve() !== null) {
$this->resolver->__resolve()->updateChild($child);
} else {
throw new ProxyFactoryException(sprintf('Method `%s()` not loaded for `%s`', 'updateChild', 'App\\Entity'));
}
}
}
As seen here, use of App\Entity\Child
is incorrect because it should be \App\Entity\Child
. Otherwise, it's being considered local, i.e \App\App\Entity\Child
.
1). Methods with variadic arguments like public function hasRole(...$roles)
are generated as public function hasRole($roles)
2). Arguments with default values like public function fill($attributes = [])
are generated as public function fill($attributes)
I want to use in my entity classes methods smth like this:
public function setProperties(array|string $properties) : self
{
$this->properties = is_string($properties) ? $properties : json_encode($properties);
return $this;
}
When a model loaded directly (fetchOne(), findByPk() and etc) everything is fine.
But when I want to use lazy load of relations (it happens through promises classes) I receive a 500 error with the message
Call to undefined method ReflectionUnionType::getName() in ... /Promise/Declaration/Extractor/Methods.php at line 215
I also use union types in class properties, but with them all seems good.
Instantiator required for ProxyFactory.
I try use cycle/proxy-factory with yii2, but yii2 container don't support DI for constructor arguments with default null.
I have pretty common code, where I get Id in Controller action, and try to load entity:
public function store(int $id, ProfileQuestionAnswerRequest $request)
{
if (!$request->isValid()) {
return $this->response->validationError($request->getErrors());
}
/** @var ProfileQuestion $question */
$question = $this->profileQuestionRepository->findByPK($request->questionId);
return [
'type' => $question->getType(),
];
}
Where profileQuestionRepository is class ProfileQuestionRepository extends Repository
,
And $question is ProfileQuestion entity with annotation
@Annotation\Entity(repository = "App\Repository\ProfileQuestionRepository")
@Annotation\Table(
indexes = {
@Annotation\Table\Index(columns = {"profile_id"}),
@Annotation\Table\Index(columns = {"sort_order"}),
@Annotation\Table\Index(columns = {"active"})
}
)
findByPK() causes Fatal error:
Fatal error: '\self' is an invalid class name in /var/www/core/vendor/cycle/proxy-factory/src/Materizalizer/EvalMaterializer.php(16) : eval()'d code on line 86
Call Stack:
0.0003 394448 1. {main}() /var/www/core/app.php:0
0.1466 4222104 2. App\App->serve() /var/www/core/app.php:21
0.1467 4222480 3. Spiral\Core\ContainerScope::runScope() /var/www/core/vendor/spiral/boot/src/AbstractKernel.php:100
0.1467 4222480 4. Spiral\Http\RrDispacher->serve() /var/www/core/vendor/spiral/core/src/ContainerScope.php:48
111.9680 4711552 5. Spiral\Http\Http->handle() /var/www/core/vendor/spiral/framework/src/Http/RrDispacher.php:75
111.9681 4711664 6. Spiral\Http\Pipeline->handle() /var/www/core/vendor/spiral/http/src/Http.php:98
111.9681 4711664 7. App\Middleware\ParseJsonMiddleware->process() /var/www/core/vendor/spiral/http/src/Pipeline.php:80
111.9683 4712376 8. Spiral\Http\Pipeline->handle() /var/www/core/app/src/Middleware/ParseJsonMiddleware.php:27
111.9684 4713448 9. Spiral\Core\Container->runScope() /var/www/core/vendor/spiral/http/src/Pipeline.php:85
111.9685 4713824 10. Spiral\Http\Pipeline->Spiral\Http\{closure:/var/www/core/vendor/spiral/http/src/Pipeline.php:83-85}() /var/www/core/vendor/spiral/core/src/Container.php:264
111.9686 4713824 11. Spiral\Router\Router->handle() /var/www/core/vendor/spiral/http/src/Pipeline.php:84
111.9696 4716072 12. Spiral\Core\Container->runScope() /var/www/core/vendor/spiral/router/src/Router.php:76
111.9696 4716448 13. Spiral\Router\Router->Spiral\Router\{closure:/var/www/core/vendor/spiral/router/src/Router.php:74-76}() /var/www/core/vendor/spiral/core/src/Container.php:264
111.9696 4716448 14. Spiral\Router\Route->handle() /var/www/core/vendor/spiral/router/src/Router.php:75
111.9731 4836456 15. Spiral\Http\Pipeline->process() /var/www/core/vendor/spiral/router/src/Route.php:118
111.9732 4836568 16. Spiral\Http\Pipeline->handle() /var/www/core/vendor/spiral/http/src/Pipeline.php:66
111.9732 4837640 17. Spiral\Core\Container->runScope() /var/www/core/vendor/spiral/http/src/Pipeline.php:85
111.9732 4838016 18. Spiral\Http\Pipeline->Spiral\Http\{closure:/var/www/core/vendor/spiral/http/src/Pipeline.php:83-85}() /var/www/core/vendor/spiral/core/src/Container.php:264
111.9732 4838016 19. Spiral\Router\CoreHandler->handle() /var/www/core/vendor/spiral/http/src/Pipeline.php:84
111.9739 4876304 20. Spiral\Core\Core->callAction() /var/www/core/vendor/spiral/router/src/CoreHandler.php:107
111.9845 4891104 21. Spiral\Core\Container->runScope() /var/www/core/vendor/spiral/hmvc/src/AbstractCore.php:69
111.9846 4891856 22. Spiral\Core\Core->Spiral\Core\{closure:/var/www/core/vendor/spiral/hmvc/src/AbstractCore.php:51-69}() /var/www/core/vendor/spiral/core/src/Container.php:264
112.1378 9609152 23. Spiral\Core\ContainerScope::runScope() /var/www/core/vendor/spiral/hmvc/src/AbstractCore.php:68
112.1378 9609152 24. Spiral\Core\Core->Spiral\Core\{closure:/var/www/core/vendor/spiral/hmvc/src/AbstractCore.php:57-68}() /var/www/core/vendor/spiral/core/src/ContainerScope.php:48
112.1379 9609152 25. Spiral\Core\Core->callMethod() /var/www/core/vendor/spiral/hmvc/src/AbstractCore.php:63
112.1593 9976616 26. ReflectionMethod->invokeArgs() /var/www/core/vendor/spiral/hmvc/src/AbstractCore.php:115
112.1593 9976648 27. App\Controller\ProfileAnswerController->store() /var/www/core/vendor/spiral/hmvc/src/AbstractCore.php:115
112.1625 10044592 28. App\Repository\ProfileQuestionRepository->findByPK() /var/www/core/app/src/Controller/ProfileAnswerController.php:130
112.1633 10061752 29. Cycle\ORM\Select->fetchOne() /var/www/core/vendor/cycle/orm/src/Select/Repository.php:38
112.1810 10233040 30. Cycle\ORM\ORM->make() /var/www/core/vendor/cycle/orm/src/Select.php:350
112.1878 10507856 31. Cycle\ORM\RelationMap->init() /var/www/core/vendor/cycle/orm/src/ORM.php:146
112.1878 10507856 32. Cycle\ORM\Relation\BelongsTo->initPromise() /var/www/core/vendor/cycle/orm/src/RelationMap.php:65
112.1878 10508232 33. Cycle\ORM\ORM->promise() /var/www/core/vendor/cycle/orm/src/Relation/Traits/PromiseOneTrait.php:25
112.1879 10508232 34. Cycle\ORM\Promise\ProxyFactory->promise() /var/www/core/vendor/cycle/orm/src/ORM.php:303
112.2537 11461504 35. Cycle\ORM\Promise\Materizalizer\EvalMaterializer->materialize() /var/www/core/vendor/cycle/proxy-factory/src/ProxyFactory.php:94
Is there any documentation on this library? It is mentioned here: https://cycle-orm.dev/docs/advanced-promise that you can use it for resolving relation references, but what's not mentioned is that you cannot create a new object like new \Cycle\ORM\Promise\ProxyFactory()
, because there a few required arguments in the constructor. I found no info regarding how exactly this library is supposed to be used.
Looks like every proxy object instantiation leads to parsing original object.
Here it is call extract
method from in instantiate()
:
proxy-factory/src/Promise/ProxyFactory.php
Line 128 in 6ab6784
which internaly calls getExtendedMethodNodes()
which perform file read and parse:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.