/**
  * {@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();
 }
 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);
 }
예제 #3
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)));
                         }
                     }
                 }
             }
         }
     }
 }