Skip to content

Commit

Permalink
feat: switch to namespaces and other modernizations (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jun 18, 2021
1 parent 7272d54 commit 0375af4
Show file tree
Hide file tree
Showing 24,788 changed files with 831,911 additions and 890,090 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
vendor
composer.lock
src/Google/Service/Compute/HTTPHealthCheck.php
src/Google/Service/Compute/HTTPSHealthCheck.php
src/Compute/HTTPHealthCheck.php
src/Compute/HTTPSHealthCheck.php

# Python for generator
*.pyc
Expand Down
36 changes: 36 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

// For older (pre-2.7.2) verions of google/apiclient
if (
file_exists(__DIR__ . '/../apiclient/src/Google/Client.php')
&& !class_exists('Google_Client', false)
) {
require_once(__DIR__ . '/../apiclient/src/Google/Client.php');
if (
defined('Google_Client::LIBVER')
&& version_compare(Google_Client::LIBVER, '2.7.2', '<=')
) {
$servicesClassMap = [
'Google\\Client' => 'Google_Client',
'Google\\Service' => 'Google_Service',
'Google\\Service\\Resource' => 'Google_Service_Resource',
'Google\\Model' => 'Google_Model',
'Google\\Collection' => 'Google_Collection',
];
foreach ($servicesClassMap as $alias => $class) {
class_alias($class, $alias);
}
}
}
spl_autoload_register(function ($class) {
if (0 === strpos($class, 'Google_Service_')) {
// Autoload the new class, which will also create an alias for the
// old class by changing underscores to namespaces:
// Google_Service_Speech_Resource_Operations
// => Google\Service\Speech\Resource\Operations
$classExists = class_exists($newClass = str_replace('_', '\\', $class));
if ($classExists) {
return true;
}
}
}, true, true);
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@
"phpunit/phpunit": "^5.7||^8.5.13"
},
"autoload": {
"psr-0": {
"Google_Service_": "src"
"psr-4": {
"Google\\Service\\": "src"
},
"files": [
"autoload.php"
]
},
"autoload-dev": {
"psr-4": {
"Google\\": "tests/mocks"
}
}
}
20 changes: 19 additions & 1 deletion generator/src/googleapis/codegen/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ def __init__(self, discovery_doc, language=None):
self._BuildSchemaDefinitions()
self._BuildResourceDefinitions()
self.SetTemplateValue('resources', self._resources)
resource_names = []
for resource in self._resources:
resource_names.append(resource.GetTemplateValue('className'))
self.SetTemplateValue('resourceNames', resource_names)

# Make data models part of the api dictionary
self.SetTemplateValue('models', self.ModelClasses())
Expand Down Expand Up @@ -556,9 +560,20 @@ def __init__(self, api, name, def_dict, parent=None):
self.SetTemplateValue('className', class_name)
# Replace methods dict with Methods
self._methods = []
self._method_classes = []
method_dict = self.values.get('methods') or {}
for name in sorted(method_dict):
self._methods.append(Method(api, name, method_dict[name], parent=self))
method = Method(api, name, method_dict[name], parent=self)
requestType = method.values.get('requestType')
if requestType and requestType.GetTemplateValue('className') and \
requestType.GetTemplateValue('className') not in self._method_classes:
self._method_classes.append(requestType.GetTemplateValue('className'))
response = method.values.get('response')
responseType = method.values.get('responseType')
if response and responseType.GetTemplateValue('className') not in self._method_classes:
self._method_classes.append(responseType.GetTemplateValue('className'))
self._methods.append(method)
self._method_classes.sort()
self.SetTemplateValue('methods', self._methods)
# Get sub resources
self._resources = []
Expand All @@ -576,6 +591,9 @@ def methods(self):
def methods_dict(self):
return {method['wireName']: method for method in self._methods}

@property
def methodClasses(self):
return self._method_classes

class AuthScope(template_objects.CodeObject):
"""The definition of an auth scope.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
{% language php %}{% copyright_block %}

namespace {{ api.ownerName }}\Service;

use Google\Client;

/**
* Service definition for {{ api.className }} ({{ api.version }}).
*
Expand All @@ -15,7 +19,7 @@
*
* @author Google, Inc.
*/
class {{ api.ownerName }}_Service_{{ api.className }} extends Google_Service
class {{ api.className }} extends \Google\Service
{
{% if api.auth.oauth2.scopes %}{% filter noblanklines %}{% indent %}
{% for authscope in api.authscopes %}
Expand All @@ -30,14 +34,15 @@ const {{ authscope.name }} =
* Constructs the internal representation of the {{ api.className }} service.
* {% endfilter %}
*
* @param Google_Client $client The client used to deliver requests.
* @param Client|array $clientOrConfig The client used to deliver requests, or a
* config array to pass to a new Client instance.
* @param string $rootUrl The root URL used for requests to the service.
*/
public function __construct(Google_Client $client, $rootUrl = null)
public function __construct($clientOrConfig = [], $rootUrl = null)
{
{% filter noblanklines %}
{% indent 2 %}
parent::__construct($client);
parent::__construct($clientOrConfig);
$this->rootUrl = $rootUrl ?: {% literal api.rootUrl %};
$this->servicePath = {% literal api.servicePath %};
{% if api.batchPath %}$this->batchPath = {% literal api.batchPath %};{% endif %}
Expand All @@ -51,7 +56,7 @@ $this->serviceName = {% literal api.name %};
{% indent %}
{% indent %}
{% if api.methods %}
$this->base_methods = new Google_Service_Resource(
$this->base_methods = new Resource(
$this,
$this->serviceName,
'{{ resource.codeName }}',
Expand All @@ -68,3 +73,6 @@ $this->base_methods = new Google_Service_Resource(
{% endindent %}
{% endfilter %}
}

// Adding a class alias for backwards compatibility with the previous class name.
class_alias({{ api.className }}::class, '{{ api.ownerName }}_Service_{{ api.className }}');
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<?php
{% language php %}{% copyright_block %}

namespace {{ api.ownerName }}\Service\{{ api.className }}\Resource;
{% if resource.methodClasses %}
{% for methodClass in resource.methodClasses %}use {{ api.ownerName }}\Service\{{ api.className }}\{{ methodClass }}{% if methodClass|lower == resource.className|lower or methodClass in api.resourceNames %} as {{ methodClass }}Model{% endif %};
{% endfor %}{% endif %}
/**
* The "{{ resource.wireName }}" collection of methods.
* Typical usage is:
* <code>
* ${{ api.name }}Service = new {{ api.ownerName }}_Service_{{ api.className }}(...);
* ${{ api.name }}Service = new {{ api.ownerName }}\Service\{{ api.className }}(...);
* ${{ resource.codeName }} = ${{ api.name }}Service->{{ resource.codeName }};
* </code>
*/
class {{ api.ownerName }}_Service_{{ api.className }}_Resource_{{ resource.className }} extends Google_Service_Resource
class {{ resource.className }} extends \Google\Service\Resource
{{% indent %}{% for method in resource.methods %}
{% call_template _method method=method %}{% endfor %}
{% endindent %}}

// Adding a class alias for backwards compatibility with the previous class name.
class_alias({{ resource.className }}::class, '{{ api.ownerName }}_Service_{{ api.className }}_Resource_{{ resource.className }}');
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
<?php
{% language php %}{% copyright_block %}

class {{ api.ownerName }}_Service_{{ api.className }}_{{ model.className }} {% if model.superClass %}extends {{ api.ownerName }}_Service_{{ api.className }}_{{ model.superClass }}{% else %}{% if model.dataType == "array" %}extends Google_Collection{% else %}extends Google_Model{% endif %}{% endif %}
namespace {{ api.ownerName }}\Service\{{ api.className }};
{% if model.superClass %}
use {{ api.ownerName }}\Service\{{ api.className }}\{{ model.superClass }}
{% endif %}
class {{ model.className }} extends {% if model.superClass %}{{ model.superClass }}{% else %}{% if model.dataType == "array" %}\Google\Collection{% else %}\Google\Model{% endif %}{% endif %}
{{% if model.properties %}
{% filter noblanklines %}
{% if not model.is_variant_base and model.collectionKey %}
protected $collection_key = '{{ model.collectionKey }}';
{% endif %}{% if model.has_gapi %}
protected $internal_gapi_mappings = array(
protected $internal_gapi_mappings = [
{% for property in model.properties %}
{% if not property.member_name_is_json_name %}
"{{ property.memberName }}" => "{{ property.wireName }}",
{% endif %}
{% endfor %}
);
];
{% endif %}{% for property in model.properties %}
{% if property.typeHint %}
{% if property.memberName|add:"Type" not in model.propNames %}
protected ${{ property.memberName }}Type = '{{ property.typeHint|cut:" " }}';
protected ${{ property.memberName }}Type = {{ property.typeHint }}::class;
{% endif %}{% if property.memberName|add:"DataType" not in model.propNames %}
protected ${{ property.memberName }}DataType = '{{ property.dataType }}';
{% endif %}
Expand Down Expand Up @@ -60,3 +64,6 @@ class {{ api.ownerName }}_Service_{{ api.className }}_{{ model.className }} {% i
}
{% endfilter %}{% endfor %}{% endif %}
}

// Adding a class alias for backwards compatibility with the previous class name.
class_alias({{ model.className }}::class, '{{ api.ownerName }}_Service_{{ api.className }}_{{ model.className }}');
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{% endfor %}
{% if method.requestType %}
{% parameter %}
{{ api.ownerName }}_Service_{{ api.className }}_{{ method.requestType.className }} $postBody
{{ method.requestType.className }}{% if method.requestType.className|lower == resource.className|lower or method.requestType.className in api.resourceNames %}Model{% endif %} $postBody
{% end_parameter %}
{% endif %}
{% parameter %}$optParams = array(){% end_parameter %}
{% parameter %}$optParams = []{% end_parameter %}
{% end_parameter_list %}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @param {{ param.codeType }}{% if param.repeated %}|array{% endif %} ${{ param.memberName }}
* {{ param.description }}{% endfilter %}{% endfor %}
{% if method.requestType %}
* @param {{ api.ownerName }}_Service_{{ api.className }}_{{ method.requestType.className }} $postBody
* @param {{ method.requestType.className }}{% if method.requestType.className|lower == resource.className|lower or method.requestType.className in api.resourceNames %}Model{% endif %} $postBody
{% endif %}
* @param array $optParams Optional parameters.
{% if method.optional_parameters %}
Expand All @@ -16,26 +16,26 @@
* {{ param.description }}{% endfilter %}{% endfor %}
{% endif %}
{% if method.response %}
* @return {{ api.ownerName }}_Service_{{ api.className }}_{{ method.responseType.className }}
* @return {{ method.responseType.className }}{% if method.responseType.className|lower == resource.className|lower or method.requestType.className in api.resourceNames %}Model{% endif %}
{% endif %}
*/
{% endfilter %}
public function {{ method.name }}({% call_template _func_params method=method %})
{
{% filter noblanklines %}
$params = array({% parameter_list %}
$params = [{% parameter_list %}
{% for p in method.requiredParameters %}
{% parameter %}'{{ p.wireName }}' => ${{ p.memberName }}{% end_parameter %}
{% endfor %}
{% if method.requestType %}
{% parameter %}'postBody' => $postBody{% end_parameter %}
{% endif %}
{% end_parameter_list %});
{% end_parameter_list %}];
$params = array_merge($params, $optParams);
{% if method.response %}
return $this->{% if baseMethod == 1 %}base_methods->{% endif %}call('{{ method.wireName }}', array($params), "{{ api.ownerName }}_Service_{{ api.className }}_{{ method.responseType.className }}");
return $this->{% if baseMethod == 1 %}base_methods->{% endif %}call('{{ method.wireName }}', [$params], {{ method.responseType.className }}{% if method.responseType.className|lower == resource.className|lower or method.responseType.className in api.resourceNames %}Model{% endif %}::class);
{% else %}
return $this->{% if baseMethod == 1 %}base_methods->{% endif %}call('{{ method.wireName }}', array($params));
return $this->{% if baseMethod == 1 %}base_methods->{% endif %}call('{{ method.wireName }}', [$params]);
{% endif %}
{% endfilter %}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
array(
'methods' => array(
{% for method in methods %}{% literal method.wireName %} => array(
[
'methods' => [
{% for method in methods %}{% literal method.wireName %} => [
'path' => {% literal method.path %},
'httpMethod' => {% literal method.httpMethod %},
'parameters' => array({% for p in method.parameters %}
{% literal p.wireName %} => array(
'parameters' => [{% for p in method.parameters %}
{% literal p.wireName %} => [
'location' => {% literal p.location %},
'type' => {% literal p.type %},
{% if p.repeated %}'repeated' => true,{% endif %}
{% if p.required %}'required' => true,{% endif %}
),
{% endfor %}),
),{% endfor %}
)
)
],
{% endfor %}],
],{% endfor %}
]
]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% filter noblanklines %}
{% if resource.json %}
$this->{{ resource.phpPropName }} = new {{ api.ownerName }}_Service_{{ api.className }}_Resource_{{ resource.className }}(
$this->{{ resource.phpPropName }} = new {{ api.className }}\Resource\{{ resource.className }}(
$this,
$this->serviceName,
'{{ resource.codeName }}',
Expand Down
8 changes: 5 additions & 3 deletions generator/src/googleapis/codegen/php_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ def _SetTypeHint(self, prop):
if code_type and code_type.lower() in PhpLanguageModel.PHP_TYPES:
prop.values['typeHint'] = ''
prop.values['typeHintOld'] = ''
prop.values['typeHintFull'] = ''
else:
prop.values['typeHintOld'] = ('%s_%s' %
(self._api.values['owner'].title(),
code_type))
prop.values['typeHint'] = ('%s_Service_%s_%s' %
(self._api.values['owner'].title(),
self._api.values['className'], code_type))
prop.values['typeHintFull'] = ('%s\\Service\\%s\\%s' %
(self._api.values['owner'].title(),
self._api.values['className'], code_type))
prop.values['typeHint'] = (code_type)


class PhpLanguageModel(language_model.LanguageModel):
Expand Down
Loading

0 comments on commit 0375af4

Please sign in to comment.