/** * getDeclaringClass() test * * Call as method call * * Returns: \Zend\Server\Reflection\ReflectionClass */ public function testGetDeclaringClass() { $r = new Reflection\ReflectionMethod($this->_class, $this->_method); $class = $r->getDeclaringClass(); $this->assertTrue($class instanceof Reflection\ReflectionClass); $this->assertTrue($this->_class === $class); }
/** * Generate a list of routes available fo the specified method. * * @param ReflectionMethod $methodReflection * @return array */ public function generateRestRoutes(ReflectionMethod $methodReflection) { $routes = array(); $routePath = "/:" . Mage_Webapi_Controller_Router_Route_Rest::PARAM_VERSION; $routeParts = $this->_helper->getResourceNameParts($methodReflection->getDeclaringClass()->getName()); $partsCount = count($routeParts); for ($i = 0; $i < $partsCount; $i++) { if ($this->_isParentResourceIdExpected($methodReflection) && $i == $partsCount - 1) { $routePath .= "/:" . Mage_Webapi_Controller_Router_Route_Rest::PARAM_PARENT_ID; } $routePath .= "/" . lcfirst($this->_helper->convertSingularToPlural($routeParts[$i])); } if ($this->_isResourceIdExpected($methodReflection)) { $routePath .= "/:" . Mage_Webapi_Controller_Router_Route_Rest::PARAM_ID; } foreach ($this->_getAdditionalRequiredParamNames($methodReflection) as $additionalRequired) { $routePath .= "/{$additionalRequired}/:{$additionalRequired}"; } $actionType = Mage_Webapi_Controller_Request_Rest::getActionTypeByOperation($this->_helper->getMethodNameWithoutVersionSuffix($methodReflection)); $resourceName = $this->_helper->translateResourceName($methodReflection->getDeclaringClass()->getName()); $optionalParams = $this->_getOptionalParamNames($methodReflection); foreach ($this->_getPathCombinations($optionalParams, $routePath) as $finalRoutePath) { $routes[$finalRoutePath] = array('actionType' => $actionType, 'resourceName' => $resourceName); } $this->_routes = array_merge($this->_routes, $routes); return $routes; }
/** * Identify if API resource is top level resource or subresource. * * @param ReflectionMethod $methodReflection * @return bool * @throws InvalidArgumentException In case when class name is not valid API resource class. */ public function isSubresource(ReflectionMethod $methodReflection) { $className = $methodReflection->getDeclaringClass()->getName(); if (preg_match(Mage_Webapi_Model_Config_ReaderAbstract::RESOURCE_CLASS_PATTERN, $className, $matches)) { return count(explode('_', trim($matches[3], '_'))) > 1; } throw new InvalidArgumentException(sprintf('"%s" is not a valid resource class.', $className)); }
/** * Extract method deprecation policy "use method" data. * * @param ReflectionMethod $methodReflection * @param string $useMethod * @param array $deprecationPolicy * @throws LogicException */ protected function _extractDeprecationPolicyUseMethod(ReflectionMethod $methodReflection, $useMethod, &$deprecationPolicy) { $invalidFormatMessage = sprintf('The "%s" method has invalid format of Deprecation policy. ' . 'Accepted formats are createV1, catalogProduct::createV1 ' . 'and Mage_Catalog_Webapi_ProductController::createV1.', $methodReflection->getDeclaringClass()->getName() . '::' . $methodReflection->getName()); /** Add information about what method should be used instead of deprecated/removed one. */ /** * Description is expected in one of the following formats: * - Mage_Catalog_Webapi_ProductController::createV1 * - catalogProduct::createV1 * - createV1 */ $useMethodParts = explode('::', $useMethod); switch (count($useMethodParts)) { case 2: try { /** Support of: Mage_Catalog_Webapi_ProductController::createV1 */ $resourceName = $this->_helper->translateResourceName($useMethodParts[0]); } catch (InvalidArgumentException $e) { /** Support of: catalogProduct::createV1 */ $resourceName = $useMethodParts[0]; } $deprecationPolicy['use_resource'] = $resourceName; $methodName = $useMethodParts[1]; break; case 1: $methodName = $useMethodParts[0]; /** If resource was not specified, current one should be used. */ $deprecationPolicy['use_resource'] = $this->_helper->translateResourceName($methodReflection->getDeclaringClass()->getName()); break; default: throw new LogicException($invalidFormatMessage); break; } try { $methodWithoutVersion = $this->getMethodNameWithoutVersionSuffix($methodName); } catch (Exception $e) { throw new LogicException($invalidFormatMessage); } $deprecationPolicy['use_method'] = $methodWithoutVersion; $methodVersion = str_replace($methodWithoutVersion, '', $methodName); $deprecationPolicy['use_version'] = ucfirst($methodVersion); }
/** * Retrieve method full documentation description. * * @param ReflectionMethod $method * @return string */ protected function extractMethodDescription(ReflectionMethod $method) { $methodReflection = new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()); $docBlock = $methodReflection->getDocBlock(); return $this->_typeProcessor->getDescription($docBlock); }
/** * Retrieve method metadata. * * @param Zend\Server\Reflection\ReflectionMethod $methodReflection * @return array * @throws InvalidArgumentException If specified method was not previously registered in API config. */ public function getMethodMetadata(ReflectionMethod $methodReflection) { $resourceName = $this->_helper->translateResourceName($methodReflection->getDeclaringClass()->getName()); $resourceVersion = $this->_getMethodVersion($methodReflection); $methodName = $this->_helper->getMethodNameWithoutVersionSuffix($methodReflection); if (!isset($this->_data['resources'][$resourceName]['versions'][$resourceVersion]['methods'][$methodName])) { throw new InvalidArgumentException(sprintf('The "%s" method of "%s" resource in version "%s" is not registered.', $methodName, $resourceName, $resourceVersion)); } return $this->_data['resources'][$resourceName]['versions'][$resourceVersion]['methods'][$methodName]; }
/** * Retrieve method full documentation description. * * @param ReflectionMethod $method * @return string */ protected function extractMethodDescription(ReflectionMethod $method) { $methodReflection = new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()); $docBlock = $methodReflection->getDocBlock(); if (!$docBlock) { throw new \LogicException('The docBlock of the method ' . $method->getDeclaringClass()->getName() . '::' . $method->getName() . ' is empty.'); } return $this->_typeProcessor->getDescription($docBlock); }