diff --git a/src/Api/Abstracts/AbstractApiRequester.php b/src/Api/Abstracts/AbstractApiRequester.php index 53e7934..80dae96 100644 --- a/src/Api/Abstracts/AbstractApiRequester.php +++ b/src/Api/Abstracts/AbstractApiRequester.php @@ -4,15 +4,21 @@ namespace Gitea\Api\Abstracts; use Gitea\Client; +// Traits +use Gitea\Core\Traits\RequestChainable; + use Gitea\Api\Interfaces\ApiRequesterInterface; +use Gitea\Core\Interfaces\RequestChainableInterface; /** * Abstract class for Api classes * * @author Benjamin Blake (sitelease.ca) */ -abstract class AbstractApiRequester implements ApiRequesterInterface +abstract class AbstractApiRequester implements ApiRequesterInterface, RequestChainableInterface { + use RequestChainable; + /** * The client * @@ -42,7 +48,7 @@ abstract class AbstractApiRequester implements ApiRequesterInterface * @var array */ protected $defaultHeaders = [ - 'Accept' => 'application/json' + 'Accept' => 'application/json' ]; /** @@ -68,7 +74,7 @@ abstract class AbstractApiRequester implements ApiRequesterInterface * @var array */ protected $postDefaultHeaders = [ - 'Content-Type' => 'application/json' + 'Content-Type' => 'application/json' ]; /** @@ -78,7 +84,7 @@ abstract class AbstractApiRequester implements ApiRequesterInterface * @var array */ protected $putDefaultHeaders = [ - 'Content-Type' => 'application/json' + 'Content-Type' => 'application/json' ]; /** @@ -91,25 +97,66 @@ abstract class AbstractApiRequester implements ApiRequesterInterface /** * @param Client $client + * @param object|null $caller */ - public function __construct(Client $client, $authToken) + public function __construct(Client &$client , ?object $caller) { - $this->client = $client; - $this->authToken = $authToken; + $this->setClient($client); + $this->setCaller($caller); + $this->setAuthToken($client->getAuthToken()); // Set authorization headers and parameters + $authToken = $client->getAuthToken(); $this->getDefaultParameters["access_token"] = $authToken; $this->postDefaultHeaders["Authorization"] = "token $authToken"; $this->putDefaultHeaders["Authorization"] = "token $authToken"; $this->deleteDefaultHeaders["Authorization"] = "token $authToken"; } - public function getClient() { - return $this->client; + /** + * Get the gitea client (by reference) + * + * @author Benjamin Blake (sitelease.ca) + * + * @return Client + */ + public function getClient(): Client { + return $this->client; } - public function getAuthToken() { - return $this->authToken; + /** + * Set the gitea client (by reference) + * + * @author Benjamin Blake (sitelease.ca) + * @param Client $client + * @return self + */ + public function setClient(Client &$client): self { + $this->client = $client; + return $this; + } + + /** + * Set the authentication token + * + * @author Benjamin Blake (sitelease.ca) + * + * @return string + */ + public function getAuthToken(): string { + return $this->authToken; + } + + /** + * Set the authentication token + * + * @author Benjamin Blake (sitelease.ca) + * @param string $authToken + * @return self + */ + public function setAuthToken(string $authToken): self { + $this->authToken = $authToken; + return $this; } /** @@ -124,16 +171,16 @@ abstract class AbstractApiRequester implements ApiRequesterInterface * @return array */ public function getDefaultParametersForType($type = "all") { - if (!$type || $type == "all") { - return $this->defaultParameters; - } else { - $propertyName = $type."DefaultParameters"; - if (property_exists(__CLASS__, $propertyName) && is_array($this->$propertyName)) { - return array_merge($this->defaultParameters, $this->$propertyName); + if (!$type || $type == "all") { + return $this->defaultParameters; } else { - return []; + $propertyName = $type."DefaultParameters"; + if (property_exists(__CLASS__, $propertyName) && is_array($this->$propertyName)) { + return array_merge($this->defaultParameters, $this->$propertyName); + } else { + return []; + } } - } } /** @@ -148,16 +195,16 @@ abstract class AbstractApiRequester implements ApiRequesterInterface * @return array */ public function getDefaultHeadersForType($type = "all") { - if (!$type || $type == "all") { - return $this->defaultHeaders; - } else { - $propertyName = $type."DefaultHeaders"; - if (property_exists(__CLASS__, $propertyName) && is_array($this->$propertyName)) { - return array_merge($this->defaultParameters, $this->$propertyName); + if (!$type || $type == "all") { + return $this->defaultHeaders; } else { - return []; + $propertyName = $type."DefaultHeaders"; + if (property_exists(__CLASS__, $propertyName) && is_array($this->$propertyName)) { + return array_merge($this->defaultParameters, $this->$propertyName); + } else { + return []; + } } - } } /** @@ -177,29 +224,29 @@ abstract class AbstractApiRequester implements ApiRequesterInterface */ public function get($path, array $parameters = array(), $requestHeaders = array(), $debugRequest = false) { - $client = $this->getClient(); - $guzzleClient = $client->getGuzzleClient(); - $defaultParameters = $this->getDefaultParametersForType("get"); - $defaultHeaders = $this->getDefaultParametersForType("get"); + $client = $this->getClient(); + $guzzleClient = $client->getGuzzleClient(); + $defaultParameters = $this->getDefaultParametersForType("get"); + $defaultHeaders = $this->getDefaultParametersForType("get"); - // Create request options array - // and populate it with defaults - $requestOptions = []; - $requestOptions['query'] = $defaultParameters; - $requestOptions['headers'] = $defaultHeaders; - if ($debugRequest) { - $requestOptions['debug'] = true; - } + // Create request options array + // and populate it with defaults + $requestOptions = []; + $requestOptions['query'] = $defaultParameters; + $requestOptions['headers'] = $defaultHeaders; + if ($debugRequest) { + $requestOptions['debug'] = true; + } - if ($parameters) { - $requestOptions['query'] = array_merge($defaultParameters, $parameters); - } + if ($parameters) { + $requestOptions['query'] = array_merge($defaultParameters, $parameters); + } - if ($requestHeaders) { - $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); - } + if ($requestHeaders) { + $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); + } - return $guzzleClient->request('GET', $path, $requestOptions); + return $guzzleClient->request('GET', $path, $requestOptions); } /** @@ -210,34 +257,34 @@ abstract class AbstractApiRequester implements ApiRequesterInterface */ public function post($path, $body, $requestHeaders = array(), $debugRequest = false) { - $client = $this->getClient(); - $guzzleClient = $client->getGuzzleClient(); - $defaultHeaders = $this->getDefaultHeadersForType("post"); + $client = $this->getClient(); + $guzzleClient = $client->getGuzzleClient(); + $defaultHeaders = $this->getDefaultHeadersForType("post"); - // Create request options array - // and populate it with defaults - $requestOptions = []; - $requestOptions['headers'] = $defaultHeaders; - $requestOptions['body'] = "{}"; - if ($debugRequest) { - $requestOptions['debug'] = true; - } - - if ($body) { - if (is_object($body)) { - $requestOptions['body'] = json_encode($body); + // Create request options array + // and populate it with defaults + $requestOptions = []; + $requestOptions['headers'] = $defaultHeaders; + $requestOptions['body'] = "{}"; + if ($debugRequest) { + $requestOptions['debug'] = true; } - if (is_string($body)) { - $requestOptions['body'] = $body; + if ($body) { + if (is_object($body)) { + $requestOptions['body'] = json_encode($body); + } + + if (is_string($body)) { + $requestOptions['body'] = $body; + } } - } - if ($requestHeaders) { - $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); - } + if ($requestHeaders) { + $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); + } - return $guzzleClient->request('POST', $path, $requestOptions); + return $guzzleClient->request('POST', $path, $requestOptions); } /** @@ -248,34 +295,34 @@ abstract class AbstractApiRequester implements ApiRequesterInterface */ public function put($path, $body, $requestHeaders = array(), $debugRequest = false) { - $client = $this->getClient(); - $guzzleClient = $client->getGuzzleClient(); - $defaultHeaders = $this->getDefaultHeadersForType("put"); + $client = $this->getClient(); + $guzzleClient = $client->getGuzzleClient(); + $defaultHeaders = $this->getDefaultHeadersForType("put"); - // Create request options array - // and populate it with defaults - $requestOptions = []; - $requestOptions['headers'] = $defaultHeaders; - $requestOptions['body'] = "{}"; - if ($debugRequest) { - $requestOptions['debug'] = true; - } - - if ($body) { - if (is_object($body)) { - $requestOptions['body'] = json_encode($body); + // Create request options array + // and populate it with defaults + $requestOptions = []; + $requestOptions['headers'] = $defaultHeaders; + $requestOptions['body'] = "{}"; + if ($debugRequest) { + $requestOptions['debug'] = true; } - if (is_string($body)) { - $requestOptions['body'] = $body; + if ($body) { + if (is_object($body)) { + $requestOptions['body'] = json_encode($body); + } + + if (is_string($body)) { + $requestOptions['body'] = $body; + } } - } - if ($requestHeaders) { - $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); - } + if ($requestHeaders) { + $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); + } - return $guzzleClient->request('PUT', $path, $requestOptions); + return $guzzleClient->request('PUT', $path, $requestOptions); } /** @@ -285,22 +332,22 @@ abstract class AbstractApiRequester implements ApiRequesterInterface */ public function delete($path, $requestHeaders = array(), $debugRequest = false) { - $client = $this->getClient(); - $guzzleClient = $client->getGuzzleClient(); - $defaultHeaders = $this->getDefaultHeadersForType("delete"); + $client = $this->getClient(); + $guzzleClient = $client->getGuzzleClient(); + $defaultHeaders = $this->getDefaultHeadersForType("delete"); - // Create request options array - // and populate it with defaults - $requestOptions = []; - $requestOptions['headers'] = $defaultHeaders; - if ($debugRequest) { - $requestOptions['debug'] = true; - } + // Create request options array + // and populate it with defaults + $requestOptions = []; + $requestOptions['headers'] = $defaultHeaders; + if ($debugRequest) { + $requestOptions['debug'] = true; + } - if ($requestHeaders) { - $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); - } + if ($requestHeaders) { + $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); + } - return $guzzleClient->request('DELETE', $path, $requestOptions); + return $guzzleClient->request('DELETE', $path, $requestOptions); } } diff --git a/src/Api/Branches.php b/src/Api/Branches.php index 5321963..ebd1748 100644 --- a/src/Api/Branches.php +++ b/src/Api/Branches.php @@ -19,10 +19,10 @@ class Branches extends AbstractApiRequester * Example: * ``` * // Get all tags from a repository owned by a user - * $giteaClient->tags()->fromRepository("username", "test-repository"); + * $client->tags()->fromRepository("username", "test-repository"); * * // Get all tags from a repository owned by an organization - * $giteaClient->tags()->fromRepository("organizationName", "test-repository"); + * $client->tags()->fromRepository("organizationName", "test-repository"); * ``` * * @param string $owner The name of the user or organization that owns the repository @@ -46,7 +46,7 @@ class Branches extends AbstractApiRequester foreach ($jsonItemList as $jsonItem) { $encodedItem = json_encode($jsonItem); $itemObject = Branch::fromJson( - $this->getClient(), + $client, $this, json_decode($encodedItem) ); diff --git a/src/Api/Interfaces/ApiRequesterInterface.php b/src/Api/Interfaces/ApiRequesterInterface.php index 66bdaf3..e2f658d 100644 --- a/src/Api/Interfaces/ApiRequesterInterface.php +++ b/src/Api/Interfaces/ApiRequesterInterface.php @@ -13,12 +13,46 @@ interface ApiRequesterInterface { /** * @param Client $client + * @param object|null $caller */ - public function __construct(Client $client, $authToken); + public function __construct(Client &$client , ?object $caller); - public function getClient(); + /** + * Get the gitea client (by reference) + * + * @author Benjamin Blake (sitelease.ca) + * + * @return Client + */ + public function getClient(): Client; - public function getAuthToken(); + /** + * Set the gitea client (by reference) + * + * @author Benjamin Blake (sitelease.ca) + * @param Client $client + * @return self + */ + public function setClient(Client &$client): self; + + /** + * Get the authentication token + * + * @author Benjamin Blake (sitelease.ca) + * + * @return string + */ + public function getAuthToken(): string; + + + /** + * Set the authentication token + * + * @author Benjamin Blake (sitelease.ca) + * @param string $authToken + * @return self + */ + public function setAuthToken(string $authToken): self; /** * @return $this diff --git a/src/Api/Organizations.php b/src/Api/Organizations.php index a359b5b..053102b 100644 --- a/src/Api/Organizations.php +++ b/src/Api/Organizations.php @@ -18,7 +18,7 @@ class Organizations extends AbstractApiRequester * * Example: * ``` - * $giteaClient->organizations()->getByUsername($orgUsername); + * $client->organizations()->getByUsername($orgUsername); * ``` * * @param string $username @@ -34,7 +34,7 @@ class Organizations extends AbstractApiRequester $statusCode = $response->getStatusCode(); $body = $response->getBody(); if ($statusCode == 200) { - return Organization::fromJson(json_decode($body)); + return Organization::fromJson($client, $this, json_decode($body)); } else { return $response; } diff --git a/src/Api/Repositories.php b/src/Api/Repositories.php index ed351f7..e46f426 100644 --- a/src/Api/Repositories.php +++ b/src/Api/Repositories.php @@ -32,7 +32,7 @@ class Repositories extends AbstractAllApiRequester * * Example: * ``` - * $giteaClient->repositories()->search($keyword, $page); + * $client->repositories()->search($keyword, $page); * ``` * * @param string $keyword Keyword to search by @@ -63,7 +63,7 @@ class Repositories extends AbstractAllApiRequester foreach ($jsonItemList as $jsonItem) { $encodedItem = json_encode($jsonItem); $itemObject = Repository::fromJson( - $this->getClient(), + $client, $this, json_decode($encodedItem) ); @@ -83,7 +83,7 @@ class Repositories extends AbstractAllApiRequester * * Example: * ``` - * $giteaClient->repositories()->getByName($owner, $repoName); + * $client->repositories()->getByName($owner, $repoName); * ``` * * @param string $owner The owner of the repository @@ -99,7 +99,7 @@ class Repositories extends AbstractAllApiRequester $body = (string) $response->getBody(); if ($statusCode == 200) { return Repository::fromJson( - $this->getClient(), + $client, $this, json_decode($body) ); @@ -117,7 +117,7 @@ class Repositories extends AbstractAllApiRequester * * Example: * ``` - * $giteaClient->repositories()->getRawFile($owner, $repoName, "README.md"); + * $client->repositories()->getRawFile($owner, $repoName, "README.md"); * ``` * * @param string $owner The owner of the repository @@ -147,7 +147,7 @@ class Repositories extends AbstractAllApiRequester * * Example: * ``` - * $giteaClient->repositories()->downloadArchive($owner, $repoName, "master", ".zip"); + * $client->repositories()->downloadArchive($owner, $repoName, "master", ".zip"); * ``` * * @param string $owner The owner of the repository diff --git a/src/Api/Tags.php b/src/Api/Tags.php index 3551fa6..9b00926 100644 --- a/src/Api/Tags.php +++ b/src/Api/Tags.php @@ -19,10 +19,10 @@ class Tags extends AbstractApiRequester * Example: * ``` * // Get all tags from a repository owned by a user - * $giteaClient->tags()->fromRepository("username", "test-repository"); + * $client->tags()->fromRepository("username", "test-repository"); * * // Get all tags from a repository owned by an organization - * $giteaClient->tags()->fromRepository("organizationName", "test-repository"); + * $client->tags()->fromRepository("organizationName", "test-repository"); * ``` * * @param string $owner The name of the user or organization that owns the repository @@ -46,7 +46,7 @@ class Tags extends AbstractApiRequester foreach ($jsonItemList as $jsonItem) { $encodedItem = json_encode($jsonItem); $itemObject = Tag::fromJson( - $this->getClient(), + $client, $this, json_decode($encodedItem) ); diff --git a/src/Client.php b/src/Client.php index 879d55d..91bebe3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -14,10 +14,15 @@ use Gitea\Api\Repositories; use Gitea\Api\Branches; use Gitea\Api\Tags; -use \JsonSerializable; +// Traits +use Gitea\Core\Traits\RequestChainable; + +use Gitea\Core\Interfaces\RequestChainableInterface; /** Represents a Gitea push event. */ -class Client { +class Client implements RequestChainableInterface +{ + use RequestChainable; /** * Stores an instance of Guzzle's Client @@ -148,7 +153,7 @@ class Client { */ public function repositories() { - return new Repositories($this, $this->getAuthToken()); + return new Repositories($this, $this); } /** @@ -160,7 +165,7 @@ class Client { */ public function branches() { - return new Branches($this, $this->getAuthToken()); + return new Branches($this, $this); } /** @@ -172,7 +177,7 @@ class Client { */ public function tags() { - return new Tags($this, $this->getAuthToken()); + return new Tags($this, $this); } /** @@ -184,7 +189,7 @@ class Client { */ public function organizations() { - return new Organizations($this, $this->getAuthToken()); + return new Organizations($this, $this); } } diff --git a/src/Core/Interfaces/RequestChainableInterface.php b/src/Core/Interfaces/RequestChainableInterface.php new file mode 100644 index 0000000..acd4b7d --- /dev/null +++ b/src/Core/Interfaces/RequestChainableInterface.php @@ -0,0 +1,56 @@ +caller; + } + + /** + * Set the object that called this method + * + * @author Benjamin Blake (sitelease.ca) + * + * @return self + */ + public function setCaller($object): self + { + $this->caller = $object; + return $this; + } + + /** + * Return the request chain heirarchy + * as an array of objects + * + * This is useful if you need to know + * what objects where called in order to + * get the current object + * + * @author Benjamin Blake (sitelease.ca) + * @return array + */ + public function getRequestChain(): array + { + $requestChain = array(); + + // While the caller implements the RequestChainableInterface + // loop over it and add it to the request chain array + $caller = $this->getCaller(); + while ($caller instanceof RequestChainableInterface) { + $requestChain[] = $caller; + + if ($caller && !$caller instanceof Client) { + $caller = $caller->getCaller(); + } else { + break; + } + } + return $requestChain; + } + + /** + * Return the request chain heirarchy + * as a string of class names + * + * This is useful if you need to quickly print out + * a breadcrumb like heirarchy of callers + * + * @author Benjamin Blake (sitelease.ca) + * @return array + */ + public function debugRequestChain(): string + { + $requestChainDebug = ""; + $requestChain = $this->getRequestChain(); + + while (count($requestChain) > 0) { + $callerObj = array_shift($requestChain); + if (count($requestChain) != 0) { + $requestChainDebug .= get_class($callerObj)." -> "; + } else { + $requestChainDebug .= get_class($callerObj); + } + } + + return $requestChainDebug; + } + +} diff --git a/src/Model/Abstracts/AbstractApiModel.php b/src/Model/Abstracts/AbstractApiModel.php index e06f840..b55da12 100644 --- a/src/Model/Abstracts/AbstractApiModel.php +++ b/src/Model/Abstracts/AbstractApiModel.php @@ -6,27 +6,40 @@ use Gitea\Client; use Gitea\Api\Interfaces\ApiRequesterInterface; use \stdClass; - -use Gitea\Model\Interfaces\ApiModelInterface; use \JsonSerializable; -abstract class AbstractApiModel implements ApiModelInterface, JsonSerializable +// Traits +use Gitea\Core\Traits\RequestChainable; + +use Gitea\Model\Interfaces\ApiModelInterface; +use Gitea\Core\Interfaces\RequestChainableInterface; + +abstract class AbstractApiModel implements ApiModelInterface, JsonSerializable, RequestChainableInterface { + + use RequestChainable; + + /** + * Creates a new API model object + * + * @author Benjamin Blake (sitelease.ca) + * + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method + * @param mixed $args The organization visibility. + */ + public function __construct(Client &$client , ?object $caller, ...$args) { + $this->setClient($client); + $this->setCaller($caller); + } + /** * The Gitea client that originally made the * Api request for this object * * @var Client */ - protected $giteaClient; - - /** - * The Api request object that created - * this model object - * - * @var ApiRequesterInterface - */ - protected $apiRequester; + protected $client; /** * Create a new API model object from a JSON map object @@ -34,19 +47,19 @@ abstract class AbstractApiModel implements ApiModelInterface, JsonSerializable * Example: * ``` * ClassName::fromJson( - * $giteaClient, - * $apiRequester, + * $client, + * $this, * json_decode($jsonString) * ); * ``` * * @author Benjamin Blake (sitelease.ca) * - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON data object */ - static function fromJson(object $giteaClient, object $apiRequester, object $map) { + static function fromJson(object &$client , ?object $caller, object $map) { trigger_error("The abstract 'fromJson()' method must be overwritten"); return false; } @@ -65,38 +78,25 @@ abstract class AbstractApiModel implements ApiModelInterface, JsonSerializable } /** - * Get the Gitea client that originally made the - * Api request for this object + * Get the gitea client (by reference) * * @author Benjamin Blake (sitelease.ca) * * @return Client */ - public function getGiteaClient() { - return $this->giteaClient; - } - - public function setGiteaClient($object): self { - $this->giteaClient = $object; - return $this; + public function getClient(): Client { + return $this->client; } /** - * Get the Api request object that created - * this model object + * Set the gitea client (by reference) * * @author Benjamin Blake (sitelease.ca) - * - * @return ApiRequesterInterface + * @param Client $client + * @return self */ - public function getApiRequester() { - return $this->apiRequester; - } - - public function setApiRequester($object): self { - $this->apiRequester = $object; + public function setClient(Client &$client): self { + $this->client = $client; return $this; } - - } diff --git a/src/Model/Branch.php b/src/Model/Branch.php index a9688a1..23351bd 100644 --- a/src/Model/Branch.php +++ b/src/Model/Branch.php @@ -5,6 +5,7 @@ namespace Gitea\Model; use GuzzleHttp\Psr7\Uri; use Psr\Http\Message\UriInterface; +use Gitea\Client; use Gitea\Model\PayloadCommit; use \InvalidArgumentException; @@ -32,13 +33,12 @@ class Branch extends AbstractApiModel { /** * Creates a new branch - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param string $name The branch name */ - function __construct(object $giteaClient, object $apiRequester, ...$args) { - $this->setGiteaClient($giteaClient); - $this->setApiRequester($apiRequester); + public function __construct(Client &$client , ?object $caller, ...$args) { + parent::__construct($client, $caller, $args); if (count($args) >= 1) { $name = $args[0]; if (!is_string($name)) { @@ -54,20 +54,20 @@ class Branch extends AbstractApiModel { /** * Creates a new branch from the specified JSON map. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON map representing an branch. * @return static The instance corresponding to the specified JSON map. */ - static function fromJson(object $giteaClient, object $apiRequester, object $map): self { + static function fromJson(object &$client , ?object $caller, object $map): self { return ( new static( - $giteaClient, - $apiRequester, + $client, + $caller, isset($map->name) && is_string($map->name) ? $map->name : '' ) ) - ->setCommit(isset($map->commit) && is_object($map->commit) ? PayloadCommit::fromJson($giteaClient, $apiRequester, $map->commit) : null) + ->setCommit(isset($map->commit) && is_object($map->commit) ? PayloadCommit::fromJson($client, null, $map->commit) : null) ->setProtected(isset($map->protected) && is_bool($map->protected) ? $map->protected : true) ->setCanUserPush(isset($map->user_can_push) && is_bool($map->user_can_push) ? $map->user_can_push : false) ->setUserCanMerge(isset($map->user_can_merge) && is_bool($map->user_can_merge) ? $map->user_can_merge : false); diff --git a/src/Model/Interfaces/ApiModelInterface.php b/src/Model/Interfaces/ApiModelInterface.php index e19fc0d..1fbab7f 100644 --- a/src/Model/Interfaces/ApiModelInterface.php +++ b/src/Model/Interfaces/ApiModelInterface.php @@ -15,11 +15,11 @@ interface ApiModelInterface * * @author Benjamin Blake (sitelease.ca) * - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param mixed $args The organization visibility. */ - public function __construct(object $giteaClient, object $apiRequester, ...$args); + public function __construct(Client &$client , ?object $caller, ...$args); /** * Create a new API model object from a JSON map object @@ -27,19 +27,19 @@ interface ApiModelInterface * Example: * ``` * ClassName::fromJson( - * $giteaClient, - * $apiRequester, + * $client, + * $this, * json_decode($jsonString) * ); * ``` * * @author Benjamin Blake (sitelease.ca) * - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON data object */ - static function fromJson(object $giteaClient, object $apiRequester, object $map); + static function fromJson(object &$client , ?object $caller, object $map); /** * Convert this Api model object to a JSON map. @@ -54,24 +54,21 @@ interface ApiModelInterface public function jsonSerialize(): \stdClass; /** - * Get the Gitea client that originally made the - * Api request for this object + * Get the gitea client (by reference) * * @author Benjamin Blake (sitelease.ca) * * @return Client */ - public function getGiteaClient(); + public function getClient(): Client; /** - * Get the Api request object that created - * this model object + * Set the gitea client (by reference) * * @author Benjamin Blake (sitelease.ca) - * - * @return ApiRequesterInterface + * @param Client $client + * @return self */ - public function getApiRequester(); - + public function setClient(Client &$client): self; } diff --git a/src/Model/Organization.php b/src/Model/Organization.php index cbb1d6d..7e10160 100644 --- a/src/Model/Organization.php +++ b/src/Model/Organization.php @@ -5,6 +5,8 @@ namespace Gitea\Model; use GuzzleHttp\Psr7\Uri; use Psr\Http\Message\UriInterface; +use Gitea\Client; + use \InvalidArgumentException; use Gitea\Model\Abstracts\AbstractApiModel; @@ -38,14 +40,13 @@ class Organization extends AbstractApiModel { /** * Creates a new organization. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param string $username The organization name. * @param string $visibility The organization visibility. */ - function __construct(object $giteaClient, object $apiRequester, ...$args) { - $this->setGiteaClient($giteaClient); - $this->setApiRequester($apiRequester); + public function __construct(Client &$client , ?object $caller, ...$args) { + parent::__construct($client, $caller, $args); if (count($args) >= 2) { $username = $args[0]; $visibility = $args[1]; @@ -67,14 +68,16 @@ class Organization extends AbstractApiModel { /** * Creates a new organization from the specified JSON map. + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON map representing an organization. * @return static The instance corresponding to the specified JSON map. */ - static function fromJson(object $giteaClient, object $apiRequester, object $map): self { + static function fromJson(object &$client , ?object $caller, object $map): self { return ( new static( - $giteaClient, - $apiRequester, + $client, + $caller, isset($map->username) && is_string($map->username) ? $map->username : '', isset($map->visibility) && is_string($map->visibility) ? $map->visibility : 'private' ) diff --git a/src/Model/Owner.php b/src/Model/Owner.php index 0aa3df3..5743b34 100644 --- a/src/Model/Owner.php +++ b/src/Model/Owner.php @@ -5,6 +5,8 @@ namespace Gitea\Model; use GuzzleHttp\Psr7\Uri; use Psr\Http\Message\UriInterface; +use Gitea\Client; + use Gitea\Model\Abstracts\AbstractApiModel; /** Represents a Gitea owner. */ @@ -36,14 +38,13 @@ class Owner extends AbstractApiModel { /** * Creates a new owner. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param int $id The owner identifier. * @param string $login The name of the Gitea account. */ - function __construct(object $giteaClient, object $apiRequester, ...$args) { - $this->setGiteaClient($giteaClient); - $this->setApiRequester($apiRequester); + public function __construct(Client &$client , ?object $caller, ...$args) { + parent::__construct($client, $caller, $args); if (count($args) >= 2) { $id = $args[0]; $login = $args[1]; @@ -65,16 +66,16 @@ class Owner extends AbstractApiModel { /** * Creates a new owner from the specified JSON map. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON map representing a owner. * @return static The instance corresponding to the specified JSON map. */ - static function fromJson(object $giteaClient, object $apiRequester, object $map): self { + static function fromJson(object &$client , ?object $caller, object $map): self { return ( new static( - $giteaClient, - $apiRequester, + $client, + $caller, isset($map->id) && is_int($map->id) ? $map->id : -1, isset($map->login) && is_string($map->login) ? $map->login : '' ) diff --git a/src/Model/PayloadCommit.php b/src/Model/PayloadCommit.php index 5ff26d1..6c98f5b 100644 --- a/src/Model/PayloadCommit.php +++ b/src/Model/PayloadCommit.php @@ -5,6 +5,7 @@ namespace Gitea\Model; use GuzzleHttp\Psr7\Uri; use Psr\Http\Message\UriInterface; +use Gitea\Client; use Gitea\Model\PayloadUser; use Gitea\Model\PayloadCommitVerification; @@ -38,14 +39,13 @@ class PayloadCommit extends AbstractApiModel { /** * Creates a new payload commit. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param string $id The commit hash. * @param string $message The commit message. */ - function __construct(object $giteaClient, object $apiRequester, ...$args) { - $this->setGiteaClient($giteaClient); - $this->setApiRequester($apiRequester); + public function __construct(Client &$client , ?object $caller, ...$args) { + parent::__construct($client, $caller, $args); if (count($args) >= 2) { $id = $args[0]; $message = $args[1]; @@ -67,25 +67,25 @@ class PayloadCommit extends AbstractApiModel { /** * Creates a new commit from the specified JSON map. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON map representing a commit. * @return static The instance corresponding to the specified JSON map. */ - static function fromJson(object $giteaClient, object $apiRequester, object $map): self { + static function fromJson(object &$client , ?object $caller, object $map): self { return ( new static( - $giteaClient, - $apiRequester, + $client, + $caller, isset($map->id) && is_string($map->id) ? $map->id : '', isset($map->message) && is_string($map->message) ? $map->message : '' ) ) - ->setAuthor(isset($map->author) && is_object($map->author) ? PayloadUser::fromJson($giteaClient, $apiRequester, $map->author) : null) - ->setCommitter(isset($map->committer) && is_object($map->committer) ? PayloadUser::fromJson($giteaClient, $apiRequester, $map->committer) : null) + ->setAuthor(isset($map->author) && is_object($map->author) ? PayloadUser::fromJson($client, null, $map->author) : null) + ->setCommitter(isset($map->committer) && is_object($map->committer) ? PayloadUser::fromJson($client, null, $map->committer) : null) ->setTimestamp(isset($map->timestamp) && is_string($map->timestamp) ? new \DateTime($map->timestamp) : null) ->setUrl(isset($map->url) && is_string($map->url) ? new Uri($map->url) : null) - ->setVerification(isset($map->verification) && is_object($map->verification) ? PayloadCommitVerification::fromJson($giteaClient, $apiRequester, $map->verification) : null); + ->setVerification(isset($map->verification) && is_object($map->verification) ? PayloadCommitVerification::fromJson($client, null, $map->verification) : null); } /** diff --git a/src/Model/PayloadCommitVerification.php b/src/Model/PayloadCommitVerification.php index 5799a79..fd54bf1 100644 --- a/src/Model/PayloadCommitVerification.php +++ b/src/Model/PayloadCommitVerification.php @@ -5,6 +5,8 @@ namespace Gitea\Model; use GuzzleHttp\Psr7\Uri; use Psr\Http\Message\UriInterface; +use Gitea\Client; + use \InvalidArgumentException; use Gitea\Model\Abstracts\AbstractApiModel; @@ -26,14 +28,12 @@ class PayloadCommitVerification extends AbstractApiModel { /** * Creates a new verification of a payload commit. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param bool $isVerified Value indicating whether the verification has succeeded. */ - function __construct(object $giteaClient, object $apiRequester, ...$args) { - - $this->setGiteaClient($giteaClient); - $this->setApiRequester($apiRequester); + public function __construct(Client &$client , ?object $caller, ...$args) { + parent::__construct($client, $caller, $args); if (count($args) >= 1) { $isVerified = $args[0]; if (!is_bool($isVerified)) { @@ -49,16 +49,16 @@ class PayloadCommitVerification extends AbstractApiModel { /** * Creates a new commit from the specified JSON map. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON map representing a commit. * @return static The instance corresponding to the specified JSON map. */ - static function fromJson(object $giteaClient, object $apiRequester, object $map): self { + static function fromJson(object &$client , ?object $caller, object $map): self { return ( new static( - $giteaClient, - $apiRequester, + $client, + $caller, isset($map->verified) && is_bool($map->verified) ? $map->verified : false ) ) diff --git a/src/Model/PayloadUser.php b/src/Model/PayloadUser.php index 56b524e..66c30c0 100644 --- a/src/Model/PayloadUser.php +++ b/src/Model/PayloadUser.php @@ -5,6 +5,8 @@ namespace Gitea\Model; use GuzzleHttp\Psr7\Uri; use Psr\Http\Message\UriInterface; +use Gitea\Client; + use \stdClass; use \InvalidArgumentException; @@ -24,13 +26,12 @@ class PayloadUser extends AbstractApiModel { /** * Creates a new payload user. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param string $username The name of the Gitea account. */ - function __construct(object $giteaClient, object $apiRequester, ...$args) { - $this->setGiteaClient($giteaClient); - $this->setApiRequester($apiRequester); + public function __construct(Client &$client , ?object $caller, ...$args) { + parent::__construct($client, $caller, $args); if (count($args) >= 1) { $username = $args[0]; if (!is_string($username)) { @@ -46,16 +47,16 @@ class PayloadUser extends AbstractApiModel { /** * Creates a new user from the specified JSON map. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON map representing a user. * @return static The instance corresponding to the specified JSON map. */ - static function fromJson(object $giteaClient, object $apiRequester, object $map): self { + static function fromJson(object &$client , ?object $caller, object $map): self { return ( new static( - $giteaClient, - $apiRequester, + $client, + $caller, isset($map->username) && is_string($map->username) ? $map->username : '' ) ) diff --git a/src/Model/Permission.php b/src/Model/Permission.php index 5925bba..2249664 100644 --- a/src/Model/Permission.php +++ b/src/Model/Permission.php @@ -5,6 +5,8 @@ namespace Gitea\Model; use GuzzleHttp\Psr7\Uri; use Psr\Http\Message\UriInterface; +use Gitea\Client; + use \InvalidArgumentException; use Gitea\Model\Abstracts\AbstractApiModel; @@ -23,15 +25,14 @@ class Permission extends AbstractApiModel { /** * Creates a new permission. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param bool $admin Value indicating whether administrator access is allowed. * @param bool $pull Value indicating whether pull is allowed. * @param bool $push Value indicating whether push is allowed. */ - function __construct(object $giteaClient, object $apiRequester, ...$args) { - $this->setGiteaClient($giteaClient); - $this->setApiRequester($apiRequester); + public function __construct(Client &$client , ?object $caller, ...$args) { + parent::__construct($client, $caller, $args); if (count($args) >= 2) { $admin = $args[0]; $pull = $args[1]; @@ -57,15 +58,15 @@ class Permission extends AbstractApiModel { /** * Creates a new user from the specified JSON map. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON map representing a user. * @return static The instance corresponding to the specified JSON map. */ - static function fromJson(object $giteaClient, object $apiRequester, object $map): self { + static function fromJson(object &$client , ?object $caller, object $map): self { return new static( - $giteaClient, - $apiRequester, + $client, + $caller, isset($map->admin) && is_bool($map->admin) ? $map->admin : false, isset($map->pull) && is_bool($map->pull) ? $map->pull : false, isset($map->push) && is_bool($map->push) ? $map->push : false diff --git a/src/Model/Repository.php b/src/Model/Repository.php index a82ebb8..781386f 100644 --- a/src/Model/Repository.php +++ b/src/Model/Repository.php @@ -1,9 +1,11 @@ setGiteaClient($giteaClient); - $this->setApiRequester($apiRequester); + public function __construct(Client &$client , ?object $caller, ...$args) { + parent::__construct($client, $caller, $args); if (count($args) >= 2) { $id = $args[0]; $fullName = $args[1]; @@ -117,14 +118,16 @@ class Repository extends AbstractApiModel { /** * Creates a new repository from the specified JSON map. + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON map representing a repository. * @return static The instance corresponding to the specified JSON map. */ - static function fromJson(object $giteaClient, object $apiRequester, object $map): self { + static function fromJson(object &$client , ?object $caller, object $map): self { return ( new static( - $giteaClient, - $apiRequester, + $client, + $caller, isset($map->id) && is_int($map->id) ? $map->id : -1, isset($map->full_name) && is_string($map->full_name) ? $map->full_name : '' ) @@ -140,9 +143,9 @@ class Repository extends AbstractApiModel { ->setMirror(isset($map->mirror) && is_bool($map->mirror) ? $map->mirror : false) ->setName(isset($map->name) && is_string($map->name) ? $map->name : '') ->setOpenIssuesCount(isset($map->open_issues_count) && is_int($map->open_issues_count) ? $map->open_issues_count : 0) - ->setOwner(isset($map->owner) && is_object($map->owner) ? Owner::fromJson($giteaClient, $apiRequester, $map->owner) : null) - ->setParent(isset($map->parent) && is_object($map->parent) ? Repository::fromJson($giteaClient, $apiRequester, $map->parent) : null) - ->setPermissions(isset($map->permissions) && is_object($map->permissions) ? Permission::fromJson($giteaClient, $apiRequester, $map->permissions) : null) + ->setOwner(isset($map->owner) && is_object($map->owner) ? Owner::fromJson($client, null, $map->owner) : null) + ->setParent(isset($map->parent) && is_object($map->parent) ? Repository::fromJson($client, null, $map->parent) : null) + ->setPermissions(isset($map->permissions) && is_object($map->permissions) ? Permission::fromJson($client, null, $map->permissions) : null) ->setPrivate(isset($map->private) && is_bool($map->private) ? $map->private : false) ->setSize(isset($map->size) && is_int($map->size) ? $map->size : 0) ->setSshUrl(isset($map->ssh_url) && is_string($map->ssh_url) ? new Uri($map->ssh_url) : null) @@ -602,8 +605,8 @@ class Repository extends AbstractApiModel { { $owner = $this->getOwner(); if ($owner) { - $client = $this->getGiteaClient(); - $repositoriesApi = new Repositories($client, $client->getAuthToken()); + $client = $this->getClient(); + $repositoriesApi = new Repositories($client, $this); return $repositoriesApi->downloadArchive($owner->getUsername(), $this->getName(), $gitRef, $format); } } @@ -619,8 +622,8 @@ class Repository extends AbstractApiModel { { $owner = $this->getOwner(); if ($owner) { - $client = $this->getGiteaClient(); - $branchesApi = new Branches($client, $client->getAuthToken()); + $client = $this->getClient(); + $branchesApi = new Branches($client, $this); return $branchesApi->fromRepository($owner->getUsername(), $this->getName()); } } @@ -636,8 +639,8 @@ class Repository extends AbstractApiModel { { $owner = $this->getOwner(); if ($owner) { - $client = $this->getGiteaClient(); - $tagsApi = new Tags($client, $client->getAuthToken()); + $client = $this->getClient(); + $tagsApi = new Tags($client, $this); return $tagsApi->fromRepository($owner->getUsername(), $this->getName()); } } diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 5155ab9..7539212 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -5,6 +5,8 @@ namespace Gitea\Model; use GuzzleHttp\Psr7\Uri; use Psr\Http\Message\UriInterface; +use Gitea\Client; + use \InvalidArgumentException; use Gitea\Model\Abstracts\AbstractApiModel; @@ -32,14 +34,13 @@ class Tag extends AbstractApiModel { /** * Creates a new tag - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param int $id The tag identifier * @param string $name The tag name */ - function __construct(object $giteaClient, object $apiRequester, ...$args) { - $this->setGiteaClient($giteaClient); - $this->setApiRequester($apiRequester); + public function __construct(Client &$client , ?object $caller, ...$args) { + parent::__construct($client, $caller, $args); if (count($args) >= 2) { $id = $args[0]; $name = $args[1]; @@ -61,15 +62,15 @@ class Tag extends AbstractApiModel { /** * Creates a new tag from the specified JSON map. - * @param object $giteaClient The Gitea client that originally made the request for this object's data - * @param object $apiRequester The Api requester that created this object + * @param object $client The Gitea client that originally made the request for this object's data + * @param object|null $caller The object that called this method * @param object $map A JSON map representing an tag. * @return static The instance corresponding to the specified JSON map. */ - static function fromJson(object $giteaClient, object $apiRequester, object $map): self { + static function fromJson(object &$client , ?object $caller, object $map): self { $newTag = new static( - $giteaClient, - $apiRequester, + $client, + $caller, isset($map->id) && is_int($map->id) ? $map->id : -1, isset($map->name) && is_string($map->name) ? $map->name : '' ); diff --git a/src/TestRun.php b/src/TestRun.php index 5c03e02..dad2bc0 100644 --- a/src/TestRun.php +++ b/src/TestRun.php @@ -43,7 +43,8 @@ $giteaClient->setAuthToken('32e609ad39539c1d0e8544800bd49b1025ac6b49'); // $tags = $repository->tags(); // if ($tags && count($tags) > 0) { // foreach ($tags as $tag) { -// var_dump(json_encode($tag)); +// print("Processing ".$tag->getName()."\n"); +// // var_dump(json_encode($tag)); // print("\n\n"); // } // print("Total Tags: ".count($tags)."\n"); @@ -52,20 +53,25 @@ $giteaClient->setAuthToken('32e609ad39539c1d0e8544800bd49b1025ac6b49'); // print("The repository could not be retrieved"."\n"); // } -// print("Getting all branches for the 'sl-product-catalog' repository \n\n"); -// $repository = $giteaClient->repositories()->getByName("Sitelease", "sl-theme-recipe"); -// if ($repository) { -// $branches = $repository->branches(); -// if ($branches && count($branches) > 0) { -// //foreach ($branches as $branch) { -// var_dump($branches->getItem("master")); -// print("\n\n"); -// //} -// print("Total Branches: ".count($branches)."\n"); -// } -// } else { -// print("The repository could not be retrieved"."\n"); -// } +print("Getting all branches for the 'sl-product-catalog' repository \n\n"); +$repository = $giteaClient->repositories()->getByName("Sitelease", "sl-theme-recipe"); +if ($repository) { + $branches = $repository->branches(); + if ($branches && count($branches) > 0) { + foreach ($branches as $branch) { + // var_dump(json_encode($branch)); + print("Processing ".$branch->getName()."\n"); + $debugChain = $branch->debugRequestChain(); + if ($debugChain) { + print("Chain ".$debugChain."\n"); + } + print("\n\n"); + } + print("Total Branches: ".count($branches)."\n"); + } +} else { + print("The repository could not be retrieved"."\n"); +} // print("Getting contents of \"composer.json\" file \n\n"); // $rawFile = $giteaClient->repositories()->getRawFile("Sitelease", "sl-theme-recipe", "composer.json"); @@ -94,17 +100,17 @@ $giteaClient->setAuthToken('32e609ad39539c1d0e8544800bd49b1025ac6b49'); // print("The repository could not be retrieved"."\n"); // } -print("Getting archive for the 'sl-product-catalog' repository \n\n"); -$repository = $giteaClient->repositories()->getByName("Sitelease", "sl-theme-recipe"); -if ($repository) { - $archive = $repository->archive("master"); - if ($archive) { - print("Downloading Archive...\n"); - file_put_contents("master.tar.gz", $archive); - print("Success!\n"); - } -} else { - print("The archive could not be retrieved"."\n"); -} +// print("Getting archive for the 'sl-product-catalog' repository \n\n"); +// $repository = $giteaClient->repositories()->getByName("Sitelease", "sl-theme-recipe"); +// if ($repository) { +// $archive = $repository->archive("master"); +// if ($archive) { +// print("Downloading Archive...\n"); +// file_put_contents("master.tar.gz", $archive); +// print("Success!\n"); +// } +// } else { +// print("The archive could not be retrieved"."\n"); +// } print("Exiting script"."\n");