Skip to content

Commit

Permalink
Merge pull request #19 from joanrodas/dev
Browse files Browse the repository at this point in the history
Fix serialize + Duplicate RouteProcessor instances
  • Loading branch information
joanrodas authored May 22, 2023
2 parents 7c73faf + 10948e3 commit d6698af
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
5 changes: 4 additions & 1 deletion PluboRoutes/Endpoint/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ public function getPermissionCallback()
*/
public function __serialize()
{
return [$this->path, $this->method];
return [
'path' => $this->path,
'method' => $this->method
];
}

/**
Expand Down
8 changes: 5 additions & 3 deletions PluboRoutes/Route/RouteTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ public function getExtraVars()
*/
public function __serialize()
{

return [$this->path, $this->getExtraVars()];
return [
'path' => $this->path,
'extra_vars' => $this->getExtraVars()
];
}

/**
Expand All @@ -119,6 +121,6 @@ public function __serialize()
public function __unserialize($data)
{
$this->path = $data['path'];
$this->args = $data['args'];
$this->config['extra_vars'] = $data['extra_vars'];
}
}
36 changes: 27 additions & 9 deletions PluboRoutes/RoutesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class RoutesProcessor
*/
private $router;

/**
* The router instance.
*
* @var Router
*/
private static $instance = NULL;

/**
* Constructor.
*
Expand All @@ -44,23 +51,34 @@ class RoutesProcessor
public function __construct(Router $router)
{
$this->router = $router;
add_action('init', [$this, 'addRoutes']);
add_action('parse_request', [$this, 'matchRouteRequest']);
add_action('send_headers', [$this, 'basicAuth']);
add_action('rest_api_init', [$this, 'addEndpoints']);
add_action('template_redirect', [$this, 'doRouteActions']);
add_action('template_include', [$this, 'includeRouteTemplate']);
add_filter('body_class', [$this, 'addBodyClasses']);
add_filter('document_title_parts', [$this, 'modifyTitle']);
}

/**
* Clone not allowed.
*
*/
private function __clone()
{
}

/**
* Initialize processor with WordPress.
*
*/
public static function init()
{
$self = new self(new Router());
add_action('init', [$self, 'addRoutes']);
add_action('parse_request', [$self, 'matchRouteRequest']);
add_action('send_headers', [$self, 'basicAuth']);
add_action('rest_api_init', [$self, 'addEndpoints']);
add_action('template_redirect', [$self, 'doRouteActions']);
add_action('template_include', [$self, 'includeRouteTemplate']);
add_filter('body_class', [$self, 'addBodyClasses']);
add_filter('document_title_parts', [$self, 'modifyTitle']);
if (is_null(self::$instance)) {
self::$instance = new self(new Router());
}
return self::$instance;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
}
},
"require": {
"php": ">=7.0"
"php": ">=7.4"
}
}
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions plubo-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,20 @@
add_filter('plubo/routes', function ($routes) {
$routes[] = new PluboRoutes\Route\Route(
'clients/testing',
function () {
//Do some stuff...
function ($matches) {
return locate_template('content');
},
array(
// 'guest' => true,
// 'logged_in' => false,
'redirect' => 'https://sirvelia.com',
'permission_callback' => function($matches) {
return false;
},
'permission_callback' => [null, 'check_permission'],
// 'extra_vars' => [
// 'lss_page' => 'test-page'
// ],
// 'basic_auth' => [
// 'user' => 'testing'
// ],
'basic_auth' => [
'user' => 'testing'
],
)
);
$routes[] = new PluboRoutes\Route\RedirectRoute(
Expand Down Expand Up @@ -80,3 +77,8 @@ function ($request) {
);
return $endpoints;
});

function check_permission($matches)
{
return true;
}

0 comments on commit d6698af

Please sign in to comment.