From e5152db6aeccfb4b513a138af092fc8349245122 Mon Sep 17 00:00:00 2001 From: Zoltan Szabo Date: Tue, 23 Oct 2018 20:49:06 +0300 Subject: [PATCH 1/2] [Feature] Multiple namespaces to look for endpoints --- src/WpClient.php | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/WpClient.php b/src/WpClient.php index c35ebe0..e32b1b0 100644 --- a/src/WpClient.php +++ b/src/WpClient.php @@ -45,6 +45,18 @@ class WpClient */ private $endPoints = []; + /** + * An array of namespaces that are searched for the requested endpoint. + * By default it contains the default namespace only. + * Additional namespaces can be added using the method addEndpointNamespace(). + * Newly added namespaces are always prepended, thus will have priority over already existing namespaces. + * This also means that the default namespace has the lowest priority. + * @var array + */ + private $endpointNamespaces = [ + 'Vnn\WpApiClient\Endpoint\\', + ]; + /** * WpClient constructor. * @param ClientInterface $httpClient @@ -80,10 +92,13 @@ public function setCredentials(AuthInterface $auth) public function __call($endpoint, array $args) { if (!isset($this->endPoints[$endpoint])) { - $class = 'Vnn\WpApiClient\Endpoint\\' . ucfirst($endpoint); - if (class_exists($class)) { - $this->endPoints[$endpoint] = new $class($this); - } else { + foreach($this->endpointNamespaces as $namespace) { + $class = $namespace . ucfirst($endpoint); + if (class_exists($class)) { + $this->endPoints[$endpoint] = new $class($this); + } + } + if (!isset($this->endPoints[$endpoint])) { throw new RuntimeException('Endpoint "' . $endpoint . '" does not exist"'); } } @@ -107,4 +122,13 @@ public function send(RequestInterface $request) return $this->httpClient->send($request); } + + /** + * Add a new endpoint namespace that is to be searched for endpoints. + * @param string $namespace + */ + public function addEndpointNamespace($namespace) + { + array_unshift($this->endpointNamespaces, $namespace); + } } From 3fb94af3d0f92a95726c3d2b6b02929500aa37f3 Mon Sep 17 00:00:00 2001 From: Zoltan Szabo Date: Wed, 24 Oct 2018 14:56:49 +0300 Subject: [PATCH 2/2] [Fix] PSR-2 compatibility issue --- src/WpClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WpClient.php b/src/WpClient.php index e32b1b0..c5bc9a3 100644 --- a/src/WpClient.php +++ b/src/WpClient.php @@ -92,7 +92,7 @@ public function setCredentials(AuthInterface $auth) public function __call($endpoint, array $args) { if (!isset($this->endPoints[$endpoint])) { - foreach($this->endpointNamespaces as $namespace) { + foreach ($this->endpointNamespaces as $namespace) { $class = $namespace . ucfirst($endpoint); if (class_exists($class)) { $this->endPoints[$endpoint] = new $class($this);