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`; } }
/** * {@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 testVisitFunction() { $writer = new Writer(); $function = new PhpFunction(); $function->setName('foo')->addParameter(PhpParameter::create('a'))->addParameter(PhpParameter::create('b'))->setBody($writer->writeln('if ($a === $b) {')->indent()->writeln('throw new \\InvalidArgumentException(\'$a is not allowed to be the same as $b.\');')->outdent()->write("}\n\n")->write('return $b;')->getContent()); $visitor = new DefaultVisitor(); $visitor->visitFunction($function); $this->assertEquals($this->getContent('a_b_function.php'), $visitor->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); }
public function testGetUnindentedDocComment() { $writer = new Writer(); $comment = $writer->writeln('/**')->indent()->writeln(' * Foo.')->write(' */')->getContent(); $this->assertEquals("/**\n * Foo.\n */", ReflectionUtils::getUnindentedDocComment($comment)); }
public function generate(Definition $def, $className) { $writer = new Writer(); $parts = explode("\\", $className); $shortName = array_pop($parts); $writer->writeln("<?php\n"); if ($parts) { $writer->writeln("namespace " . 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()) { $writer->writeln('require_once ' . var_export($file, true) . ';'); require_once $file; } foreach ($this->getInlineDefinitions($def) as $inlineDef) { $name = $this->nameGenerator->nextName(); $this->inlinedDefinitions[$inlineDef] = $name; $writer->writeln('$' . $name . ' = new \\' . $inlineDef->getClass() . $this->dumpArguments($inlineDef->getArguments()) . ';'); } $writer->writeln('$instance = new \\' . $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 * * @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); }
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))); } } } } } } }