Skip to content

Commit

Permalink
ServiceProxyFactory implemented but still failing
Browse files Browse the repository at this point in the history
  • Loading branch information
cordoval committed Nov 10, 2012
1 parent 6ca64c9 commit 169be0a
Show file tree
Hide file tree
Showing 6 changed files with 444 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock
.idea
cache
322 changes: 322 additions & 0 deletions s HeavyObject extends PHPPeruHeavyObject implements PHPPeru
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
diff --git a/src/PHPPeru/ClassMetadataInterface.php b/src/PHPPeru/ClassMetadataInterface.php
deleted file mode 100644
index 56bdbe8..0000000
--- a/src/PHPPeru/ClassMetadataInterface.php
+++ /dev/null
@@ -1,33 +0,0 @@
-<?php
-
-namespace PHPPeru;
-
-/**
- * Contract for a Service layer ClassMetadata class to implement.
- *
- * @author Luis Cordova Sosa <cordoval@gmail.com>
- * @author Fernando Paredes Garcia <fernando@develcuy.com>
- */
-interface ClassMetadataInterface
-{
- /**
- * Get fully-qualified class name of this service class.
- *
- * @return string
- */
- function getName();
-
- /**
- * Gets the service identifier.
- *
- * @return string
- */
- function getIdentifier();
-
- /**
- * Gets the ReflectionClass instance for this service class.
- *
- * @return \ReflectionClass
- */
- function getReflectionClass();
-}
diff --git a/src/PHPPeru/Examples.php b/src/PHPPeru/Examples.php
index d7559f9..73be71e 100644
--- a/src/PHPPeru/Examples.php
+++ b/src/PHPPeru/Examples.php
@@ -78,17 +78,12 @@ class Examples
$this->c['cache_dir'] = __DIR__.'/../../cache';
$this->c['phpperu_namespace'] = 'PHPPeru';

- $this->c['proxy_generator'] = function($c) {
- return new ProxyGenerator($c['cache_dir'], $c['phpperu_namespace']);
+ $this->c['proxy_factory'] = function($c) {
+ return new ServiceProxyFactory($c['cache_dir'], $c['phpperu_namespace']);
};

$this->c['heavy_object_proxy'] = function($c) {
- $factory = new ServiceProxyFactory($c['cache_dir'], $c['phpperu_namespace']);
- $factory->setProxyGenerator($c['proxy_generator']);
-
- $heavyObjectProxy = $factory->getProxy('HeavyObject', 'heavy_object');
-
- return $heavyObjectProxy;
+ return $c['proxy_factory']->getProxy("PHPPeru\\HeavyObject", "heavy_object");
};

$this->c['ioc_controller'] = function ($c) {
diff --git a/src/PHPPeru/ServiceClassMetadata.php b/src/PHPPeru/ServiceClassMetadata.php
index fd65b3a..5f0226a 100644
--- a/src/PHPPeru/ServiceClassMetadata.php
+++ b/src/PHPPeru/ServiceClassMetadata.php
@@ -2,7 +2,9 @@

namespace PHPPeru;

-class ServiceClassMetadata implements ClassMetadataInterface
+use Doctrine\Common\Persistence\Mapping\ClassMetadata;
+
+class ServiceClassMetadata implements ClassMetadata
{
protected $className;
protected $identifier;
@@ -18,7 +20,7 @@ class ServiceClassMetadata implements ClassMetadataInterface
*/
function getName()
{
- return $this->serviceClassName;
+ return $this->className;
}

/**
@@ -36,4 +38,108 @@ class ServiceClassMetadata implements ClassMetadataInterface
{
return new \ReflectionClass($this->className);
}
+
+ /**
+ * {@inheritDoc}
+ */
+ function isIdentifier($fieldName)
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function hasField($fieldName)
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function hasAssociation($fieldName)
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function isSingleValuedAssociation($fieldName)
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function isCollectionValuedAssociation($fieldName)
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function getFieldNames()
+ {
+ return array();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function getIdentifierFieldNames()
+ {
+ return array();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function getAssociationNames()
+ {
+ return array();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function getTypeOfField($fieldName)
+ {
+ return 'type_of_field_dummy';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function getAssociationTargetClass($assocName)
+ {
+ return 'association_target_class_dummy';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function isAssociationInverseSide($assocName)
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function getAssociationMappedByTargetField($assocName)
+ {
+ return 'target_field_dummy';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ function getIdentifierValues($object)
+ {
+ return array();
+ }
}
\ No newline at end of file
diff --git a/src/PHPPeru/ServiceProxyFactory.php b/src/PHPPeru/ServiceProxyFactory.php
index c14b546..05fcabe 100644
--- a/src/PHPPeru/ServiceProxyFactory.php
+++ b/src/PHPPeru/ServiceProxyFactory.php
@@ -37,8 +37,8 @@ class ServiceProxyFactory
*/
public function __construct($proxyDir, $proxyNamespace)
{
- $this->proxyDir = $proxyDir;
- $this->proxyNs = $proxyNamespace;
+ $this->proxyDir = $proxyDir;
+ $this->proxyNamespace = $proxyNamespace;
}

/**
@@ -52,20 +52,18 @@ class ServiceProxyFactory
*/
public function getProxy($className, $identifier)
{
- $fqn = ClassUtils::generateProxyClassName($className, $this->proxyNs);
+ $fqn = ClassUtils::generateProxyClassName($className, $this->proxyNamespace);

if ( ! class_exists($fqn, false)) {
$generator = $this->getProxyGenerator();
$fileName = $generator->getProxyFileName($className);
- $classMetadata = new ServiceClassMetaData($className, $identifier);
+ $classMetadata = new ServiceClassMetadata($className, $identifier);
$generator->generateProxyClass($classMetadata);

require $fileName;
}

- $entityPersister = $this->uow->getEntityPersister($className);
-
- $initializer = function (Proxy $proxy) use ($entityPersister, $identifier) {
+ $initializer = function (Proxy $proxy) {
$proxy->__setInitializer(function () {});
$proxy->__setCloner(function () {});

@@ -86,70 +84,23 @@ class ServiceProxyFactory
if (method_exists($proxy, '__wakeup')) {
$proxy->__wakeup();
}
-
- if (null === $entityPersister->load($identifier, $proxy)) {
- throw new \Doctrine\ORM\EntityNotFoundException();
- }
};

- $cloner = function (Proxy $proxy) use ($entityPersister, $identifier) {
+ $cloner = function (Proxy $proxy) {
if ($proxy->__isInitialized()) {
return;
}

$proxy->__setInitialized(true);
- $proxy->__setInitializer(function () {});
- $class = $entityPersister->getClassMetadata();
- $original = $entityPersister->load($identifier);
-
- if (null === $original) {
- throw new \Doctrine\ORM\EntityNotFoundException();
- }
+ $proxy->__setInitializer(function (){});

- foreach ($class->getReflectionClass()->getProperties() as $reflectionProperty) {
- $propertyName = $reflectionProperty->getName();
-
- if ($class->hasField($propertyName) || $class->hasAssociation($propertyName)) {
- $reflectionProperty->setAccessible(true);
- $reflectionProperty->setValue($proxy, $reflectionProperty->getValue($original));
- }
- }
+ return;
};

return new $fqn($initializer, $cloner, $identifier);
}

/**
- * Generates proxy classes for all given classes.
- *
- * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata[] $classes The classes (ClassMetadata instances)
- * for which to generate proxies.
- * @param string $proxyDir The target directory of the proxy classes. If not specified, the
- * directory configured on the Configuration of the EntityManager used
- * by this factory is used.
- * @return int Number of generated proxies.
- */
- public function generateProxyClasses(array $classes, $proxyDir = null)
- {
- $generated = 0;
-
- foreach ($classes as $class) {
- /* @var $class \Doctrine\ORM\Mapping\ClassMetadataInfo */
- if ($class->isMappedSuperclass || $class->getReflectionClass()->isAbstract()) {
- continue;
- }
-
- $generator = $this->getProxyGenerator();
-
- $proxyFileName = $generator->getProxyFileName($class->getName(), $proxyDir);
- $generator->generateProxyClass($class, $proxyFileName);
- $generated += 1;
- }
-
- return $generated;
- }
-
- /**
* @param ProxyGenerator $proxyGenerator
*/
public function setProxyGenerator(ProxyGenerator $proxyGenerator)
@@ -163,8 +114,8 @@ class ServiceProxyFactory
public function getProxyGenerator()
{
if (null === $this->proxyGenerator) {
- $this->proxyGenerator = new ProxyGenerator($this->proxyDir, $this->proxyNs);
- $this->proxyGenerator->setPlaceholder('<baseProxyInterface>', 'Doctrine\ORM\Proxy\Proxy');
+ $this->proxyGenerator = new ProxyGenerator($this->proxyDir, $this->proxyNamespace);
+ $this->proxyGenerator->setPlaceholder('<baseProxyInterface>', 'PHPPeru');
}

return $this->proxyGenerator;
33 changes: 0 additions & 33 deletions src/PHPPeru/ClassMetadataInterface.php

This file was deleted.

11 changes: 3 additions & 8 deletions src/PHPPeru/Examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,12 @@ public function goodAndFastAndAutomaticExample()
$this->c['cache_dir'] = __DIR__.'/../../cache';
$this->c['phpperu_namespace'] = 'PHPPeru';

$this->c['proxy_generator'] = function($c) {
return new ProxyGenerator($c['cache_dir'], $c['phpperu_namespace']);
$this->c['proxy_factory'] = function($c) {
return new ServiceProxyFactory($c['cache_dir'], $c['phpperu_namespace']);
};

$this->c['heavy_object_proxy'] = function($c) {
$factory = new ServiceProxyFactory($c['cache_dir'], $c['phpperu_namespace']);
$factory->setProxyGenerator($c['proxy_generator']);

$heavyObjectProxy = $factory->getProxy('HeavyObject', 'heavy_object');

return $heavyObjectProxy;
return $c['proxy_factory']->getProxy("PHPPeru\\HeavyObject", array("heavy_object"));
};

$this->c['ioc_controller'] = function ($c) {
Expand Down
Loading

0 comments on commit 169be0a

Please sign in to comment.