Exemplo n.º 1
0
 public function sample()
 {
     $file = File::make('Memio.php')->setStructure(Object::make('name\\space\\Sample')->addProperty(Property::make('string')->makePublic()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(VariableTag::make('string String'))))->addMethod(Method::make('get')->setPhpdoc(MethodPhpdoc::make()->setDescription(Description::make('Return string'))->setReturnTag(ReturnTag::make('string')))->setBody('return $this->string;'))->addMethod(Method::make('set')->setPhpdoc(MethodPhpdoc::make()->setDescription(Description::make('Set string'))->addParameterTag(ParameterTag::make('string', 'string', 'String'))->setReturnTag(ReturnTag::make('$this')))->addArgument(Argument::make('string', 'string'))->setBody('$this->string = $string;' . PHP_EOL . 'return $this;')));
     // Generate the code and display in the console
     $prettyPrinter = Build::prettyPrinter();
     $generatedCode = $prettyPrinter->generateCode($file);
     file_put_contents('tmp/origin/Memio.php', (string) $generatedCode);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $file = $input->getArgument('file');
     $entity = $input->getArgument('entity');
     $entityDir = $input->getArgument('entity-dir');
     if (!file_exists($file)) {
         throw new \InvalidArgumentException("{$file} does not exist");
     }
     if (!is_dir($entityDir)) {
         throw new \InvalidArgumentException("{$entityDir} is not a valid directory");
     }
     $item = json_decode(file_get_contents($file))[0];
     $file = File::make($entityDir . '/' . $entity . '.php');
     $object = Object::make('Dandomain\\Api\\Entity\\' . $entity);
     $arrayProperties = [];
     foreach ($item as $key => $val) {
         if (gettype($val) == 'array') {
             $arrayProperties[] = $key;
         }
     }
     if (count($arrayProperties)) {
         $body = [];
         foreach ($arrayProperties as $arrayProperty) {
             $body[] = '        $this->' . $arrayProperty . ' = new ArrayCollection();';
         }
         $object->addMethod(Method::make('__construct')->setBody(join("\n", $body)));
         $file->addFullyQualifiedName(new FullyQualifiedName('Doctrine\\Common\\Collections\\ArrayCollection'));
     }
     foreach ($item as $key => $val) {
         $type = gettype($val);
         if ($type == 'array') {
             $type = 'ArrayCollection';
         }
         $property = Property::make($key)->makeProtected()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag($type)));
         $object->addProperty($property);
         $object->addMethod(Method::make('get' . ucfirst($key))->setPhpdoc(MethodPhpdoc::make()->setReturnTag(new ReturnTag($type)))->setBody('        return $this->' . $key . ';'));
         $object->addMethod(Method::make('set' . ucfirst($key))->setPhpdoc(MethodPhpdoc::make()->addParameterTag(new ParameterTag($type, $key))->setReturnTag(new ReturnTag($entity)))->addArgument(Argument::make('string', $key))->setBody('        $this->' . $key . ' = $' . $key . ';'));
     }
     $file->setStructure($object);
     $prettyPrinter = Build::prettyPrinter();
     $generatedCode = $prettyPrinter->generateCode($file);
     file_put_contents($file->getFilename(), $generatedCode);
 }
Exemplo n.º 3
0
 public function testWithPhpdoc()
 {
     $property = Property::make('myClass')->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag('Memio\\Memio\\MyClass')));
     $generatedCode = $this->prettyPrinter->generateCode($property);
     $this->assertExpectedCode($generatedCode);
 }
Exemplo n.º 4
0
    protected function generateRepositoryModel($modulePath, $moduleName, $modelName, array $fields)
    {
        $resourceModelName = basename($modelName) . 'Resource';
        $resourceModelFqn = str_replace('/', '\\', "{$moduleName}/Model/ResourceModel/{$modelName}");
        $collectionFactoryName = 'CollectionFactory';
        $collectionFactoryFqn = str_replace('/', '\\', "{$moduleName}/Model/ResourceModel/{$modelName}/CollectionFactory");
        $searchResultsFactoryName = basename($modelName) . 'SearchResultsInterfaceFactory';
        $searchResultsFactoryFqn = str_replace('/', '\\', "{$moduleName}/Api/Data/{$modelName}SearchResultsInterfaceFactory");
        $searchResultsContractName = basename($modelName) . 'SearchResultsInterface';
        $searchResultsContractFqn = str_replace('/', '\\', "{$moduleName}/Api/Data/{$modelName}SearchResultsInterface");
        $dataContractName = basename($modelName) . 'Interface';
        $dataContractFqn = str_replace('/', '\\', "{$moduleName}/Api/Data/{$modelName}Interface");
        $dataFactoryName = basename($modelName) . 'InterfaceFactory';
        $dataFactoryFqn = str_replace('/', '\\', "{$moduleName}/Api/Data/{$modelName}InterfaceFactory");
        $factoryName = basename($modelName) . 'Factory';
        $model = Object::make(str_replace('/', '\\', "{$moduleName}/Model/{$modelName}Repository"));
        $model->implement(Contract::make(str_replace('/', '\\', "{$moduleName}/Api/{$modelName}RepositoryInterface")));
        $__constructBody = '        $this->resource             = $resource;
        $this->factory              = $factory;
        $this->collectionFactory    = $collectionFactory;
        $this->searchResultsFactory = $searchResultsFactory;
        $this->dataFactory          = $dataFactory;
        $this->dataObjectHelper     = $dataObjectHelper;
        $this->dataObjectProcessor  = $dataObjectProcessor;
        $this->storeManager         = $storeManager;';
        $model->addProperty(Property::make('resource')->makeProtected()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag($resourceModelName))));
        $model->addProperty(Property::make('factory')->makeProtected()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag($factoryName))));
        $model->addProperty(Property::make('collectionFactory')->makeProtected()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag($collectionFactoryName))));
        $model->addProperty(Property::make('searchResultsFactory')->makeProtected()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag($searchResultsFactoryName))));
        $model->addProperty(Property::make('dataFactory')->makeProtected()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag($dataFactoryName))));
        $model->addProperty(Property::make('dataObjectHelper')->makeProtected()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag('DataObjectHelper'))));
        $model->addProperty(Property::make('dataObjectProcessor')->makeProtected()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag('DataObjectProcessor'))));
        $model->addProperty(Property::make('storeManager')->makeProtected()->setPhpdoc(PropertyPhpdoc::make()->setVariableTag(new VariableTag('StoreManagerInterface'))));
        $model->addMethod(Method::make('__construct')->setBody($__constructBody)->addArgument(new Argument($resourceModelName, 'resource'))->addArgument(new Argument($factoryName, 'factory'))->addArgument(new Argument($collectionFactoryName, 'collectionFactory'))->addArgument(new Argument($searchResultsFactoryName, 'searchResultsFactory'))->addArgument(new Argument($dataFactoryName, 'dataFactory'))->addArgument(new Argument('DataObjectHelper', 'dataObjectHelper'))->addArgument(new Argument('DataObjectProcessor', 'dataObjectProcessor'))->addArgument(new Argument('StoreManagerInterface', 'storeManager'))->setPhpdoc(MethodPhpdoc::make()->addParameterTag(new ParameterTag($resourceModelName, 'resource'))->addParameterTag(new ParameterTag($factoryName, 'factory'))->addParameterTag(new ParameterTag($collectionFactoryName, 'collectionFactory'))->addParameterTag(new ParameterTag($searchResultsFactoryName, 'searchResultsFactory'))->addParameterTag(new ParameterTag($dataFactoryName, 'dataFactory'))->addParameterTag(new ParameterTag('DataObjectHelper', 'dataObjectHelper'))->addParameterTag(new ParameterTag('DataObjectProcessor', 'dataObjectProcessor'))->addParameterTag(new ParameterTag('StoreManagerInterface', 'storeManager'))));
        $model->addMethod(Method::make('getById')->setBody('        $object = $this->factory->create();
        $this->resource->load($object, $id);
        if (!$object->getId()) {
            throw new NoSuchEntityException("Object not found");
        }

        return $object;')->addArgument(new Argument('int', 'id'))->setPhpdoc(MethodPhpdoc::make()->addParameterTag(new ParameterTag('int', 'id'))->setReturnTag(new ReturnTag($dataContractName))->addThrowTag(new ThrowTag('NoSuchEntityException'))));
        $model->addMethod(Method::make('save')->setBody('        $this->resource->save($object);

        return $object;')->addArgument(new Argument($dataContractName, 'object'))->setPhpdoc(MethodPhpdoc::make()->addParameterTag(new ParameterTag($dataContractName, 'object'))->setReturnTag(new ReturnTag($dataContractName))));
        $getListBody = '        $searchResults = $this->searchResultsFactory->create();
        $searchResults->setSearchCriteria($criteria);

        $collection = $this->collectionFactory->create();
        foreach ($criteria->getFilterGroups() as $filterGroup) {
            foreach ($filterGroup->getFilters() as $filter) {
                $condition = $filter->getConditionType() ?: \'eq\';
                $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
            }
        }
        $searchResults->setTotalCount($collection->getSize());
        $sortOrders = $criteria->getSortOrders();
        if ($sortOrders) {
            /** @var SortOrder $sortOrder */
            foreach ($sortOrders as $sortOrder) {
                $collection->addOrder(
                    $sortOrder->getField(),
                    ($sortOrder->getDirection() == SortOrder::SORT_ASC) ? \'ASC\' : \'DESC\'
                );
            }
        }
        $collection->setCurPage($criteria->getCurrentPage());
        $collection->setPageSize($criteria->getPageSize());
        $objects = [];
        /** @var ' . $dataContractName . ' $object */
        foreach ($collection as $object) {
            $data = $this->dataFactory->create();
            $this->dataObjectHelper->populateWithArray(
                $data,
                $object->getData(),
                \'' . $dataContractFqn . '\'
            );
            $objects[] = $this->dataObjectProcessor->buildOutputDataArray(
                $data,
                \'' . $dataContractFqn . '\'
            );
        }
        $searchResults->setItems($objects);

        return $searchResults;';
        $model->addMethod(Method::make('getList')->setBody($getListBody)->addArgument(new Argument('SearchCriteriaInterface', 'criteria'))->setPhpdoc(MethodPhpdoc::make()->addParameterTag(new ParameterTag('SearchCriteriaInterface', 'criteria'))->setReturnTag(new ReturnTag($searchResultsContractName))));
        $model->addMethod(Method::make('delete')->setBody('        $this->resource->delete($object);

        return true;')->addArgument(new Argument($dataContractName, 'object'))->setPhpdoc(MethodPhpdoc::make()->addParameterTag(new ParameterTag($dataContractName, 'object'))->setReturnTag(new ReturnTag('bool'))));
        $model->addMethod(Method::make('deleteById')->setBody('        return $this->resource->delete($this->getById($id));')->addArgument(new Argument('int', 'id'))->setPhpdoc(MethodPhpdoc::make()->addParameterTag(new ParameterTag('int', 'id'))->setReturnTag(new ReturnTag('bool'))));
        $this->dumpObject($model, "{$modulePath}/Model/{$modelName}Repository.php", [[$resourceModelName => $resourceModelFqn], $collectionFactoryFqn, $searchResultsFactoryFqn, $dataFactoryFqn, $dataContractFqn, $searchResultsContractFqn, 'Magento\\Framework\\Api\\DataObjectHelper', 'Magento\\Framework\\Reflection\\DataObjectProcessor', 'Magento\\Framework\\Api\\SortOrder', 'Magento\\Store\\Model\\StoreManagerInterface', 'Magento\\Framework\\Exception\\NoSuchEntityException', 'Magento\\Framework\\Api\\SearchCriteriaInterface']);
        return $this;
    }