Ejemplo n.º 1
0
 /**
  * Generate view content
  *
  * @param string          $moduleName
  * @param ClassReflection $loadedEntity
  */
 protected function addContent($moduleName, ClassReflection $loadedEntity)
 {
     // prepare some params
     $moduleIdentifier = $this->filterCamelCaseToUnderscore($moduleName);
     $entityName = $loadedEntity->getShortName();
     $entityParam = lcfirst($entityName);
     $listParam = lcfirst(str_replace('Entity', '', $entityName)) . 'List';
     $moduleRoute = $this->filterCamelCaseToDash($moduleName);
     // set action body
     $body = [];
     $body[] = 'use ' . $loadedEntity->getName() . ';';
     $body[] = '';
     $body[] = '$this->h1(\'' . $moduleIdentifier . '_title_index\');';
     $body[] = '?>';
     $body[] = '<table class="table table-bordered table-striped">';
     $body[] = '    <thead>';
     $body[] = '    <tr>';
     foreach ($loadedEntity->getProperties() as $property) {
         $body[] = '        <th><?php echo $this->translate(\'' . $moduleIdentifier . '_label_' . $this->filterCamelCaseToUnderscore($property->getName()) . '\'); ?></th>';
     }
     $body[] = '        <th width="95">';
     $body[] = '            <a class="btn btn-default btn-xs" href="<?php echo $this->url(\'' . $moduleRoute . '/create\'); ?>" title="<?php echo $this->translate(\'' . $moduleIdentifier . '_action_create\'); ?>">';
     $body[] = '                <i class="fa fa-plus"></i>';
     $body[] = '            </a>';
     $body[] = '        </th>';
     $body[] = '    </tr>';
     $body[] = '    </thead>';
     $body[] = '    <tbody>';
     $body[] = '    <?php /** @var ' . $entityName . ' $' . $entityParam . ' */ ?>';
     $body[] = '    <?php foreach ($this->' . $listParam . ' as $' . $entityParam . '): ?>';
     $body[] = '        <tr>';
     foreach ($loadedEntity->getProperties() as $property) {
         $methodName = 'get' . ucfirst($property->getName());
         $body[] = '            <td><?php echo $' . $entityParam . '->' . $methodName . '() ?></td>';
     }
     $body[] = '            <td>';
     $body[] = '                <a class="btn btn-default btn-xs" href="<?php echo $this->url(\'' . $moduleRoute . '/show\', [\'id\' => $' . $entityParam . '->getIdentifier()]); ?>" title="<?php echo $this->translate(\'' . $moduleIdentifier . '_action_show\'); ?>">';
     $body[] = '                    <i class="fa fa-search"></i>';
     $body[] = '                </a>';
     $body[] = '                <a class="btn btn-default btn-xs" href="<?php echo $this->url(\'' . $moduleRoute . '/update\', [\'id\' => $' . $entityParam . '->getIdentifier()]); ?>" title="<?php echo $this->translate(\'' . $moduleIdentifier . '_action_update\'); ?>">';
     $body[] = '                    <i class="fa fa-pencil"></i>';
     $body[] = '                </a>';
     $body[] = '                <a class="btn btn-default btn-xs" href="<?php echo $this->url(\'' . $moduleRoute . '/delete\', [\'id\' => $' . $entityParam . '->getIdentifier()]); ?>" title="<?php echo $this->translate(\'' . $moduleIdentifier . '_action_delete\'); ?>">';
     $body[] = '                    <i class="fa fa-trash"></i>';
     $body[] = '                </a>';
     $body[] = '            </td>';
     $body[] = '        </tr>';
     $body[] = '    <?php endforeach; ?>';
     $body[] = '    </tbody>';
     $body[] = '</table>';
     $body = implode(AbstractGenerator::LINE_FEED, $body);
     // add method
     $this->setContent($body);
 }
Ejemplo n.º 2
0
 /**
  * Build a Code Generation Php Object from a Class Reflection
  *
  * @param  ClassReflection $classReflection
  * @return TraitGenerator
  */
 public static function fromReflection(ClassReflection $classReflection)
 {
     // class generator
     $cg = new static($classReflection->getName());
     $cg->setSourceContent($cg->getSourceContent());
     $cg->setSourceDirty(false);
     if ($classReflection->getDocComment() != '') {
         $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock()));
     }
     // set the namespace
     if ($classReflection->inNamespace()) {
         $cg->setNamespaceName($classReflection->getNamespaceName());
     }
     $properties = array();
     foreach ($classReflection->getProperties() as $reflectionProperty) {
         if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) {
             $properties[] = PropertyGenerator::fromReflection($reflectionProperty);
         }
     }
     $cg->addProperties($properties);
     $methods = array();
     foreach ($classReflection->getMethods() as $reflectionMethod) {
         $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName();
         if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
             $methods[] = MethodGenerator::fromReflection($reflectionMethod);
         }
     }
     $cg->addMethods($methods);
     return $cg;
 }
 protected function findProperties(ClassReflection $class)
 {
     $result = array();
     foreach ($class->getProperties() as $property) {
         $result[] = $property;
     }
     return $result;
 }
Ejemplo n.º 4
0
 public function testPropertyReturns()
 {
     $reflectionClass = new ClassReflection('ZendTest\\Code\\Reflection\\TestAsset\\TestSampleClass2');
     $propertyByName = $reflectionClass->getProperty('_prop1');
     $this->assertInstanceOf('Zend\\Code\\Reflection\\PropertyReflection', $propertyByName);
     $propertiesAll = $reflectionClass->getProperties();
     $this->assertEquals(2, count($propertiesAll));
     $firstProperty = array_shift($propertiesAll);
     $this->assertEquals('_prop1', $firstProperty->getName());
 }
Ejemplo n.º 5
0
 /**
  * Generate view content
  *
  * @param string          $moduleName
  * @param ClassReflection $loadedEntity
  */
 protected function addContent($moduleName, ClassReflection $loadedEntity)
 {
     // prepare some params
     $moduleIdentifier = $this->filterCamelCaseToUnderscore($moduleName);
     $entityName = $loadedEntity->getShortName();
     $entityParam = lcfirst($entityName);
     $formParam = lcfirst(str_replace('Entity', '', $loadedEntity->getShortName())) . 'DeleteForm';
     $moduleRoute = $this->filterCamelCaseToDash($moduleName);
     $deleteMessage = $moduleRoute . '_message_' . $moduleRoute . '_deleting_possible';
     // set action body
     $body = [];
     $body[] = 'use ' . $loadedEntity->getName() . ';';
     $body[] = '';
     $body[] = '/** @var ' . $entityName . ' $' . $entityParam . ' */';
     $body[] = '$' . $entityParam . ' = $this->' . $entityParam . ';';
     $body[] = '';
     $body[] = '$this->h1(\'' . $moduleIdentifier . '_title_delete\');';
     $body[] = '';
     $body[] = '$this->' . $formParam . '->setAttribute(\'action\', $this->url(\'' . $moduleRoute . '/delete\', [\'id\' => $' . $entityParam . '->getIdentifier()]));';
     $body[] = '';
     $body[] = '?>';
     $body[] = '<div class="well">';
     $body[] = '    <table class="table">';
     $body[] = '        <tbody>';
     foreach ($loadedEntity->getProperties() as $property) {
         $methodName = 'get' . ucfirst($property->getName());
         $body[] = '            <tr>';
         $body[] = '                <th class="col-sm-2 text-right"><?php echo $this->translate(\'' . $moduleIdentifier . '_label_' . $this->filterCamelCaseToUnderscore($property->getName()) . '\'); ?></th>';
         $body[] = '                <td class="col-sm-10"><?php echo $' . $entityParam . '->' . $methodName . '() ?></td>';
         $body[] = '            </tr>';
     }
     $body[] = '            <tr>';
     $body[] = '                <th class="col-sm-2 text-right">&nbsp;</th>';
     $body[] = '                <td class="col-sm-10"><?php echo $this->form($this->' . $formParam . '); ?></td>';
     $body[] = '            </tr>';
     $body[] = '        </tbody>';
     $body[] = '    </table>';
     $body[] = '</div>';
     $body[] = '<p>';
     $body[] = '    <a class="btn btn-primary" href="<?php echo $this->url(\'' . $moduleRoute . '\'); ?>">';
     $body[] = '        <i class="fa fa-table"></i>';
     $body[] = '        <?php echo $this->translate(\'' . $moduleIdentifier . '_action_index\'); ?>';
     $body[] = '    </a>';
     $body[] = '</p>';
     $body = implode(AbstractGenerator::LINE_FEED, $body);
     // add method
     $this->setContent($body);
 }
Ejemplo n.º 6
0
 /**
  * Generate view content
  *
  * @param string          $moduleName
  * @param ClassReflection $loadedEntity
  */
 protected function addContent($moduleName, ClassReflection $loadedEntity)
 {
     // prepare some params
     $moduleIdentifier = $this->filterCamelCaseToUnderscore($moduleName);
     $entityName = $loadedEntity->getShortName();
     $entityParam = lcfirst($entityName);
     $moduleRoute = $this->filterCamelCaseToDash($moduleName);
     // set action body
     $body = [];
     $body[] = 'use ' . $loadedEntity->getName() . ';';
     $body[] = '';
     $body[] = '/** @var ' . $entityName . ' $' . $entityParam . ' */';
     $body[] = '$' . $entityParam . ' = $this->' . $entityParam . ';';
     $body[] = '';
     $body[] = '$this->h1(\'' . $moduleIdentifier . '_title_show\');';
     $body[] = '?>';
     $body[] = '<table class="table table-bordered">';
     $body[] = '    <tbody>';
     foreach ($loadedEntity->getProperties() as $property) {
         $methodName = 'get' . ucfirst($property->getName());
         $body[] = '        <tr>';
         $body[] = '            <th><?php echo $this->translate(\'' . $moduleIdentifier . '_label_' . $this->filterCamelCaseToUnderscore($property->getName()) . '\'); ?></th>';
         $body[] = '            <td><?php echo $' . $entityParam . '->' . $methodName . '() ?></td>';
         $body[] = '        </tr>';
     }
     $body[] = '    </tbody>';
     $body[] = '</table>';
     $body[] = '<p>';
     $body[] = '    <a class="btn btn-primary" href="<?php echo $this->url(\'' . $moduleRoute . '\'); ?>">';
     $body[] = '        <i class="fa fa-table"></i>';
     $body[] = '        <?php echo $this->translate(\'' . $moduleIdentifier . '_action_index\'); ?>';
     $body[] = '    </a>';
     $body[] = '</p>';
     $body = implode(AbstractGenerator::LINE_FEED, $body);
     // add method
     $this->setContent($body);
 }
Ejemplo n.º 7
0
 /**
  * fromReflection() - build a Code Generation Php Object from a Class Reflection
  *
  * @param ReflectionClass $classReflection
  * @return ClassGenerator
  */
 public static function fromReflection(ClassReflection $classReflection)
 {
     // class generator
     $cg = new static($classReflection->getName());
     $cg->setSourceContent($cg->getSourceContent());
     $cg->setSourceDirty(false);
     if ($classReflection->getDocComment() != '') {
         $cg->setDocblock(DocblockGenerator::fromReflection($classReflection->getDocblock()));
     }
     $cg->setAbstract($classReflection->isAbstract());
     // set the namespace
     if ($classReflection->inNamespace()) {
         $cg->setNamespaceName($classReflection->getNamespaceName());
     }
     /* @var $parentClass \Zend\Code\Reflection\ReflectionClass */
     if ($parentClass = $classReflection->getParentClass()) {
         $cg->setExtendedClass($parentClass->getName());
         $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces());
     } else {
         $interfaces = $classReflection->getInterfaces();
     }
     $interfaceNames = array();
     foreach ($interfaces as $interface) {
         /* @var $interface \Zend\Code\Reflection\ReflectionClass */
         $interfaceNames[] = $interface->getName();
     }
     $cg->setImplementedInterfaces($interfaceNames);
     $properties = array();
     foreach ($classReflection->getProperties() as $reflectionProperty) {
         /* @var $reflectionProperty \PropertyReflection\Code\Reflection\ReflectionProperty */
         if ($reflectionProperty->getDeclaringClass()->getName() == $cg->getName()) {
             $properties[] = PropertyGenerator::fromReflection($reflectionProperty);
         }
     }
     $cg->setProperties($properties);
     $methods = array();
     foreach ($classReflection->getMethods() as $reflectionMethod) {
         /* @var $reflectionMethod \MethodReflection\Code\Reflection\ReflectionMethod */
         if ($reflectionMethod->getDeclaringClass()->getName() == $cg->getName()) {
             $methods[] = MethodGenerator::fromReflection($reflectionMethod);
         }
     }
     $cg->setMethods($methods);
     return $cg;
 }
Ejemplo n.º 8
0
 /**
  * Generate map of public class attributes
  *
  * @param  string $typename type name
  * @param  DOMElement $typexml target XML element
  * @return void
  */
 protected function _addClassAttributes($typename, \DOMElement $typexml)
 {
     // Do not try to autoload here because _phpTypeToAS should
     // have already attempted to load this class
     if (!class_exists($typename, false)) {
         return;
     }
     $rc = new ClassReflection($typename);
     foreach ($rc->getProperties() as $prop) {
         if (!$prop->isPublic()) {
             continue;
         }
         $propxml = $this->_xml->createElement('property');
         $propxml->setAttribute('name', $prop->getName());
         $type = $this->_registerType($this->_getPropertyType($prop));
         $propxml->setAttribute('type', $type);
         $typexml->appendChild($propxml);
     }
 }
Ejemplo n.º 9
0
 /**
  * Build a Code Generation Php Object from a Class Reflection
  *
  * @param  ClassReflection $classReflection
  * @return ClassGenerator
  */
 public static function fromReflection(ClassReflection $classReflection)
 {
     $cg = new static($classReflection->getName());
     $cg->setSourceContent($cg->getSourceContent());
     $cg->setSourceDirty(false);
     if ($classReflection->getDocComment() != '') {
         $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock()));
     }
     $cg->setAbstract($classReflection->isAbstract());
     // set the namespace
     if ($classReflection->inNamespace()) {
         $cg->setNamespaceName($classReflection->getNamespaceName());
     }
     /* @var \Zend\Code\Reflection\ClassReflection $parentClass */
     $parentClass = $classReflection->getParentClass();
     $interfaces = $classReflection->getInterfaces();
     if ($parentClass) {
         $cg->setExtendedClass($parentClass->getName());
         $interfaces = array_diff($interfaces, $parentClass->getInterfaces());
     }
     $interfaceNames = array();
     foreach ($interfaces as $interface) {
         /* @var \Zend\Code\Reflection\ClassReflection $interface */
         $interfaceNames[] = $interface->getName();
     }
     $cg->setImplementedInterfaces($interfaceNames);
     $properties = array();
     foreach ($classReflection->getProperties() as $reflectionProperty) {
         if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) {
             $properties[] = PropertyGenerator::fromReflection($reflectionProperty);
         }
     }
     $cg->addProperties($properties);
     $constants = array();
     foreach ($classReflection->getConstants() as $name => $value) {
         $constants[] = array('name' => $name, 'value' => $value);
     }
     $cg->addConstants($constants);
     $methods = array();
     foreach ($classReflection->getMethods() as $reflectionMethod) {
         $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName();
         if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
             $methods[] = MethodGenerator::fromReflection($reflectionMethod);
         }
     }
     $cg->addMethods($methods);
     return $cg;
 }
Ejemplo n.º 10
0
 private function getClassProperties($entity)
 {
     $properties = array();
     $reflectedAuditedEntity = new ClassReflection($entity);
     // Get mapping from metadata
     foreach ($reflectedAuditedEntity->getProperties() as $property) {
         $property->setAccessible(true);
         $value = $property->getValue($entity);
         // If a property is an object we probably are not mapping that to
         // a field.  Do no special handing...
         if ($value instanceof PersistentCollection) {
         }
         // Set values to getId for classes
         if (gettype($value) == 'object' and method_exists($value, 'getId')) {
             $value = $value->getId();
         }
         $properties[$property->getName()] = $value;
     }
     return $properties;
 }
Ejemplo n.º 11
0
 /**
  * Retrieve complex type information from class public properties.
  *
  * @param string $class
  * @return array
  * @throws InvalidArgumentException
  */
 protected function _processComplexType($class)
 {
     $typeName = $this->_helper->translateTypeName($class);
     $this->_types[$typeName] = array();
     if ($this->_helper->isArrayType($class)) {
         $this->process($this->_helper->getArrayItemType($class));
     } else {
         if (!class_exists($class)) {
             throw new InvalidArgumentException(sprintf('Could not load the "%s" class as parameter type.', $class));
         }
         $reflection = new ClassReflection($class);
         $docBlock = $reflection->getDocBlock();
         $this->_types[$typeName]['documentation'] = $docBlock ? $this->_getDescription($docBlock) : '';
         $defaultProperties = $reflection->getDefaultProperties();
         /** @var \Zend\Code\Reflection\PropertyReflection $property */
         foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
             $this->_processProperty($property, $defaultProperties, $typeName);
         }
     }
     return $this->_types[$typeName];
 }
Ejemplo n.º 12
0
 function registerRoutes()
 {
     // Auto-register routes.
     if (!isset($this->classLoader->getPrefixesPsr4()[$this->config->namespacePrefix . '\\'])) {
         throw new Exception(sprintf('Namespace prefix "%s" defined in the config was not found in the autoloader.', $this->config->namespacePrefix));
     }
     $sourcePath = array_pop($this->classLoader->getPrefixesPsr4()[$this->config->namespacePrefix . '\\']);
     $routes = [];
     $app = $this->app;
     $formatNegotiator = $this->formatNegotiator;
     $serializer = $this->serializer;
     $config = $this->config;
     foreach ($this->getWebServiceClasses($this->config->namespacePrefix, $sourcePath) as $webServiceClass) {
         // Need to get methods and DTOs from class
         $classReflection = new ClassReflection($webServiceClass);
         $httpMethodNames = $this->config->httpMethodNames;
         $httpMethodReflections = array_filter($classReflection->getMethods(), function ($methodReflection) use($httpMethodNames) {
             return in_array($methodReflection->name, $httpMethodNames);
         });
         // @todo add a check to make sure DTOs are unique. This might happen implicitly when registering routes.
         // Call for each http method/DTO in web service
         /** @var MethodReflection $httpMethodReflection */
         foreach ($httpMethodReflections as $httpMethodReflection) {
             // This assumes that the first argument of the HTTP method is a DTO.
             $httpMethodReflectionPrototype = $httpMethodReflection->getPrototype();
             $requestDtoClass = array_shift($httpMethodReflectionPrototype['arguments'])['type'];
             $requestDtoClassReflection = new ClassReflection($requestDtoClass);
             $requestDtoProperties = $requestDtoClassReflection->getProperties();
             $returnDtoClass = $httpMethodReflection->getReturnType();
             $returnDtoProperties = (new ClassReflection($returnDtoClass))->getProperties();
             $requestMethod = $httpMethodReflectionPrototype['name'];
             $route = '/' . $this->config->baseUrl . '/' . $requestDtoClassReflection->getShortName();
             $routes[] = new class($route, $requestDtoClass, $requestDtoProperties, $returnDtoClass, $returnDtoProperties)
             {
                 public $path;
                 public $requestDto;
                 public $requestDtoParameters;
                 public $returnDto;
                 public $returnDtoProperties;
                 public function __construct(string $path, string $requestDto, array $requestDtoParameters, string $returnDto, array $returnDtoProperties)
                 {
                     $this->path = $path;
                     $this->requestDto = $requestDto;
                     $this->requestDtoParameters = $requestDtoParameters;
                     $this->returnDto = $returnDto;
                     $this->returnDtoProperties = $returnDtoProperties;
                 }
             };
             $app->get($route, function () use($app, $formatNegotiator, $serializer, $config, $webServiceClass, $requestDtoClass, $requestMethod) {
                 /** @var Request $httpRequest */
                 $httpRequest = $app['request'];
                 // Convert request parameters to the request DTO.
                 $params = $serializer->serialize($httpRequest->query->all(), 'json');
                 $requestDto = $serializer->deserialize($params, $requestDtoClass, 'json');
                 // Get the response DTO by calling the HTTP method of the web service class, with the request DTO.
                 $responseDto = (new $webServiceClass())->{$requestMethod}($requestDto);
                 // Content negotiation
                 $format = $formatNegotiator->getBestFormat(implode(',', $httpRequest->getAcceptableContentTypes()), $config->contentNegotiation->priorities);
                 return new Response($serializer->serialize($responseDto, $format), 200, array('Content-Type' => $app['request']->getMimeType($format)));
             });
         }
     }
     /**
      * Register custom _routes meta route
      */
     $app->get($config->baseUrl . '/_routes', function () use($app, $formatNegotiator, $serializer, $config, $routes) {
         $httpRequest = $app['request'];
         $format = $formatNegotiator->getBestFormat(implode(',', $httpRequest->getAcceptableContentTypes()), $config->contentNegotiation->priorities);
         $serializedData = $serializer->serialize($routes, $format);
         $responseCode = Response::HTTP_OK;
         if ($serializedData === false) {
             $serializedData = '';
             $responseCode = Response::HTTP_INTERNAL_SERVER_ERROR;
         }
         return new Response($serializedData, $responseCode, array('Content-Type' => $app['request']->getMimeType($format)));
     });
 }
Ejemplo n.º 13
0
 /**
  * Copied from ClassGenerator::fromReflection and tweaked slightly
  * @param ClassReflection $classReflection
  *
  * @return ClassGenerator
  */
 public function getGeneratorFromReflection(ClassReflection $classReflection)
 {
     // class generator
     $cg = new ClassGenerator($classReflection->getName());
     $cg->setSourceContent($cg->getSourceContent());
     $cg->setSourceDirty(false);
     if ($classReflection->getDocComment() != '') {
         $docblock = DocBlockGenerator::fromReflection($classReflection->getDocBlock());
         $docblock->setIndentation(Generator::$indentation);
         $cg->setDocBlock($docblock);
     }
     $cg->setAbstract($classReflection->isAbstract());
     // set the namespace
     if ($classReflection->inNamespace()) {
         $cg->setNamespaceName($classReflection->getNamespaceName());
     }
     /* @var \Zend\Code\Reflection\ClassReflection $parentClass */
     $parentClass = $classReflection->getParentClass();
     if ($parentClass) {
         $cg->setExtendedClass('\\' . ltrim($parentClass->getName(), '\\'));
         $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces());
     } else {
         $interfaces = $classReflection->getInterfaces();
     }
     $interfaceNames = array();
     foreach ($interfaces as $interface) {
         /* @var \Zend\Code\Reflection\ClassReflection $interface */
         $interfaceNames[] = $interface->getName();
     }
     $cg->setImplementedInterfaces($interfaceNames);
     $properties = array();
     foreach ($classReflection->getProperties() as $reflectionProperty) {
         if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) {
             $property = PropertyGenerator::fromReflection($reflectionProperty);
             $property->setIndentation(Generator::$indentation);
             $properties[] = $property;
         }
     }
     $cg->addProperties($properties);
     $methods = array();
     foreach ($classReflection->getMethods() as $reflectionMethod) {
         $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName();
         if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
             $method = MethodGenerator::fromReflection($reflectionMethod);
             $method->setBody(preg_replace("/^\\s+/m", '', $method->getBody()));
             $method->setIndentation(Generator::$indentation);
             $methods[] = $method;
         }
     }
     $cg->addMethods($methods);
     return $cg;
 }