-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Endpoints added + Code improvements
Showing
13 changed files
with
452 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
namespace PluboRoutes\Endpoint; | ||
|
||
/** | ||
* An endpoint with DELETE method. | ||
* | ||
*/ | ||
final class DeleteEndpoint extends Endpoint | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* @param string $namespace | ||
* @param string $path | ||
* @param callable $config | ||
*/ | ||
public function __construct(string $namespace, string $path, callable $config, callable $permission_callback = null) | ||
{ | ||
parent::__construct($namespace, $path, $config, $permission_callback); | ||
$this->method = \WP_REST_Server::DELETABLE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
namespace PluboRoutes\Endpoint; | ||
|
||
/** | ||
* An Endpoint describes a route and its parameters. | ||
* | ||
*/ | ||
abstract class Endpoint implements EndpointInterface | ||
{ | ||
/** | ||
* The endpoint namespace. | ||
* | ||
* @var string | ||
*/ | ||
private $namespace; | ||
|
||
/** | ||
* The URL path that the endpoint needs to match. | ||
* | ||
* @var string | ||
*/ | ||
private $path; | ||
|
||
/** | ||
* The configuration of the endpoint. | ||
* | ||
* @var array | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* The permission callback of the endpoint. | ||
* | ||
* @var array | ||
*/ | ||
private $permission_callback; | ||
|
||
/** | ||
* The method of the endpoint. | ||
* | ||
* @var string | ||
*/ | ||
private $method; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $namespace | ||
* @param string $path | ||
* @param callable $config | ||
* @param string $method | ||
*/ | ||
public function __construct(string $namespace, string $path, callable $config, callable $permission_callback = null) | ||
{ | ||
$this->namespace = $namespace; | ||
$this->path = $path; | ||
$this->config = $config; | ||
$this->permission_callback = $permission_callback ?? '__return_true'; | ||
$this->args = array(); | ||
} | ||
|
||
/** | ||
* Get the namespace of the endpoint. | ||
* | ||
* @return string | ||
*/ | ||
public function getNamespace() | ||
{ | ||
return $this->namespace; | ||
} | ||
|
||
/** | ||
* Get the path to be matched. | ||
* | ||
* @return string | ||
*/ | ||
public function getPath() | ||
{ | ||
return $this->path; | ||
} | ||
|
||
/** | ||
* Get the config parameters of the endpoint. | ||
* | ||
* @return array | ||
*/ | ||
public function getConfig() | ||
{ | ||
return $this->config; | ||
} | ||
|
||
/** | ||
* Get the method of the endpoint. | ||
* | ||
* @return string | ||
*/ | ||
public function getMethod() | ||
{ | ||
return $this->method; | ||
} | ||
|
||
/** | ||
* Get the endpoint permission callback. | ||
* | ||
* @return callable | ||
*/ | ||
public function getPermissionCallback() | ||
{ | ||
return $this->permission_callback; | ||
} | ||
|
||
/** | ||
* Serialize the endpoint. | ||
* | ||
* @return string | ||
*/ | ||
public function serialize() | ||
{ | ||
return serialize(array($this->path, $this->method)); | ||
} | ||
|
||
/** | ||
* Unserialize the endpoint. | ||
* | ||
* @param array | ||
*/ | ||
public function unserialize($data) | ||
{ | ||
$data = unserialize($data); | ||
$this->path = $data['path']; | ||
$this->method = $data['method']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
namespace PluboRoutes\Endpoint; | ||
|
||
/** | ||
* Route Interface. | ||
* | ||
*/ | ||
interface EndpointInterface extends \Serializable | ||
{ | ||
public function getNamespace(); | ||
public function getPath(); | ||
public function getConfig(); | ||
public function getPermissionCallback(); | ||
public function getMethod(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
namespace PluboRoutes\Endpoint; | ||
|
||
/** | ||
* An Endpoint with GET method. | ||
* | ||
*/ | ||
final class GetEndpoint extends Endpoint | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* @param string $namespace | ||
* @param string $path | ||
* @param callable $config | ||
*/ | ||
public function __construct(string $namespace, string $path, callable $config, callable $permission_callback = null) | ||
{ | ||
parent::__construct($namespace, $path, $config, $permission_callback); | ||
$this->method = \WP_REST_Server::READABLE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
namespace PluboRoutes\Endpoint; | ||
|
||
/** | ||
* An Endpoint with POST method. | ||
* | ||
*/ | ||
final class PostEndpoint extends Endpoint | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* @param string $namespace | ||
* @param string $path | ||
* @param callable $config | ||
*/ | ||
public function __construct(string $namespace, string $path, callable $config, callable $permission_callback = null) | ||
{ | ||
parent::__construct($namespace, $path, $config, $permission_callback); | ||
$this->method = \WP_REST_Server::CREATABLE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
namespace PluboRoutes\Endpoint; | ||
|
||
/** | ||
* An Endpoint with PUT method. | ||
* | ||
*/ | ||
final class PutEndpoint extends Endpoint | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* @param string $namespace | ||
* @param string $path | ||
* @param callable $config | ||
*/ | ||
public function __construct(string $namespace, string $path, callable $config, callable $permission_callback = null) | ||
{ | ||
parent::__construct($namespace, $path, $config, $permission_callback); | ||
$this->method = 'PUT'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
namespace PluboRoutes\Helpers; | ||
|
||
class RegexHelperEndpoints extends RegexHelper | ||
{ | ||
public static function getRegex($type): string | ||
{ | ||
$regex_code = array_key_exists($type[1], self::AVAILABLE_REGEX) ? self::AVAILABLE_REGEX[$type[1]] : $type[1]; | ||
return "(?P<$type[0]>$regex_code)"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace PluboRoutes\Helpers; | ||
|
||
class RegexHelperRoutes extends RegexHelper | ||
{ | ||
public static function getRegex($type): string | ||
{ | ||
return array_key_exists($type, self::AVAILABLE_REGEX) ? self::AVAILABLE_REGEX[$type] : $type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters