Louis's answer did not help me, so I just extended the AnnotationBuilder constructor to take the cache object, and then modified getFormSpecification to use this cache to cache the result. My function is below.
Very fast work ... of course, it could be improved. In my case, I was limited to some old hardware, and it took a page load time from 10 + seconds to 1 second
/** * Creates and returns a form specification for use with a factory * * Parses the object provided, and processes annotations for the class and * all properties. Information from annotations is then used to create * specifications for a form, its elements, and its input filter. * * MODIFIED: Now uses local cache to store parsed annotations * * @param string|object $entity Either an instance or a valid class name for an entity * @throws Exception\InvalidArgumentException if $entity is not an object or class name * @return ArrayObject */ public function getFormSpecification($entity) { if (!is_object($entity)) { if ((is_string($entity) && (!class_exists($entity))) // non-existent class || (!is_string($entity)) // not an object or string ) { throw new Exception\InvalidArgumentException(sprintf( '%s expects an object or valid class name; received "%s"', __METHOD__, var_export($entity, 1) )); } } $formSpec = NULL; if ($this->cache) { //generate cache key from entity name $cacheKey = (is_string($entity) ? $entity : get_class($entity)) . '_form_cache'; //get the cached form annotations, try cache first $formSpec = $this->cache->getItem($cacheKey); } if (empty($formSpec)) { $this->entity = $entity; $annotationManager = $this->getAnnotationManager(); $formSpec = new ArrayObject(); $filterSpec = new ArrayObject(); $reflection = new ClassReflection($entity); $annotations = $reflection->getAnnotations($annotationManager); if ($annotations instanceof AnnotationCollection) { $this->configureForm($annotations, $reflection, $formSpec, $filterSpec); } foreach ($reflection->getProperties() as $property) { $annotations = $property->getAnnotations($annotationManager); if ($annotations instanceof AnnotationCollection) { $this->configureElement($annotations, $property, $formSpec, $filterSpec); } } if (!isset($formSpec['input_filter'])) { $formSpec['input_filter'] = $filterSpec; } //save annotations to cache if ($this->cache) { $this->cache->addItem($cacheKey, $formSpec); } } return $formSpec; }
source share