public function generate(\ReflectionClass $original, PhpClass $proxy)
 {
     $writer = new Writer();
     // copy over all public methods
     foreach ($original->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
         if ($method->isStatic()) {
             continue;
         }
         $writer->reset()->write('return $this->delegate->')->write($method->name)->write('(');
         $first = true;
         foreach ($method->getParameters() as $param) {
             if (!$first) {
                 $writer->write(', ');
             }
             $first = false;
             $writer->write('$')->write($param->name);
         }
         $writer->write(');');
         $proxyMethod = PhpMethod::fromReflection($method)->setBody($writer->getContent());
         $proxy->setMethod($proxyMethod);
     }
     $proxy->setProperty(PhpProperty::create('delegate')->setVisibility('private'));
     $proxy->setProperty(PhpProperty::create('container')->setVisibility('private'));
     $proxy->setMethod(PhpMethod::create('__construct')->setVisibility('public')->addParameter(PhpParameter::create('objectManager')->setType('Doctrine\\Common\\Persistence\\ObjectManager'))->addParameter(PhpParameter::create('container')->setType('Symfony\\Component\\DependencyInjection\\ContainerInterface'))->setBody($writer->reset()->writeln('$this->delegate = $objectManager;')->writeln('$this->container = $container;')->getContent()));
     $proxy->setMethod(PhpMethod::fromReflection($original->getMethod('getRepository'))->setParameters(array(PhpParameter::create('className')))->setBody($writer->reset()->writeln('$repository = $this->delegate->getRepository($className);' . "\n")->writeln('if (null !== $metadata = $this->container->get("jms_di_extra.metadata.metadata_factory")->getMetadataForClass(get_class($repository))) {')->indent()->writeln('foreach ($metadata->classMetadata as $classMetadata) {')->indent()->writeln('foreach ($classMetadata->methodCalls as $call) {')->indent()->writeln('list($method, $arguments) = $call;')->writeln('call_user_func_array(array($repository, $method), $this->prepareArguments($arguments));')->outdent()->writeln('}')->outdent()->writeln('}')->outdent()->writeln('}' . "\n")->writeln('return $repository;')->getContent()));
     $proxy->setMethod(PhpMethod::create('prepareArguments')->setVisibility('private')->addParameter(PhpParameter::create('arguments')->setType('array'))->setBody($writer->reset()->writeln('$processed = array();')->writeln('foreach ($arguments as $arg) {')->indent()->writeln('if ($arg instanceof \\Symfony\\Component\\DependencyInjection\\Reference) {')->indent()->writeln('$processed[] = $this->container->get((string) $arg, $arg->getInvalidBehavior());')->outdent()->writeln('} else if ($arg instanceof \\Symfony\\Component\\DependencyInjection\\Parameter) {')->indent()->writeln('$processed[] = $this->container->getParameter((string) $arg);')->outdent()->writeln('} else {')->indent()->writeln('$processed[] = $arg;')->outdent()->writeln('}')->outdent()->writeln('}' . "\n")->writeln('return $processed;')->getContent()));
 }
 /**
  * @param string $methodName
  * @param string $methodBody
  * @param array  $methodArgs
  *
  * @return PhpMethod
  */
 protected function generateClassMethod($methodName, $methodBody, $methodArgs = [])
 {
     $writer = new Writer();
     $method = PhpMethod::create($methodName)->setBody($writer->write($methodBody)->getContent());
     if (count($methodArgs)) {
         foreach ($methodArgs as $arg) {
             $method->addParameter(PhpParameter::create($arg));
         }
     }
     return $method;
 }
 public function generate(Definition $def, $className, $targetPath)
 {
     $writer = new Writer();
     $parts = explode("\\", $className);
     $shortName = array_pop($parts);
     $writer->writeln("<?php\n");
     if ($parts) {
         $writer->writeln("namespace " . ltrim(implode("\\", $parts), "\\") . ";\n");
     }
     $writer->writeln('/**')->writeln(' * This code has been auto-generated by the JMSDiExtraBundle.')->writeln(' *')->writeln(' * Manual changes to it will be lost.')->writeln(' */')->writeln('class ' . $shortName . '__JMSInjector')->writeln('{')->indent()->writeln('public static function inject($container) {')->indent();
     if ($file = $def->getFile()) {
         require_once $file;
         $writer->write('require_once ');
         $relativePath = $this->relativizePath($targetPath, $file);
         if ($relativePath[0] === '.') {
             $writer->writeln('__DIR__ . ' . var_export('/' . $relativePath, true) . ';');
         } else {
             $writer->writeln(var_export($relativePath, true) . ';');
         }
     }
     foreach ($this->getInlineDefinitions($def) as $inlineDef) {
         $name = $this->nameGenerator->nextName();
         $this->inlinedDefinitions[$inlineDef] = $name;
         $writer->writeln('$' . $name . ' = new \\' . ltrim($inlineDef->getClass(), "\\") . $this->dumpArguments($inlineDef->getArguments()) . ';');
     }
     $writer->writeln('$instance = new \\' . ltrim($def->getClass(), "\\") . $this->dumpArguments($def->getArguments()) . ';');
     foreach ($def->getMethodCalls() as $call) {
         list($method, $arguments) = $call;
         $writer->writeln('$instance->' . $method . $this->dumpArguments($arguments) . ';');
     }
     $ref = new \ReflectionClass($def->getClass());
     foreach ($def->getProperties() as $property => $value) {
         $refProperty = $this->getReflectionProperty($ref, $property);
         if ($refProperty->isPublic()) {
             $writer->writeln('$instance->' . $property . ' = ' . $this->dumpValue($value) . ';');
         } else {
             $writer->writeln(sprintf("\$refProperty = new \\ReflectionProperty(%s, %s);", var_export($refProperty->getDeclaringClass()->getName(), true), var_export($property, true)))->writeln('$refProperty->setAccessible(true);')->writeln('$refProperty->setValue($instance, ' . $this->dumpValue($value) . ');');
         }
     }
     if (method_exists($def, 'getInitMethod') && $def->getInitMethod()) {
         $writer->writeln('$instance->' . $def->getInitMethod() . '();');
     }
     $writer->writeln('return $instance;')->outdent()->writeln('}')->outdent()->writeln('}');
     return $writer->getContent();
 }
예제 #4
0
 private static function export(\CG\Generator\Writer $writer, array &$visited, $value, $deep = 1, $counter = 0x3ffff)
 {
     $deep--;
     $counter--;
     if (is_object($value)) {
         if ($value instanceof \DateTime) {
             $writer->write(sprintf('\\DateTime(%s, %s)', $value->format("Y/m/d H:i:s"), $value->getTimezone()->getName()));
         } else {
             if ($value instanceof \DateTimeZone) {
                 $writer->write(sprintf('\\DateTimeZone(%s)', $value->getName()));
             } else {
                 if ($value instanceof \Doctrine\ORM\PersistentCollection) {
                     $writer->write(sprintf('\\Doctrine\\ORM\\PersistentCollection(%s, %s)', spl_object_hash($value), $value->getTypeClass()->getName()));
                 } else {
                     if ($value instanceof \Closure) {
                         $_rc = new \ReflectionFunction($value);
                         $writer->write(sprintf('\\Closure(%s, file:%s line:[%d,%s])', spl_object_hash($value), self::fixPath($_rc->getFileName()), $_rc->getStartLine(), $_rc->getEndLine()));
                     } else {
                         $oid = spl_object_hash($value);
                         $object_class = get_class($value);
                         if (isset($visited[$oid])) {
                             $writer->write(sprintf("#%s(%s)", $object_class, $oid));
                         } else {
                             $visited[$oid] = true;
                             if ($deep > 0) {
                                 $skip_properties = array();
                                 if ($value instanceof \Doctrine\ORM\Proxy\Proxy) {
                                     $skip_properties = array_merge(array('__initializer__', '__cloner__', '__isInitialized__'), $skip_properties);
                                 }
                                 $writer->write(sprintf("%s(%s) { \n", $object_class, $oid));
                                 $writer->indent();
                                 $r = new \ReflectionClass($object_class);
                                 $output = array();
                                 foreach ($r->getProperties() as $p) {
                                     if ($p->isStatic()) {
                                         continue;
                                     }
                                     if ($counter < 0) {
                                         $writer->writeln("......");
                                         break;
                                     }
                                     $_p = $p->getName();
                                     if (in_array($_p, $skip_properties)) {
                                         continue;
                                     }
                                     $p->setAccessible(true);
                                     $_value = $p->getValue($value);
                                     $writer->write($_p . ' : ');
                                     self::export($writer, $visited, $_value, $deep, $counter);
                                     $writer->write("\n");
                                 }
                                 $writer->outdent();
                                 $writer->write("}");
                             } else {
                                 $r = new \ReflectionClass($object_class);
                                 $output = array();
                                 foreach ($r->getProperties() as $p) {
                                     if (count($output) > 1) {
                                         break;
                                     }
                                     if ($p->isStatic()) {
                                         continue;
                                     }
                                     $p->setAccessible(true);
                                     $_value = $p->getValue($value);
                                     if (is_object($_value) || is_array($_value)) {
                                         continue;
                                     }
                                     $_p = $p->getName();
                                     if (0 === strpos($_p, '_')) {
                                         continue;
                                     }
                                     if (is_string($_value)) {
                                         if (strlen($_value) > 0xf) {
                                             $output[$_p] = substr($_value, 0xc) . '..';
                                         } else {
                                             $output[$_p] = $_value;
                                         }
                                     } else {
                                         $output[$_p] = $_value;
                                     }
                                 }
                                 $writer->write(sprintf("%s(%s)", $object_class, $oid));
                                 if (!empty($output)) {
                                     $writer->indent()->write(" = " . json_encode($output))->outdent();
                                 }
                             }
                         }
                     }
                 }
             }
         }
     } else {
         if (is_array($value)) {
             if ($deep > 0) {
                 $writer->writeln("array(");
                 $writer->indent();
                 foreach ($value as $_key => &$_value) {
                     if ($counter < 0) {
                         $writer->writeln("...");
                         break;
                     }
                     $writer->write($_key . ' => ');
                     self::export($writer, $visited, $_value, $deep, $counter);
                     $writer->write("\n");
                 }
                 $writer->outdent();
                 $writer->writeln(")");
             } else {
                 $writer->write(sprintf("array( length = %s ) ", count($value)));
             }
         } else {
             if (null === $value) {
                 $writer->write("null");
             } else {
                 if (is_string($value)) {
                     if (strlen($value) < 0x7f) {
                         $writer->write(var_export($value, 1));
                     } else {
                         $writer->write(var_export(substr($value, 0, 0x7f) . '...', 1));
                     }
                     $writer->write(sprintf("%d", strlen($value)));
                 } else {
                     if (is_bool($value)) {
                         $writer->write($value ? 'true' : 'false');
                     } else {
                         if (is_numeric($value)) {
                             $writer->write(var_export($value, 1));
                         } else {
                             $writer->write(sprintf("%s ( %s ) ", gettype($value), var_export($value, 1)));
                         }
                     }
                 }
             }
         }
     }
 }