protected function execute(InputInterface $input, OutputInterface $output) { $pieces = array(); /** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */ $dialog = $this->getHelper('dialog'); $end = $input->getArgument('end-line'); $start = $input->getArgument('start-line'); $lines = file($input->getArgument('filename')); $slice = array_slice($lines, $start - 1, $end - $start + 1); $slice = join("", $slice); if ($input->getOption('pieces')) { // only needed when we assign pieces $output->writeln('<comment>Analyzing slice:</comment>'); $output->writeln($slice); list($pieces, $slice) = $this->processPieces($output, $dialog, $slice); } $code = new Writer(); if (!$input->getOption('function')) { foreach (array_keys($pieces) as $piece) { $code->writeln("{$piece} = null;"); } } $variable = $input->getOption('variable'); if ($input->getOption('heredoc')) { $code->writeln($this->escapeHeredoc($variable, $slice)); } else { $code->writeln($this->escapeArray($variable, $slice)); } if (null !== $input->getOption('function')) { $function = new PhpFunction($input->getOption('function')); foreach ($pieces as $piece) { $function->addParameter(new PhpParameter($piece)); } $code->writeln("return \${$variable};"); $function->setBody($code->getContent()); $visitor = new DefaultVisitor(); $visitor->visitFunction($function); $content = $visitor->getContent(); $content = preg_replace_callback('/EOF\\n.*EOF;\\n/ms', function ($m) { return preg_replace('/^\\ /ms', '', $m[0]); }, $content); } else { $content = $code->getContent(); } if ($input->getOption('pieces')) { $content = str_replace(md5(__CLASS__), '$', $content); } $output->writeln("\n{$content}"); if ($input->getOption('copy')) { $content = escapeshellarg(strtr($content, array("\t" => '\\t', '\\' => '\\\\'))); `echo {$content} | pbcopy`; } }
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())); }
/** * {@inheritdoc} */ protected function prepareData(ClassMetadata $metadata, array $data) { $writer = new Writer(); $writer->writeln(sprintf('$fixtures[\'%s\'] = array(', $metadata->getName())); foreach ($data as $key => $values) { $writer->indent()->writeln(sprintf("'%s' => array(", $key))->indent(); $arrayValues = array(); foreach ($values as $k => $value) { $arrayValues[] = sprintf("'%s' => %s", $k, $this->convertValue($value)); } $writer->writeln(implode(",\n", $arrayValues))->outdent()->writeln("),")->outdent(); } $writer->writeln(');'); return $writer->getContent(); }
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(); }
/** * Creates a new enhanced class * * @throws \RuntimeException * @return string */ public final function generateClass() { static $docBlock; if (empty($docBlock)) { $writer = new Writer(); $writer->writeln('/**')->writeln(' * CG library enhanced proxy class.')->writeln(' *')->writeln(' * This code was generated automatically by the CG library, manual changes to it')->writeln(' * will be lost upon next generation.')->writeln(' */')->writeln(''); $docBlock = $writer->getContent() . $this->class->getDocComment() . "\n"; } $this->generatedClass = PhpClass::create()->setDocblock($docBlock)->setParentClassName($this->class->name); $proxyClassName = $this->getClassName($this->class); if (false === strpos($proxyClassName, NamingStrategyInterface::SEPARATOR)) { throw new \RuntimeException(sprintf('The proxy class name must be suffixed with "%s" and an optional string, but got "%s".', NamingStrategyInterface::SEPARATOR, $proxyClassName)); } $this->generatedClass->setName($proxyClassName); foreach ($this->aspectGenerators as $generator) { $generator->generate($this->class, $this->generatedClass); } return $this->generateCode($this->generatedClass); }
/** * Creates a new enhanced class * * @return string */ public final function generateClass() { static $docBlock; if (empty($docBlock)) { $writer = new Writer(); $writer->writeln('/**')->writeln(' * CG library enhanced proxy class.')->writeln(' *')->writeln(' * This code was generated automatically by the CG library, manual changes to it')->writeln(' * will be lost upon next generation.')->writeln(' */'); $docBlock = $writer->getContent(); } $this->generatedClass = PhpClass::create()->setDocblock($docBlock)->setParentClassName($this->class->name); $proxyClassName = $this->getClassName($this->class); if (false === strpos($proxyClassName, NamingStrategyInterface::SEPARATOR)) { throw new \RuntimeException(sprintf('The proxy class name must be suffixed with "%s" and an optional string, but got "%s".', NamingStrategyInterface::SEPARATOR, $proxyClassName)); } $this->generatedClass->setName($proxyClassName); if (!empty($this->interfaces)) { $this->generatedClass->setInterfaceNames(array_map(function ($v) { return '\\' . $v; }, $this->interfaces)); foreach ($this->getInterfaceMethods() as $method) { $method = PhpMethod::fromReflection($method); $method->setAbstract(false); $this->generatedClass->setMethod($method); } } if (!empty($this->generators)) { foreach ($this->generators as $generator) { $generator->generate($this->class, $this->generatedClass); } } return $this->generateCode($this->generatedClass); }
protected function addDependentFixtureInterface(PhpClass $class, ClassMetadata $metadata, array $options) { $class->addInterfaceName('Doctrine\\Common\\DataFixtures\\DependentFixtureInterface'); $writer = new Writer(); $method = PhpMethod::create('getDependencies'); $writer->writeln("return array("); $associations = array(); foreach ($metadata->getAssociationNames() as $assocName) { $targetClass = $metadata->getAssociationTargetClass($assocName); $associations[] = sprintf("'%s\\%s'", $options['namespace'], $this->namingStrategy->fixtureName($this->getManager()->getClassMetadata($targetClass))); } $writer->indent(); $writer->writeln(implode(",\n", $associations)); $writer->outdent(); $writer->writeln(");"); $method->setBody($writer->getContent()); $class->setMethod($method); }