/** * Creates a PHP class from reflection * * @param \ReflectionClass $ref * @return PhpClass */ public static function fromReflection(\ReflectionClass $ref) { $class = new static(); $class->setQualifiedName($ref->name)->setAbstract($ref->isAbstract())->setFinal($ref->isFinal())->setUseStatements(ReflectionUtils::getUseStatements($ref)); if ($ref->getDocComment()) { $docblock = new Docblock($ref); $class->setDocblock($docblock); $class->setDescription($docblock->getShortDescription()); $class->setLongDescription($docblock->getLongDescription()); } // methods foreach ($ref->getMethods() as $method) { $class->setMethod(static::createMethod($method)); } // properties foreach ($ref->getProperties() as $property) { $class->setProperty(static::createProperty($property)); } // traits foreach ($ref->getTraits() as $trait) { $class->addTrait(PhpTrait::fromReflection($trait)); } // constants // TODO: https://github.com/gossi/php-code-generator/issues/19 $class->setConstants($ref->getConstants()); return $class; }
public static function factory($config = array(), $required = array()) { if (!defined('static::ENDPOINT')) { throw new Exception\ServiceEndpointException('A client must have an endpoint'); } $default = array('base_url' => '{scheme}://{domain}/' . static::ENDPOINT); $required = array_merge(array('scheme', 'domain', 'base_url'), $required); $config = Collection::fromConfig($config, $default, $required); $client = new static($config->get('base_url'), $config); $refClass = new \ReflectionClass(get_called_class()); $serviceDefinitionPath = dirname($refClass->getFileName()); $classNamePieces = explode('\\', get_called_class()); $serviceDefinitionFile = array_pop($classNamePieces) . '.json'; switch (true) { case is_readable(dirname($serviceDefinitionPath) . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . $serviceDefinitionFile): $serviceDefinition = $serviceDefinitionPath . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . $serviceDefinitionFile; break; case is_readable($serviceDefinitionPath . DIRECTORY_SEPARATOR . $serviceDefinitionFile): $serviceDefinition = $serviceDefinitionPath . DIRECTORY_SEPARATOR . $serviceDefinitionFile; break; default: throw new Exception\ClientConfigurationException('A client must have a service definition. Could not read the file "' . $serviceDefinition . '"'); } $description = ServiceDescription::factory($serviceDefinition); $client->setDescription($description); return $client; }
/** * {@inheritDoc} */ public static function factory($config = array()) { $config = self::prepareConfig($config); $client = new static($config->get('base_url'), $config->get('api_key'), $config->get('version')); $description = ServiceDescription::factory(__DIR__ . DIRECTORY_SEPARATOR . 'service.json'); $client->setDescription($description); return $client; }
/** * {@inheritdoc} * * @return static */ public static function factory($config = array()) { $default = array('base_url' => 'https://graph.facebook.com/'); $config = Collection::fromConfig($config, $default, array()); $client = new static($config->get('base_url'), $config); $description = ServiceDescription::factory(__DIR__ . '/Resources/client.json'); $client->setDescription($description); return $client; }
public static function factory($config = array()) { $defaults = array('base_url' => 'https://api.twitter.com/{version}/', 'version' => '1.1', 'service_descriptor' => __DIR__ . '/../twitter_client.json'); $required = array('base_url', 'version', 'consumer_key', 'consumer_secret', 'token', 'token_secret', 'service_descriptor'); $config = Collection::fromConfig($config, $defaults, $required); $client = new static($config->get('base_url'), $config); $description = ServiceDescription::factory($config['service_descriptor']); $client->setDescription($description); $client->addSubscriber(new OauthPlugin(array('consumer_key' => $config['consumer_key'], 'consumer_secret' => $config['consumer_secret'], 'token' => $config['token'], 'token_secret' => $config['token_secret']))); return $client; }
public static function factory($config = array()) { // Provide a hash of default client configuration options $default = array('base_url' => 'http://api.porot.com'); // The following values are required when creating the client $required = array('base_url', 'token'); // Merge in default settings and validate the config $config = Collection::fromConfig($config, $default, $required); // Create a new client $client = new static($config->get('base_url'), $config); $client->setDescription(ServiceDescription::factory(__DIR__ . '/Resources/' . static::SERVICE_DESCRIPTION)); return $client; }
public static function fromReflection(\ReflectionMethod $ref) { $method = new static($ref->name); $method->setFinal($ref->isFinal())->setAbstract($ref->isAbstract())->setStatic($ref->isStatic())->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE))->setReferenceReturned($ref->returnsReference())->setBody(ReflectionUtils::getFunctionBody($ref)); $docblock = new Docblock($ref); $method->setDocblock($docblock); $method->setDescription($docblock->getShortDescription()); $method->setLongDescription($docblock->getLongDescription()); foreach ($ref->getParameters() as $param) { $method->addParameter(static::createParameter($param)); } return $method; }
public static function fromReflection(\ReflectionProperty $ref) { $property = new static($ref->name); $property->setStatic($ref->isStatic())->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE)); $docblock = new Docblock($ref); $property->setDocblock($docblock); $property->setDescription($docblock->getShortDescription()); $defaultProperties = $ref->getDeclaringClass()->getDefaultProperties(); if (isset($defaultProperties[$ref->name])) { $property->setDefaultValue($defaultProperties[$ref->name]); } return $property; }
/** * Creates a PHP interface from reflection * * @param \ReflectionClass $ref * @return PhpInterface */ public static function fromReflection(\ReflectionClass $ref) { $interface = new static(); $interface->setQualifiedName($ref->name)->setConstants($ref->getConstants())->setUseStatements(ReflectionUtils::getUseStatements($ref)); $docblock = new Docblock($ref); $interface->setDocblock($docblock); $interface->setDescription($docblock->getShortDescription()); $interface->setLongDescription($docblock->getLongDescription()); foreach ($ref->getMethods() as $method) { $method = static::createMethod($method); $method->setAbstract(false); $interface->setMethod($method); } return $interface; }
public static function factory($config = array()) { $defaults = array('base_url' => 'https://api.delicious.com/v1/'); $required = array('base_url'); $config = Collection::fromConfig($config, $defaults, $required); $client = new static($config->get('base_url'), $config); // Attach a service description to the client $description = ServiceDescription::factory(__DIR__ . '/client.json'); $client->setDescription($description); // Add curl authentication to the request when username and password is set. if ($config->hasKey('username') && $config->hasKey('password')) { $authPlugin = new CurlAuthPlugin($config->get('username'), $config->get('password')); $client->addSubscriber($authPlugin); } return $client; }
/** * Create a security scheme from an array * * @param string $key * @param array $data * [ * description: ?string * type: ?string * describedBy: ?string[] * settings: ?object[] * ] * @param ApiDefinition $apiDefinition * * @return SecurityScheme */ public static function createFromArray($key, array $data = [], ApiDefinition $apiDefinition = null) { $securityScheme = new static($key); if (isset($data['description'])) { $securityScheme->setDescription($data['description']); } if (isset($data['type'])) { $securityScheme->setType($data['type']); } if (isset($data['describedBy'])) { $securityScheme->setDescribedBy(SecuritySchemeDescribedBy::createFromArray('describedBy', $data['describedBy'])); } if (isset($data['settings'])) { $securityScheme->setSettings($data['settings']); } return $securityScheme; }
/** * Create a new response object from an array * * @param string $statusCode * @param array $data * * @return Response */ public static function createFromArray($statusCode, array $data = []) { $response = new static($statusCode); if (isset($data['body']) && is_array($data['body'])) { foreach ($data['body'] as $key => $bodyData) { $response->addBody(Body::createFromArray($key, $bodyData ?: [])); } } if (isset($data['description'])) { $response->setDescription($data['description']); } if (isset($data['headers'])) { foreach ($data['headers'] as $key => $header) { $response->addHeader(NamedParameter::createFromArray($key, $header)); } } return $response; }
/** * Create a new body from an array * * @param string $mediaType * @param array $data * [ * schema: ?string * example: ?string * examples: ?array * ] * * @throws \Exception * * @return Body */ public static function createFromArray($mediaType, array $data = []) { $body = new static($mediaType); if (isset($data['description'])) { $body->setDescription($data['description']); } if (isset($data['schema'])) { $body->setSchema($data['schema']); } if (isset($data['example'])) { $body->addExample($data['example']); } if (isset($data['examples'])) { foreach ($data['examples'] as $example) { $body->addExample($example); } } return $body; }
/** * Factory depuis une reflection * * @param ReflectionProperty $reflection * @return static */ public static function fromReflection(ReflectionProperty $reflection) { // gestion du type $type = implode('|', $reflection->getDocBlockTypeStrings()); // cas de la valeur null $value = $reflection->getDefaultValue(); if (is_null($value)) { $value = Maker::NO_VALUE; $class = $reflection->getDeclaringClass(); foreach ($class->getAst()->stmts as $stmt) { // si pas un attribut, on zap if (!$stmt instanceof \PhpParser\Node\Stmt\Property) { continue; } foreach ($stmt->props as $prop) { if ($prop instanceof \PhpParser\Node\Stmt\PropertyProperty) { // lecture du fichier $file = file($class->getFileName()); if (!empty($line = $file[$prop->getLine() - 1])) { if (strpos($line, '=')) { $value = null; } } } } } } // construction $property = new static($reflection->getName(), $value, $type); // docblock $docblock = new \phpDocumentor\Reflection\DocBlock($reflection->getDocComment()); $property->setSummary($docblock->getShortDescription()); $property->setDescription($docblock->getLongDescription()); // gestion des modifiers $reflection->isPrivate() ? $property->enablePrivate() : $property->disablePrivate(); $reflection->isProtected() ? $property->enableProtected() : $property->disabledProtected(); $reflection->isPublic() ? $property->enablePublic() : $property->disablePublic(); $reflection->isStatic() ? $property->enableStatic() : $property->disableStatic(); return $property; }
public static function fromReflection(\ReflectionClass $ref) { $trait = new static(); $trait->setQualifiedName($ref->name); $trait->setUseStatements(ReflectionUtils::getUseStatements($ref)); $docblock = new Docblock($ref); $trait->setDocblock($docblock); $trait->setDescription($docblock->getShortDescription()); $trait->setLongDescription($docblock->getLongDescription()); // traits foreach ($ref->getTraits() as $reflectionTrait) { $trait->addTrait(PhpTrait::fromReflection($reflectionTrait)); } // properties foreach ($ref->getProperties() as $property) { $trait->setProperty(static::createProperty($property)); } // methods foreach ($ref->getMethods() as $method) { $trait->setMethod(static::createMethod($method)); } return $trait; }
public static function fromArray(array $a) { $requiredFields = array("id", "secret", "type", "redirect_uri", "name"); foreach ($requiredFields as $r) { if (!array_key_exists($r, $a)) { throw new ClientRegistrationException("not a valid client, '" . $r . "' not set"); } } $c = new static($a['id'], $a['secret'], $a['type'], $a['redirect_uri'], $a['name']); if (array_key_exists("allowed_scope", $a)) { $c->setAllowedScope($a['allowed_scope']); } if (array_key_exists("icon", $a)) { $c->setIcon($a['icon']); } if (array_key_exists("description", $a)) { $c->setDescription($a['description']); } if (array_key_exists("contact_email", $a)) { $c->setContactEmail($a['contact_email']); } return $c; }
/** * Create a new Method from an array * * @param string $method * @param array $data * [ * body: ?array * headers: ?array * description: ?string * protocols: ?array * responses: ?array * queryParameters: ?array * ] * @param ApiDefinition $apiDefinition * * @throws \Exception * * @return Method */ public static function createFromArray($method, array $data = [], ApiDefinition $apiDefinition = null) { $method = new static($method, $apiDefinition); if (isset($data['body'])) { foreach ($data['body'] as $key => $bodyData) { if (in_array($key, WebFormBody::$validMediaTypes)) { $body = WebFormBody::createFromArray($key, $bodyData); } else { $body = Body::createFromArray($key, $bodyData); } $method->addBody($body); } } if (isset($data['headers'])) { foreach ($data['headers'] as $key => $header) { $method->addHeader(NamedParameter::createFromArray($key, $header)); } } if (isset($data['description'])) { $method->setDescription($data['description']); } if (isset($data['baseUriParameters'])) { foreach ($data['baseUriParameters'] as $key => $baseUriParameter) { $method->addBaseUriParameter(BaseUriParameter::createFromArray($key, $baseUriParameter)); } } if (isset($data['protocols'])) { foreach ($data['protocols'] as $protocol) { $method->addProtocol($protocol); } } if (isset($data['responses']) && is_array($data['responses'])) { foreach ($data['responses'] as $responseCode => $response) { $method->addResponse(Response::createFromArray($responseCode, $response ?: [])); } } if (isset($data['queryParameters'])) { foreach ($data['queryParameters'] as $key => $queryParameterData) { $method->addQueryParameter(NamedParameter::createFromArray($key, $queryParameterData)); } } if (isset($data['securedBy'])) { foreach ($data['securedBy'] as $key => $securedBy) { if ($securedBy) { $method->addSecurityScheme($apiDefinition->getSecurityScheme($securedBy)); } else { $method->addSecurityScheme(SecurityScheme::createFromArray('null', array(), $apiDefinition)); } } } return $method; }
/** * for transitions that contain regex states, we need to be able to copy an * existing (subclass of this) transition with all it's fields. * We need to instantiate it with a different from and to state since either * one of those states can be the regex states. All other fields need to be * copied. * * Override this method in a subclass to add other fields. By using 'new * static' we are already instantiating a possible subclass. * * * @param State $from * @param State $to * @return Transition */ public function getCopy(State $from, State $to) { $copy = new static($from, $to, $this->getEvent(), $this->getRuleName(), $this->getCommandName(), $this->getGuardCallable(), $this->getTransitionCallable()); $copy->setDescription($this->getDescription()); return $copy; }
/** * Create a Resource from an array * * @param string $uri * @param array $data * [ * uri: string * displayName: ?string * description: ?string * baseUriParameters: ?array * ] * * @param ApiDefinition $apiDefinition * @param Resource $parentResource * * @return Resource */ public static function createFromArray($uri, array $data = [], ApiDefinition $apiDefinition = null, Resource $parentResource = null) { $resource = new static($uri, $apiDefinition); if (isset($data['displayName'])) { $resource->setDisplayName($data['displayName']); } else { $resource->setDisplayName($uri); } if (isset($data['description'])) { $resource->setDescription($data['description']); } if (isset($data['baseUriParameters'])) { foreach ($data['baseUriParameters'] as $key => $baseUriParameter) { $resource->addBaseUriParameter(NamedParameter::createFromArray($key, $baseUriParameter)); } } if (isset($data['uriParameters'])) { foreach ($data['uriParameters'] as $key => $uriParameter) { $resource->addUriParameter(NamedParameter::createFromArray($key, $uriParameter ?: [])); } } if (null !== $parentResource) { foreach ($parentResource->getUriParameters() as $uriParameter) { $resource->addUriParameter($uriParameter); } } if (isset($data['securedBy'])) { foreach ($data['securedBy'] as $key => $securedBy) { if (null !== $securedBy && $apiDefinition->getSecurityScheme($securedBy) instanceof SecurityScheme) { $resource->addSecurityScheme($apiDefinition->getSecurityScheme($securedBy)); } else { $resource->addSecurityScheme(SecurityScheme::createFromArray('null', array(), $apiDefinition)); } } } foreach ($data as $key => $value) { if (count($resource->getSecuritySchemes()) > 0) { foreach ($resource->getSecuritySchemes() as $securityScheme) { if ($securityScheme instanceof SecurityScheme && 'null' !== $securityScheme->getKey()) { if (!isset($value['securedBy'])) { if (!is_array($value)) { $value = []; } $value['securedBy'][] = $securityScheme->getKey(); } } } } if (strpos($key, '/') === 0) { $resource->addResource(Resource::createFromArray($uri . $key, $value ?: [], $apiDefinition, $resource)); } elseif (in_array(strtoupper($key), Method::$validMethods)) { $resource->addMethod(Method::createFromArray($key, $value ?: [], $apiDefinition)); } } return $resource; }
/** * Create a Query Parameter from an Array * * @param $key * @param $data * [ * displayName: ?string * description: ?string * type: ?["string","number","integer","date","boolean","file"] * enum: ?array * pattern: ?string * validationPattern: ?string * minLength: ?integer * maxLength: ?integer * minimum: ?integer * maximum: ?integer * example: ?string * examples: ?array * repeat: ?boolean * required: ?boolean * default: ?string * ] * * @throws \Exception * * @return NamedParameter */ public static function createFromArray($key, array $data = []) { $namedParameter = new static($key); if (isset($data['displayName'])) { $namedParameter->setDisplayName($data['displayName']); } if (isset($data['description'])) { $namedParameter->setDescription($data['description']); } if (isset($data['type'])) { $namedParameter->setType($data['type']); } if (isset($data['enum'])) { $namedParameter->setEnum($data['enum']); } if (isset($data['pattern'])) { $namedParameter->setValidationPattern($data['pattern']); } // RAML 1.0 if (isset($data['validationPattern'])) { $namedParameter->setValidationPattern($data['validationPattern']); } if (isset($data['minLength'])) { $namedParameter->setMinLength($data['minLength']); } if (isset($data['maxLength'])) { $namedParameter->setMaxLength($data['maxLength']); } if (isset($data['minimum'])) { $namedParameter->setMinimum($data['minimum']); } if (isset($data['maximum'])) { $namedParameter->setMaximum($data['maximum']); } if (isset($data['example'])) { $namedParameter->addExample($data['example']); } if (isset($data['examples'])) { foreach ($data['examples'] as $example) { $namedParameter->addExample($example); } } if (isset($data['repeat'])) { $namedParameter->setRepeat($data['repeat']); } if (isset($data['required'])) { $namedParameter->setRequired($data['required']); } if (isset($data['default'])) { if ($namedParameter->getType() === self::TYPE_DATE) { $namedParameter->setDefault(\DateTime::createFromFormat('D, d M Y H:i:s T', $data['default'])); } else { $namedParameter->setDefault($data['default']); } } return $namedParameter; }
/** * @param array $config * @return static */ public static function factory($config = array()) { $default = array('ssl.certificate_authority' => 'system'); $required = array('merchant_id', 'api_key'); $config = Collection::fromConfig($config, $default, $required); $client = new static(isset($config['base_url']) ? $config['base_url'] : null, $config); $client->setDescription(ServiceDescription::factory(__DIR__ . '/Resources/xsolla-2015-07-23.php')); $client->setDefaultOption('auth', array($config['merchant_id'], $config['api_key'], 'Basic')); $client->setDefaultOption('headers', array('Accept' => 'application/json', 'Content-Type' => 'application/json')); $client->setDefaultOption('command.params', array('merchant_id' => $config['merchant_id'])); $client->setUserAgent(Version::getVersion()); $exceptionCb = function (Event $event) { $previous = $event['exception']; if ($previous instanceof BadResponseException) { $e = XsollaAPIException::fromBadResponse($previous); } else { $e = new XsollaAPIException('XsollaClient Exception: ' . $previous->getMessage(), 0, $previous); } throw $e; }; $client->getEventDispatcher()->addListener('request.exception', $exceptionCb); return $client; }
/** * @param float $cost * @param string $description * @return Payment */ public static function create($cost, $description) { $payment = new static(); $payment->setCost($cost); $payment->setDescription($description); $payment->setCreationTime(new DateTime()); $payment->setStatus(static::WAITING_PAYMENT); return $payment; }
/** * * * @param ReflectionMethod $reflection * @return Method */ public static function fromReflection(ReflectionMethod $reflection) { // gestion du type // $type = implode('|', $reflection->getDocBlockTypeStrings()); // // construction $method = new static($reflection->getName(), [], $reflection->getBodyCode()); // docblock $docblock = new \phpDocumentor\Reflection\DocBlock($reflection->getDocComment()); $method->setSummary($docblock->getShortDescription()); $method->setDescription($docblock->getLongDescription()); // gestion des modifiers $reflection->isPrivate() ? $method->enablePrivate() : $method->disablePrivate(); $reflection->isProtected() ? $method->enableProtected() : $method->disabledProtected(); $reflection->isPublic() ? $method->enablePublic() : $method->disablePublic(); $reflection->isStatic() ? $method->enableStatic() : $method->disableStatic(); $reflection->isFinal() ? $method->enableFinal() : $method->disableFinal(); foreach ($reflection->getParameters() as $parameter) { $method->addParameter(Parameter::fromReflection($parameter)); } return $method; }
/** * Create a Resource from an array * * @param string $uri * @param ApiDefinition $apiDefinition * @param array $data * [ * uri: string * displayName: ?string * description: ?string * baseUriParameters: ?array * ] * * @return self */ public static function createFromArray($uri, array $data = [], ApiDefinition $apiDefinition = null) { $resource = new static($uri, $apiDefinition); if (isset($data['displayName'])) { $resource->setDisplayName($data['displayName']); } else { $resource->setDisplayName($uri); } if (isset($data['description'])) { $resource->setDescription($data['description']); } if (isset($data['baseUriParameters'])) { foreach ($data['baseUriParameters'] as $key => $baseUriParameter) { $resource->addBaseUriParameter(BaseUriParameter::createFromArray($key, $baseUriParameter)); } } if (isset($data['uriParameters'])) { foreach ($data['uriParameters'] as $key => $uriParameter) { $resource->addUriParameter(NamedParameter::createFromArray($key, $uriParameter ?: [])); } } if (isset($data['securedBy'])) { foreach ($data['securedBy'] as $securedBy) { if ($securedBy) { $resource->addSecurityScheme($apiDefinition->getSecurityScheme($securedBy)); } else { $resource->addSecurityScheme(SecurityScheme::createFromArray('null', [], $apiDefinition)); } } } foreach ($data as $key => $value) { if (strpos($key, '/') === 0) { $resource->addResource(Resource::createFromArray($uri . $key, $value ?: [], $apiDefinition)); } elseif (in_array(strtoupper($key), Method::$validMethods)) { $resource->addMethod(Method::createFromArray($key, $value, $apiDefinition)); } } return $resource; }
/** * Add an argument(sub command) setting. * * @param string|AbstractCommand $command The argument name or Console object. * If we just send a string, the object will auto create. * @param null $description Console description. * @param array $options Console options. * @param \Closure $code The closure to execute. * * @return AbstractCommand Return this object to support chaining. * * @since 1.0 */ public function addCommand($command, $description = null, $options = array(), \Closure $code = null) { if (!$command instanceof AbstractCommand) { $command = new static($command, $this->input, $this->output, $this); } // Set argument detail $command->setApplication($this->application)->setInput($this->input); if ($description !== null) { $command->setDescription($description); } if (count($options)) { $command->setOptions($options); } if ($code) { $command->setHandler($code); } // Set parent $command->setParent($this); // Set global options to sub command /** @var $option Option */ foreach ($this->globalOptions as $option) { $command->addOption($option); } $name = $command->getName(); $this->children[$name] = $command; return $this; }