Adds one entry point for both JSON and MP APIs

Removes endpoints folder and moves versions to the root
JSON API folder
This commit is contained in:
Vlad
2017-05-10 18:20:32 -04:00
parent 3c7ac5488a
commit 8b13889c7a
30 changed files with 49 additions and 48 deletions

18
lib/API/API.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace MailPoet\API;
if(!defined('ABSPATH')) exit;
class API {
static function JSON() {
return new \MailPoet\API\JSON\API();
}
static function MP($version) {
$api_class = sprintf('%s\MP\%s\API', __NAMESPACE__, $version);
if(class_exists($api_class)) {
return new $api_class();
}
throw new \Exception(__('Invalid API version.', 'mailpoet'));
}
}

View File

@ -20,12 +20,11 @@ class API {
'v1'
);
const CURRENT_VERSION = 'v1';
const ENDPOINTS_LOCATION = 'Endpoints';
function __construct() {
foreach($this->_available_api_versions as $available_api_version) {
$this->addEndpointNamespace(
sprintf('%s\%s\%s', __NAMESPACE__, self::ENDPOINTS_LOCATION, $available_api_version),
sprintf('%s\%s', __NAMESPACE__, $available_api_version),
$available_api_version
);
}

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\WP\Posts as WPPosts;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;
use MailPoet\Models\CustomField;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\Subscribers\ImportExport\Import\MailChimp;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;
use MailPoet\Mailer\MailerLog;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\Config\Activator;

View File

@ -1,5 +1,5 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;
use MailPoet\API\JSON\Access as APIAccess;

View File

@ -1,15 +0,0 @@
<?php
namespace MailPoet\MP\API;
if(!defined('ABSPATH')) exit;
class API {
static function __callStatic($version, $arguments) {
$api_class = sprintf('%s\%s\API', __NAMESPACE__, $version);
if(class_exists($api_class)) {
$api = new $api_class();
return $api;
}
throw new \Exception(__('Invalid API version.', 'mailpoet'));
}
}

View File

@ -172,8 +172,7 @@ class Initializer {
}
function setupJSONAPI() {
$api = new API\JSON\API();
$api->init();
API\API::JSON()->init();
}
function setupRouter() {

View File

@ -84,7 +84,7 @@ class APITest extends MailPoetTest {
function testItAcceptsAndProcessesAPIVersion() {
$namespace = array(
'name' => 'MailPoet\API\JSON\Endpoints\v2',
'name' => 'MailPoet\API\JSON\v2',
'version' => 'v2'
);
$this->api->addEndpointNamespace($namespace['name'], $namespace['version']);
@ -98,13 +98,13 @@ class APITest extends MailPoetTest {
expect($this->api->getRequestedAPIVersion())->equals('v2');
expect($this->api->getRequestedEndpointClass())->equals(
'MailPoet\API\JSON\Endpoints\v2\NamespacedEndpointStub'
'MailPoet\API\JSON\v2\NamespacedEndpointStub'
);
}
function testItCallsAddedEndpoints() {
$namespace = array(
'name' => 'MailPoet\API\JSON\Endpoints\v1',
'name' => 'MailPoet\API\JSON\v1',
'version' => 'v1'
);
$this->api->addEndpointNamespace($namespace['name'], $namespace['version']);
@ -123,7 +123,7 @@ class APITest extends MailPoetTest {
function testItCallsAddedEndpointsForSpecificAPIVersion() {
$namespace = array(
'name' => 'MailPoet\API\JSON\Endpoints\v2',
'name' => 'MailPoet\API\JSON\v2',
'version' => 'v2'
);
$this->api->addEndpointNamespace($namespace['name'], $namespace['version']);

View File

@ -1,6 +1,6 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v1;
namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Access as APIAccess;

View File

@ -1,6 +1,6 @@
<?php
namespace MailPoet\API\JSON\Endpoints\v2;
namespace MailPoet\API\JSON\v2;
use MailPoet\API\JSON\Access as APIAccess;
use MailPoet\API\JSON\Endpoint as APIEndpoint;

View File

@ -1,6 +1,6 @@
<?php
use MailPoet\API\JSON\Endpoints\v1\CustomFields;
use MailPoet\API\JSON\v1\CustomFields;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Models\CustomField;

View File

@ -1,5 +1,5 @@
<?php
use MailPoet\API\JSON\Endpoints\v1\Forms;
use MailPoet\API\JSON\v1\Forms;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Models\Form;
use MailPoet\Models\Segment;

View File

@ -1,6 +1,6 @@
<?php
use MailPoet\API\JSON\Endpoints\v1\Mailer;
use MailPoet\API\JSON\v1\Mailer;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Mailer\MailerLog;

View File

@ -1,6 +1,6 @@
<?php
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\API\JSON\Endpoints\v1\NewsletterTemplates;
use MailPoet\API\JSON\v1\NewsletterTemplates;
use MailPoet\Models\NewsletterTemplate;
class NewsletterTemplatesTest extends MailPoetTest {

View File

@ -3,7 +3,7 @@ use Carbon\Carbon;
use Codeception\Util\Fixtures;
use Codeception\Util\Stub;
use Helper\WordPressHooks as WPHooksHelper;
use MailPoet\API\JSON\Endpoints\v1\Newsletters;
use MailPoet\API\JSON\v1\Newsletters;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterOptionField;

View File

@ -1,5 +1,5 @@
<?php
use MailPoet\API\JSON\Endpoints\v1\Segments;
use MailPoet\API\JSON\v1\Segments;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Models\Segment;

View File

@ -1,7 +1,7 @@
<?php
use Codeception\Util\Stub;
use MailPoet\API\JSON\Endpoints\v1\Services;
use MailPoet\API\JSON\v1\Services;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Services\Bridge;

View File

@ -1,7 +1,7 @@
<?php
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\API\JSON\Error as APIError;
use MailPoet\API\JSON\Endpoints\v1\Settings;
use MailPoet\API\JSON\v1\Settings;
use MailPoet\Models\Setting;
class SettingsTest extends MailPoetTest {

View File

@ -1,7 +1,7 @@
<?php
use Helper\WordPressHooks as WPHooksHelper;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\API\JSON\Endpoints\v1\Setup;
use MailPoet\API\JSON\v1\Setup;
use MailPoet\Models\Setting;
class SetupTest extends MailPoetTest {

View File

@ -1,7 +1,7 @@
<?php
use Codeception\Util\Fixtures;
use MailPoet\API\JSON\Endpoints\v1\Subscribers;
use MailPoet\API\JSON\v1\Subscribers;
use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Models\Form;
use MailPoet\Models\Subscriber;