-
-
Notifications
You must be signed in to change notification settings - Fork 291
[DCOM-96] ProxyFactory logic moved to doctrine common #168
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
Conversation
Just curious, have you thought about using my cg-library? This should allow you to save a lot of code. I'm using it for generating all sorts of proxies in Symfony2. |
@schmittjoh never tried it, but I don't know if we can include it here as a dependency... |
@beberlei's goal is to replace the code generation in Doctrine to use https://github.com/beberlei/DoctrineCodeGenerator so it could probably be used for proxies too |
* @static | ||
* @return self | ||
*/ | ||
public static function proxyDirectoryRequired() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CS issue :)
@stof not sure it should be achieved immediately, but yes, that would be a nice one :) |
* @param $class | ||
* @return string | ||
*/ | ||
private function _generateSleep(ClassMetadata $class) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove the _
. It was part of the doctrine1 CS (used for the early development of doctrine2 too) but not of the current ones anymore (new code does not use it anymore, and private methods are renamed when the code is modified in existing classes.
@Ocramius my link to the CodeGenerator was mainly a reply to the CG suggestion |
like i said on IRC yesterday .. i would really like to retain the ability to map public properties: |
Onl Am 22.07.2012 um 02:00 schrieb Christophe [email protected]:
|
Only userland code generation, not our own. Am 22.07.2012 um 02:00 schrieb Christophe [email protected]:
|
@lsmith77 I am quite against public properties myself in my entities. Having |
|
||
$className = str_replace('\\', '', substr($className, strlen($proxyNamespace) +1)); | ||
|
||
return $proxyDir . DIRECTORY_SEPARATOR . $className.'.php'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing spaces around the last .
@guilhermeblanco could you check this one and the related PRs so that I can remove all the references to my forks? :) |
|
||
if ($this->isShortIdentifierGetter($method, $class)) { | ||
$identifier = lcfirst(substr($name, 3)); | ||
$cast = in_array($class->getTypeOfField($identifier), array('integer', 'smallint')) ? '(int) ' : ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align =
signs
I added a few comments at the end. |
@guilhermeblanco merge |
|
[DCOM-96] ProxyFactory logic moved to doctrine common
DCOM-96 compliance - proxy generation as of doctrine/common#168
Initial requirements
ClassMetadata
definition is providedSample Proxy
See this example generated proxy class
Public properties lazy loading
Public properties lazy loading has been implemented, and I've opened php/php-src#228 to protect the trick discovered by @lsmith77.
Mapped public fields and associations will trigger lazy loading through
__get
,__set
and__isset
.Transient properties and identifier fields won't trigger lazy loading. Same applies for simple identifier getters (as was already happening in ORM since 2.2). I've used the
ClassMetadata
interface to determine fields and associations to lazy load.New Proxy interface (not a BC Break)
Lazy loading via closures
I came up with the idea of using closures to lazy load proxies. This allows removal of any reference to the
OjectManager
, and makes it possible to dump Proxy objects as long as there are not circular references between them (XDebug solves this).The default constructor as of the current proxy generator implementation has following signature:
When used by the Proxy, the initializer will be called with object itself as first parameter, the called method name (the one that triggered lazy loading) as second parameter and the parameters that were passed to that method as third parameter.
This allows any of the current implementation to define an initialization logic (even outside the scope of persistence), and to eventually define some finer grained logic to be dispatched for LOB fields or getters/setters.
Proxy generation
Generating a proxy requires that a valid instance of
Doctrine\Common\Persistence\Mapping\ClassMetadata
is passed to theDoctrine\Common\Proxy\ProxyGenerator
. A proxy namespace and a proxy directory are also required to build theProxyGenerator
itself.The generator uses a template and a list of placeholders mapped to either strings or callables. Those placeholders are used in the template while generating the Proxy class. Every callable mapped as a placeholder is called with the class metadata as first parameter. This should allow some fine-grained customization when the defaults of the proxy generator are not enough.
Autoloading
Autoloader for proxies has been simply copied from the ORM.
Testing
I tested each case that I could come up with. I need suggestions on edge cases that should be documented (like above, when I mentioned setting public properties and the PHP bug).
Directions for MongoDB ODM/PHPCR implementors
I've opened following related PRs:
Performance
Doctrine\Common\Reflection\RuntimePublicReflectionProperty
, which is a workaround for PHP Bug 63463. It could also be possible to speed up hydration of proxies by using bulk reflection operation.That said, allover performance and memory usage is kept almost at same levels (sometimes even faster), but the new feature is introduced.