Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/php/sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ sonar.projectName=twilio-oai-generator-php
sonar.organization=twilio
sonar.sources=src
sonar.tests=tests
sonar.exclusions=Api.php,Client.php,FlexApi.php,Versionless.php
sonar.php.coverage.reportPaths=php_coverage.xml
sonar.php.tests.reportPath=execution-result.xml
36 changes: 36 additions & 0 deletions examples/php/src/Twilio/Rest/Api/V2010/Account/CallContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
use Twilio\Rest\Api\V2010\Account\Call\FeedbackCallSummaryList;


/**
* @method \Twilio\Rest\Api\V2010\Account\Call\FeedbackCallSummaryContext feedbackCallSummary(string $sid)
*/

class CallContext extends InstanceContext {
/**
Expand Down Expand Up @@ -75,6 +78,39 @@ public function fetch(): CallInstance {
);
}

/**
* Magic getter to lazy load subresources
*
* @param string $name Subresource to return
* @return ListResource The requested subresource
* @throws TwilioException For unknown subresources
*/
public function __get(string $name): ListResource {
if (\property_exists($this, '_' . $name)) {
$method = 'get' . \ucfirst($name);
return $this->$method();
}

throw new TwilioException('Unknown subresource ' . $name);
}

/**
* Magic caller to get resource contexts
*
* @param string $name Resource to return
* @param array $arguments Context parameters
* @return InstanceContext The requested resource context
* @throws TwilioException For unknown resource
*/
public function __call(string $name, array $arguments): InstanceContext {
$property = $this->$name;
if (\method_exists($property, 'getContext')) {
return \call_user_func_array(array($property, 'getContext'), $arguments);
}

throw new TwilioException('Resource does not have a context');
}

/**
* Provide a friendly representation
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

/**
* @property FeedbackCallSummaryList $feedbackCallSummary
* @method \Twilio\Rest\Api\V2010\Account\Call\FeedbackCallSummaryContext feedbackCallSummary(string $sid)
*/

class CallList extends ListResource {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/twilio/oai/TwilioPhpGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public String apiFilename(final String templateName, final String tag) {
@Override
public String toApiFilename(final String name) {

return directoryStructureService.toApiFilename(super.toApiFilename(name));
String apiFileName = directoryStructureService.toApiFilename(super.toApiFilename(name));
apiFileName = apiFileName.replaceAll("/Function/", "/TwilioFunction/");
return apiFileName;
}

@Override
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/twilio/oai/api/PhpApiResourceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.stream.Collectors;
import java.util.regex.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static com.twilio.oai.common.ApplicationConstants.PATH_SEPARATOR_PLACEHOLDER;

Expand Down Expand Up @@ -66,7 +67,10 @@ public IApiResourceBuilder updateApiPath() {
public ApiResourceBuilder setImports(DirectoryStructureService directoryStructureService) {
codegenOperationList.stream().forEach(operation -> {
if (!pathSet.contains(operation.path)) {
List<Resource> dependents = directoryStructureService.getResourceTree().dependents(operation.path);
List<Resource> dependents = StreamSupport.stream(directoryStructureService.getResourceTree().getResources().spliterator(), false)
.filter(resource -> PathUtils.removeFirstPart(operation.path).equals(PathUtils.getTwilioExtension(resource.getPathItem(), "parent").orElse(null)))
.collect(Collectors.toList());

List<Resource> methodDependents = dependents.stream().filter(dep ->
PathUtils.getTwilioExtension(dep.getPathItem(), "pathType").get().equals("instance"))
.collect(Collectors.toList());
Expand Down Expand Up @@ -98,6 +102,13 @@ private static void updateDependents(DirectoryStructureService directoryStructur
opr)));
resourceList.stream().filter(dependent -> dependent.getPathItem().readOperations().isEmpty()).
forEach(dep -> directoryStructureService.addContextdependents(dependentList, dep.getName(), null));

dependentList.stream().map(DirectoryStructureService.ContextResource.class::cast)
.map(contextResource -> {
if (contextResource.getParent().matches("(.*)Function\\\\(.*)"))
contextResource.setParent(contextResource.getParent().replaceAll("\\\\Function\\\\", "\\\\TwilioFunction\\\\"));
return (Object) contextResource;
}).collect(Collectors.toList());
}

private String formatPath(String path) {
Expand Down Expand Up @@ -130,6 +141,8 @@ private void updateNamespaceSubPart(CodegenOperation codegenOperation) {
.collect(Collectors.joining("\\"));
namespaceSubPart = "\\" + namespacePath;
}
namespaceSubPart = namespaceSubPart.replaceAll("\\\\Function$", "\\\\TwilioFunction");
namespaceSubPart = namespaceSubPart.replaceAll("\\\\Function[\\\\]", "\\\\TwilioFunction\\\\");
}

@Override
Expand Down
41 changes: 6 additions & 35 deletions src/main/resources/twilio-php/context.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -80,41 +80,12 @@ class {{apiName}}Context extends InstanceContext {
}

{{/metaProperties.contextImportProperties}}
{{#metaProperties.contextImportProperties.0}}
/**
* Magic getter to lazy load subresources
*
* @param string $name Subresource to return
* @return ListResource The requested subresource
* @throws TwilioException For unknown subresources
*/
public function __get(string $name): ListResource {
if (\property_exists($this, '_' . $name)) {
$method = 'get' . \ucfirst($name);
return $this->$method();
}

throw new TwilioException('Unknown subresource ' . $name);
}

/**
* Magic caller to get resource contexts
*
* @param string $name Resource to return
* @param array $arguments Context parameters
* @return InstanceContext The requested resource context
* @throws TwilioException For unknown resource
*/
public function __call(string $name, array $arguments): InstanceContext {
$property = $this->$name;
if (\method_exists($property, 'getContext')) {
return \call_user_func_array(array($property, 'getContext'), $arguments);
}

throw new TwilioException('Resource does not have a context');
}

{{/metaProperties.contextImportProperties.0}}
{{#metaProperties.contextImportProperties.0}}{{>magicGetterandCaller}}
{{/metaProperties.contextImportProperties.0}}
{{^metaProperties.contextImportProperties.0}}
{{#metaProperties.contextImportMethods.0}}{{>magicGetterandCaller}}
{{/metaProperties.contextImportMethods.0}}
{{/metaProperties.contextImportProperties.0}}
/**
* Provide a friendly representation
*
Expand Down
41 changes: 6 additions & 35 deletions src/main/resources/twilio-php/list.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -101,41 +101,12 @@ class {{apiName}}List extends ListResource {
}

{{/metaProperties.listImportProperties}}
{{#metaProperties.listImportProperties.0}}
/**
* Magic getter to lazy load subresources
*
* @param string $name Subresource to return
* @return ListResource The requested subresource
* @throws TwilioException For unknown subresources
*/
public function __get(string $name): ListResource {
if (\property_exists($this, '_' . $name)) {
$method = 'get' . \ucfirst($name);
return $this->$method();
}

throw new TwilioException('Unknown subresource ' . $name);
}

/**
* Magic caller to get resource contexts
*
* @param string $name Resource to return
* @param array $arguments Context parameters
* @return InstanceContext The requested resource context
* @throws TwilioException For unknown resource
*/
public function __call(string $name, array $arguments): InstanceContext {
$property = $this->$name;
if (\method_exists($property, 'getContext')) {
return \call_user_func_array(array($property, 'getContext'), $arguments);
}

throw new TwilioException('Resource does not have a context');
}

{{/metaProperties.listImportProperties.0}}
{{#metaProperties.listImportProperties.0}}{{>magicGetterandCaller}}
{{/metaProperties.listImportProperties.0}}
{{^metaProperties.listImportProperties.0}}
{{#metaProperties.listImportMethods.0}}{{>magicGetterandCaller}}
{{/metaProperties.listImportMethods.0}}
{{/metaProperties.listImportProperties.0}}
/**
* Provide a friendly representation
*
Expand Down
32 changes: 32 additions & 0 deletions src/main/resources/twilio-php/magicGetterandCaller.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Magic getter to lazy load subresources
*
* @param string $name Subresource to return
* @return ListResource The requested subresource
* @throws TwilioException For unknown subresources
*/
public function __get(string $name): ListResource {
if (\property_exists($this, '_' . $name)) {
$method = 'get' . \ucfirst($name);
return $this->$method();
}

throw new TwilioException('Unknown subresource ' . $name);
}

/**
* Magic caller to get resource contexts
*
* @param string $name Resource to return
* @param array $arguments Context parameters
* @return InstanceContext The requested resource context
* @throws TwilioException For unknown resource
*/
public function __call(string $name, array $arguments): InstanceContext {
$property = $this->$name;
if (\method_exists($property, 'getContext')) {
return \call_user_func_array(array($property, 'getContext'), $arguments);
}

throw new TwilioException('Resource does not have a context');
}