/**
  * Test that constructor args were pulled properly
  *
  * Notie that we need to deuplicate the CamelCase conversion here for old
  * fashioned classes
  */
 public function testInitConstructorArgs()
 {
     $expectedConstructorArgs = array();
     $nameConverter = new CamelCaseToSnakeCaseNameConverter();
     foreach ($this->getConstructorArgs() as $param) {
         $expectedConstructorArgs[$nameConverter->denormalize($param->getName())] = $param;
     }
     $this->assertEquals($expectedConstructorArgs, $this->resolver->getConstructorArgs());
 }
 /**
  * Fetches constructor args (array of ReflectionParameter) from the reflected class
  * and set them as an associative array
  *
  * Convert the parameter names to camelCase for classes that have contructor
  * params defined in snake_case for consistency with the options
  */
 public function initConstructorArgs()
 {
     $constructor = $this->reflected->getConstructor();
     $nameConverter = new CamelCaseToSnakeCaseNameConverter();
     if (!is_null($constructor)) {
         // Index parameters by their names
         foreach ($constructor->getParameters() as $param) {
             $this->constructorArgs[$nameConverter->denormalize($param->getName())] = $param;
         }
     }
 }
 /**
  * Executes the command.
  *
  * @param InputInterface  $input
  * @param OutputInterface $output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $force = $input->getOption('force');
     $db_driver = $this->getContainer()->getParameter('nyroDev_utility.db_driver');
     $dirname = null;
     switch ($db_driver) {
         case 'orm':
             $dirname = 'Entity';
             break;
     }
     if ($dirname) {
         $sourceDir = realpath(__DIR__ . '/../Model/' . $dirname);
         $converter = new CamelCaseToSnakeCaseNameConverter();
         $dbService = $this->getContainer()->get('nyrocms_db');
         $namespace = $dbService->getNamespace();
         $originalNamespace = 'NyroDev\\NyroCmsBundle\\Model\\' . $dirname;
         $srcDir = dirname($this->getContainer()->getParameter('kernel.root_dir')) . '/src';
         $finder = new Finder();
         $sources = $finder->files()->name('*.php')->in($sourceDir);
         $fs = new Filesystem();
         foreach ($sources as $source) {
             /* @var $source SplFileInfo */
             $classname = lcfirst(substr($source->getBasename(), 0, -4));
             $classnameIdent = $converter->normalize($classname);
             $src = $source->getRealPath();
             $dstClass = $dbService->getClass($classnameIdent, false);
             $dst = str_replace(array('/', '\\'), array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), $srcDir . '/' . $namespace . '/' . $dstClass . '.php');
             $exists = $fs->exists($dst);
             if ($force || !$exists) {
                 $output->writeln(($exists ? 'Overwriting' : 'Writing') . ': ' . $dst);
                 $fs->dumpFile($dst, str_replace($originalNamespace, $namespace, file_get_contents($src)));
             } else {
                 $output->writeln('Exists: ' . $dst);
             }
         }
     } else {
         $output->writeln($db_driver . ' is not supported.');
     }
 }
Exemple #4
0
 /**
  * @param $modulePath
  * @param $moduleName
  * @param $modelName
  * @param array $fields
  * @return $this
  */
 protected function generateModelStructure($modulePath, $moduleName, $modelName, array $fields)
 {
     $model = Object::make(str_replace('/', '\\', "{$moduleName}/Model/{$modelName}"));
     $model->extend(Object::make('Magento\\Framework\\Model\\AbstractModel'));
     $model->implement(Contract::make(str_replace('/', '\\', "{$moduleName}/Api/Data/{$modelName}Interface")));
     $_constructBody = '        $this->_init(\'' . str_replace('/', '\\\\', "{$moduleName}/Model/ResourceModel/{$modelName}") . '\');';
     $model->addMethod(Method::make('_construct')->setBody($_constructBody)->setPhpdoc(MethodPhpdoc::make()->setDescription(new Description('Initialize resource model(s)'))));
     foreach ($fields as $fieldDefinition) {
         $fieldName = $fieldDefinition['name'];
         $fieldType = $fieldDefinition['type'];
         $model->addMethod(Method::make($this->converter->denormalize('set_' . $fieldName))->setPhpdoc(MethodPhpdoc::make()->addParameterTag(new ParameterTag($fieldType, 'value'))->setReturnTag(new ReturnTag($model->getName())))->setBody(str_repeat(' ', 8) . 'return $this->setData(self::' . strtoupper($fieldName) . ', $value);')->addArgument(new Argument($fieldType, 'value')));
         $model->addMethod(Method::make($this->converter->denormalize('get_' . $fieldName))->setBody(str_repeat(' ', 8) . 'return $this->getData(self::' . strtoupper($fieldName) . ');')->setPhpdoc(MethodPhpdoc::make()->setReturnTag(new ReturnTag($fieldType))));
     }
     $this->dumpObject($model, "{$modulePath}/Model/{$modelName}.php");
     return $this;
 }
 public function denormalize($propertyName)
 {
     return 'nameConverted' === $propertyName ? parent::denormalize($propertyName) : $propertyName;
 }
 /**
  * @dataProvider attributeProvider
  */
 public function testDenormalize($underscored, $lowerCamelCased)
 {
     $nameConverter = new CamelCaseToSnakeCaseNameConverter();
     $this->assertEquals($nameConverter->denormalize($underscored), $lowerCamelCased);
 }
 /**
  * Return option values indexed by name using camelCased keys
  *
  * @param  array  $options array of options
  * @return mixed[] array of options indexed by (camelCased) name
  */
 public static function optionsToCamelCase(array $options)
 {
     $optionsByName = array();
     if (count($options)) {
         $nameConverter = new CamelCaseToSnakeCaseNameConverter();
         foreach ($options as $name => $value) {
             $optionsByName[$nameConverter->denormalize($name)] = $value;
         }
     }
     return $optionsByName;
 }
 /**
  * Installs the data fixtures for a test case.
  *
  * @param string $managerName
  * @param boolean $append
  * @return boolean
  */
 protected function installDataFixtures($managerName, $append = false)
 {
     $_em = $this->get('doctrine')->getManager($managerName);
     // Reset any fixtures already managed for this entity manager.
     $this->fixtures[$managerName] = [];
     // Get all of the fixtures from the parameters.
     $fixtures = $this->getContainer()->getParameterBag()->all()['fixtures'];
     $_conn = $_em->getConnection();
     // Purge out old data first.
     if (!$append) {
         $entities = array_map(function ($fixture) {
             return $fixture['_entity'];
         }, array_reverse($fixtures));
         $entities = array_unique($entities);
         foreach ($entities as $entity) {
             $table = $_em->getClassMetadata($entity)->getTableName();
             // DELETEs are dramatically faster than TRUNCATEs,
             // at least for small tables in Postgres.
             $_conn->executeUpdate("DELETE FROM {$table}");
         }
     }
     // Create a converter to convert snake case to camelCase.
     $converter = new CamelCaseToSnakeCaseNameConverter();
     // Next, create the new fixtures.
     foreach ($fixtures as $ref => $fixture) {
         $refClass = new ReflectionClass($fixture['_entity']);
         $entity = $refClass->newInstance();
         foreach ($fixture as $field => $value) {
             // _entity is a special field so it is ignored.
             if ('_entity' !== $field) {
                 // If the value begins with a tilde, it references
                 // another entity that is hopefully already hydrated.
                 if (0 === strpos($value, '~')) {
                     $xref = substr($value, 1);
                     $value = null;
                     if (isset($this->fixtures[$managerName][$xref])) {
                         $value = $this->fixtures[$managerName][$xref];
                     }
                 }
                 // See if the value can be converted to a DateTime object
                 // because Doctrine expects an object rather than a string.
                 if (is_string($value)) {
                     $dateTime = date_create_from_format(self::DATETIME_FORMAT, $value);
                     if ($dateTime) {
                         $value = $dateTime;
                     }
                 }
                 // Construct the setter and call it.
                 $field = $converter->denormalize($field);
                 $setter = sprintf('set%s', ucwords($field));
                 $refMethod = new ReflectionMethod($entity, $setter);
                 $refMethod->invoke($entity, $value);
             }
         }
         // Immediately flush the fixture entity.
         $_em->persist($entity);
         $_em->flush();
         // And refresh it so cross relationships work.
         $_em->refresh($entity);
         $this->fixtures[$managerName][$ref] = $entity;
     }
     return true;
 }