Since: 2.4
Author: Marco Pivetta (ocramius@gmail.com)
Inheritance: extends UnexpectedValueException, implements ProxyException
コード例 #1
0
ファイル: ProxyGenerator.php プロジェクト: sanborino/clinica
 /**
  * Generates decorated methods by picking those available in the parent class.
  *
  * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
  *
  * @return string
  */
 private function generateMethods(ClassMetadata $class)
 {
     $methods = '';
     $methodNames = array();
     $reflectionMethods = $class->getReflectionClass()->getMethods(\ReflectionMethod::IS_PUBLIC);
     $skippedMethods = array('__sleep' => true, '__clone' => true, '__wakeup' => true, '__get' => true, '__set' => true, '__isset' => true);
     foreach ($reflectionMethods as $method) {
         $name = $method->getName();
         if ($method->isConstructor() || isset($skippedMethods[strtolower($name)]) || isset($methodNames[$name]) || $method->isFinal() || $method->isStatic() || !$method->isPublic()) {
             continue;
         }
         $methodNames[$name] = true;
         $methods .= "\n    /**\n" . "     * {@inheritDoc}\n" . "     */\n" . '    public function ';
         if ($method->returnsReference()) {
             $methods .= '&';
         }
         $methods .= $name . '(';
         $firstParam = true;
         $parameterString = '';
         $argumentString = '';
         $parameters = array();
         foreach ($method->getParameters() as $param) {
             if ($firstParam) {
                 $firstParam = false;
             } else {
                 $parameterString .= ', ';
                 $argumentString .= ', ';
             }
             try {
                 $paramClass = $param->getClass();
             } catch (\ReflectionException $previous) {
                 throw UnexpectedValueException::invalidParameterTypeHint($class->getName(), $method->getName(), $param->getName(), $previous);
             }
             // We need to pick the type hint class too
             if (null !== $paramClass) {
                 $parameterString .= '\\' . $paramClass->getName() . ' ';
             } elseif ($param->isArray()) {
                 $parameterString .= 'array ';
             } elseif (method_exists($param, 'isCallable') && $param->isCallable()) {
                 $parameterString .= 'callable ';
             }
             if ($param->isPassedByReference()) {
                 $parameterString .= '&';
             }
             $parameters[] = '$' . $param->getName();
             $parameterString .= '$' . $param->getName();
             $argumentString .= '$' . $param->getName();
             if ($param->isDefaultValueAvailable()) {
                 $parameterString .= ' = ' . var_export($param->getDefaultValue(), true);
             }
         }
         $methods .= $parameterString . ')';
         $methods .= "\n" . '    {' . "\n";
         if ($this->isShortIdentifierGetter($method, $class)) {
             $identifier = lcfirst(substr($name, 3));
             $fieldType = $class->getTypeOfField($identifier);
             $cast = in_array($fieldType, array('integer', 'smallint')) ? '(int) ' : '';
             $methods .= '        if ($this->__isInitialized__ === false) {' . "\n";
             $methods .= '            return ' . $cast . ' parent::' . $method->getName() . "();\n";
             $methods .= '        }' . "\n\n";
         }
         $methods .= "\n        \$this->__initializer__ " . "&& \$this->__initializer__->__invoke(\$this, " . var_export($name, true) . ", array(" . implode(', ', $parameters) . "));" . "\n\n        return parent::" . $name . '(' . $argumentString . ');' . "\n" . '    }' . "\n";
     }
     return $methods;
 }
コード例 #2
0
 /**
  * @param ClassMetadata $class
  * @param \ReflectionMethod $method
  * @param \ReflectionParameter $parameter
  *
  * @return string|null
  */
 private function getParameterType(ClassMetadata $class, \ReflectionMethod $method, \ReflectionParameter $parameter)
 {
     // We need to pick the type hint class too
     if ($parameter->isArray()) {
         return 'array';
     }
     if (method_exists($parameter, 'isCallable') && $parameter->isCallable()) {
         return 'callable';
     }
     try {
         $parameterClass = $parameter->getClass();
         if ($parameterClass) {
             return '\\' . $parameterClass->getName();
         }
     } catch (\ReflectionException $previous) {
         throw UnexpectedValueException::invalidParameterTypeHint($class->getName(), $method->getName(), $parameter->getName(), $previous);
     }
     return null;
 }
コード例 #3
0
ファイル: ProxyGenerator.php プロジェクト: doctrine/common
 /**
  * @param \ReflectionType $type
  * @param \ReflectionMethod $method
  * @param \ReflectionParameter|null $parameter
  *
  * @return string
  */
 private function formatType(\ReflectionType $type, \ReflectionMethod $method, \ReflectionParameter $parameter = null)
 {
     $name = method_exists($type, 'getName') ? $type->getName() : (string) $type;
     $nameLower = strtolower($name);
     if ('self' === $nameLower) {
         $name = $method->getDeclaringClass()->getName();
     }
     if ('parent' === $nameLower) {
         $name = $method->getDeclaringClass()->getParentClass()->getName();
     }
     if (!$type->isBuiltin() && !class_exists($name) && !interface_exists($name)) {
         if (null !== $parameter) {
             throw UnexpectedValueException::invalidParameterTypeHint($method->getDeclaringClass()->getName(), $method->getName(), $parameter->getName());
         }
         throw UnexpectedValueException::invalidReturnTypeHint($method->getDeclaringClass()->getName(), $method->getName());
     }
     if (!$type->isBuiltin()) {
         $name = '\\' . $name;
     }
     if ($type->allowsNull() && (null === $parameter || !$parameter->isOptional() || null !== $parameter->getDefaultValue())) {
         $name = '?' . $name;
     }
     return $name;
 }