Skip to content
Open
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
32 changes: 28 additions & 4 deletions src/WpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"');
}
}
Expand All @@ -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);
}
}