Add php namespaces

This commit is contained in:
Timothy Warren 2015-06-26 16:39:10 -04:00
parent 24fb6b9815
commit 4785b82208
22 changed files with 85 additions and 37 deletions

View File

@ -2,6 +2,7 @@
/** /**
* Base API Model * Base API Model
*/ */
namespace AnimeClient;
use \GuzzleHttp\Client; use \GuzzleHttp\Client;
use \GuzzleHttp\Cookie\CookieJar; use \GuzzleHttp\Cookie\CookieJar;
@ -63,7 +64,7 @@ class BaseApiModel extends BaseModel {
{ {
$result = $this->client->post('https://hummingbird.me/api/v1/users/authenticate', [ $result = $this->client->post('https://hummingbird.me/api/v1/users/authenticate', [
'body' => [ 'body' => [
'username' => $this->config->hummingbird_username, 'username' => $username,
'password' => $password 'password' => $password
] ]
]); ]);

View File

@ -2,6 +2,7 @@
/** /**
* Base Controller * Base Controller
*/ */
namespace AnimeClient;
use Aura\Web\WebFactory; use Aura\Web\WebFactory;
@ -28,6 +29,18 @@ class BaseController {
*/ */
protected $response; protected $response;
/**
* The api model for the current controller
* @var object
*/
protected $model;
/**
* Common data to be sent to views
* @var array
*/
protected $base_data = [];
/** /**
* Constructor * Constructor
*/ */
@ -77,7 +90,6 @@ class BaseController {
if ( ! is_file($template_path)) if ( ! is_file($template_path))
{ {
throw new Exception("Invalid template : {$path}"); throw new Exception("Invalid template : {$path}");
die();
} }
ob_start(); ob_start();
@ -198,10 +210,14 @@ class BaseController {
*/ */
public function login_action() public function login_action()
{ {
if ($this->model->authenticate($this->config->hummingbird_username, $this->request->post->get('password'))) if (
$this->model->authenticate(
$this->config->hummingbird_username,
$this->request->post->get('password')
)
)
{ {
$this->response->redirect->afterPost(full_url('', $this->base_data['url_type'])); $this->response->redirect->afterPost(full_url('', $this->base_data['url_type']));
$this->output();
return; return;
} }
@ -227,7 +243,15 @@ class BaseController {
// cookies // cookies
foreach($this->response->cookies->get() as $name => $cookie) foreach($this->response->cookies->get() as $name => $cookie)
{ {
@setcookie($name, $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httponly']); @setcookie(
$name,
$cookie['value'],
$cookie['expire'],
$cookie['path'],
$cookie['domain'],
$cookie['secure'],
$cookie['httponly']
);
} }
// send the actual response // send the actual response

View File

@ -2,6 +2,7 @@
/** /**
* Base DB model * Base DB model
*/ */
namespace AnimeClient;
/** /**
* Base model for database interaction * Base model for database interaction

View File

@ -2,6 +2,7 @@
/** /**
* Base for base models * Base for base models
*/ */
namespace AnimeClient;
use abeautifulsite\SimpleImage; use abeautifulsite\SimpleImage;

View File

@ -1,5 +1,7 @@
<?php <?php
namespace AnimeClient;
/** /**
* Wrapper for configuration values * Wrapper for configuration values
*/ */
@ -22,22 +24,16 @@ class Config {
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
if (empty($config_files)) if (empty($config_files))
{ {
/* $config = */require_once _dir(CONF_DIR, 'config.php'); require_once _dir(CONF_DIR, 'config.php'); // $config
/* $base_config = */require_once _dir(CONF_DIR, 'base_config.php'); require_once _dir(CONF_DIR, 'base_config.php'); // $base_config
} }
// @codeCoverageIgnoreEnd else // @codeCoverageIgnoreEnd
else
{ {
$config = $config_files['config']; $config = $config_files['config'];
$base_config = $config_files['base_config']; $base_config = $config_files['base_config'];
} }
$this->config = $config; $this->config = array_merge($config, $base_config);
foreach($base_config as $key => $val)
{
$this->config[$key] = $val;
}
} }

View File

@ -3,6 +3,8 @@
* Routing logic * Routing logic
*/ */
namespace AnimeClient;
use Aura\Router\RouterFactory; use Aura\Router\RouterFactory;
/** /**
@ -120,8 +122,8 @@ class Router {
private function _setup_routes() private function _setup_routes()
{ {
$route_map = [ $route_map = [
'anime' => 'AnimeController', 'anime' => '\\AnimeClient\\AnimeController',
'manga' => 'MangaController', 'manga' => '\\AnimeClient\\MangaController',
]; ];
$route_type = "anime"; $route_type = "anime";

View File

@ -25,6 +25,9 @@ function _setup_autoloaders()
{ {
require _dir(ROOT_DIR, '/vendor/autoload.php'); require _dir(ROOT_DIR, '/vendor/autoload.php');
spl_autoload_register(function ($class) { spl_autoload_register(function ($class) {
$class_parts = explode('\\', $class);
$class = end($class_parts);
$dirs = ["base", "controllers", "models"]; $dirs = ["base", "controllers", "models"];
foreach($dirs as $dir) foreach($dirs as $dir)

View File

@ -3,6 +3,8 @@
* Anime Controller * Anime Controller
*/ */
namespace AnimeClient;
/** /**
* Controller for Anime-related pages * Controller for Anime-related pages
*/ */

View File

@ -2,6 +2,7 @@
/** /**
* Manga Controller * Manga Controller
*/ */
namespace AnimeClient;
/** /**
* Controller for manga list * Controller for manga list

View File

@ -3,6 +3,8 @@
* Anime Collection DB Model * Anime Collection DB Model
*/ */
namespace AnimeClient;
/** /**
* Model for getting anime collection data * Model for getting anime collection data
*/ */
@ -27,7 +29,7 @@ class AnimeCollectionModel extends BaseDBModel {
{ {
parent::__construct(); parent::__construct();
$this->db = Query($this->db_config['collection']); $this->db = \Query($this->db_config['collection']);
$this->anime_model = new AnimeModel(); $this->anime_model = new AnimeModel();
// Is database valid? If not, set a flag so the // Is database valid? If not, set a flag so the
@ -82,7 +84,7 @@ class AnimeCollectionModel extends BaseDBModel {
->order_by('title') ->order_by('title')
->get(); ->get();
return $query->fetchAll(PDO::FETCH_ASSOC); return $query->fetchAll(\PDO::FETCH_ASSOC);
} }
/** /**

View File

@ -3,6 +3,8 @@
* Anime API Model * Anime API Model
*/ */
namespace AnimeClient;
/** /**
* Model for handling requests dealing with the anime list * Model for handling requests dealing with the anime list
*/ */

View File

@ -2,6 +2,7 @@
/** /**
* Manga API Model * Manga API Model
*/ */
namespace AnimeClient;
/** /**
* Model for handling requests dealing with the manga list * Model for handling requests dealing with the manga list
@ -9,17 +10,11 @@
class MangaModel extends BaseApiModel { class MangaModel extends BaseApiModel {
/** /**
* @var string $base_url - The base url for api requests * The base url for api requests
* @var string
*/ */
protected $base_url = "https://hummingbird.me/"; protected $base_url = "https://hummingbird.me/";
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
}
/** /**
* Update the selected manga * Update the selected manga
@ -112,9 +107,9 @@ class MangaModel extends BaseApiModel {
$raw_data = $response->json(); $raw_data = $response->json();
// Attempt to create the cache dir if it doesn't exist // Attempt to create the cache dir if it doesn't exist
if ( ! is_dir($config->data_cache_path)) if ( ! is_dir($this->config->data_cache_path))
{ {
mkdir($config->data_cache_path); mkdir($this->config->data_cache_path);
} }
// Cache data in case of downtime // Cache data in case of downtime

View File

@ -5,10 +5,9 @@
<thead> <thead>
<tr> <tr>
<th>Title</th> <th>Title</th>
<th>Alternate Title</th>
<th>Rating</th> <th>Rating</th>
<th>Chapters</th> <th>Chapters</th>
<!-- <th>Volumes</th> --> <th>Volumes</th>
<th>Type</th> <th>Type</th>
</tr> </tr>
</thead> </thead>
@ -17,13 +16,13 @@
<tr id="manga-<?= $item['manga']['id'] ?>"> <tr id="manga-<?= $item['manga']['id'] ?>">
<td class="align_left"> <td class="align_left">
<a href="https://hummingbird.me/manga/<?= $item['manga']['id'] ?>"> <a href="https://hummingbird.me/manga/<?= $item['manga']['id'] ?>">
<?= $item['manga']['romaji_title'] ?> <?= $item['manga']['romaji_title'] ?>
</a> </a>
<?= (array_key_exists('english_title', $item['manga'])) ? " &middot; " . $item['manga']['english_title'] : "" ?>
</td> </td>
<td class="align_left"><?= (array_key_exists('english_title', $item['manga'])) ? $item['manga']['english_title'] : "" ?></td>
<td><?= ($item['rating'] > 0) ? (int)($item['rating'] * 2) : '-' ?> / 10</td> <td><?= ($item['rating'] > 0) ? (int)($item['rating'] * 2) : '-' ?> / 10</td>
<td><?= $item['chapters_read'] ?> / <?= ($item['manga']['chapter_count'] > 0) ? $item['manga']['chapter_count'] : "-" ?></td> <td><?= $item['chapters_read'] ?> / <?= ($item['manga']['chapter_count'] > 0) ? $item['manga']['chapter_count'] : "-" ?></td>
<!-- <td><?= $item['volumes_read'] ?> / <?= ($item['manga']['volume_count'] > 0) ? $item['manga']['volume_count'] : "-" ?></td> --> <td><?= $item['volumes_read'] ?> / <?= ($item['manga']['volume_count'] > 0) ? $item['manga']['volume_count'] : "-" ?></td>
<td><?= $item['manga']['manga_type'] ?></td> <td><?= $item['manga']['manga_type'] ?></td>
</tr> </tr>
<?php endforeach ?> <?php endforeach ?>

View File

@ -3,6 +3,8 @@
* Here begins everything! * Here begins everything!
*/ */
namespace AnimeClient;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// ! Start config // ! Start config
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -36,7 +38,7 @@ _setup_autoloaders();
$config = new Config(); $config = new Config();
require _dir(BASE_DIR, '/functions.php'); require _dir(BASE_DIR, '/functions.php');
session_start(); \session_start();
use \Whoops\Handler\PrettyPageHandler; use \Whoops\Handler\PrettyPageHandler;
use \Whoops\Handler\JsonResponseHandler; use \Whoops\Handler\JsonResponseHandler;

View File

@ -35,6 +35,10 @@ tbody > tr:nth-child(odd) {
align-items: flex-end; align-items: flex-end;
} }
.flex-justify-space-around {
jusify-content: space-around;
}
.flex { .flex {
display: -webkit-flex; display: -webkit-flex;
display: -ms-flexbox; display: -ms-flexbox;

View File

@ -23,6 +23,7 @@ tbody > tr:nth-child(odd) {
.flex-wrap {flex-wrap: wrap} .flex-wrap {flex-wrap: wrap}
.flex-no-wrap {flex-wrap: nowrap} .flex-no-wrap {flex-wrap: nowrap}
.flex-align-end {align-items: flex-end} .flex-align-end {align-items: flex-end}
.flex-justify-space-around {jusify-content: space-around}
.flex {display: flex} .flex {display: flex}
.small-font { .small-font {

View File

@ -1,5 +1,7 @@
<?php <?php
use \AnimeClient\BaseApiModel;
class MockBaseApiModel extends BaseApiModel { class MockBaseApiModel extends BaseApiModel {
public function __construct() public function __construct()
@ -20,8 +22,8 @@ class BaseApiModelTest extends AnimeClient_TestCase {
$baseApiModel = new MockBaseApiModel(); $baseApiModel = new MockBaseApiModel();
// Some basic type checks for class memebers // Some basic type checks for class memebers
$this->assertInstanceOf('BaseModel', $baseApiModel); $this->assertInstanceOf('\AnimeClient\BaseModel', $baseApiModel);
$this->assertInstanceOf('BaseApiModel', $baseApiModel); $this->assertInstanceOf('\AnimeClient\BaseApiModel', $baseApiModel);
$this->assertInstanceOf('\GuzzleHttp\Client', $baseApiModel->client); $this->assertInstanceOf('\GuzzleHttp\Client', $baseApiModel->client);
$this->assertInstanceOf('\GuzzleHttp\Cookie\CookieJar', $baseApiModel->cookieJar); $this->assertInstanceOf('\GuzzleHttp\Cookie\CookieJar', $baseApiModel->cookieJar);

View File

@ -1,5 +1,7 @@
<?php <?php
use \AnimeClient\BaseDBModel;
class BaseDBModelTest extends AnimeClient_TestCase { class BaseDBModelTest extends AnimeClient_TestCase {
public function testBaseDBModelSanity() public function testBaseDBModelSanity()

View File

@ -1,5 +1,7 @@
<?php <?php
use \AnimeClient\BaseModel;
class BaseModelTest extends AnimeClient_TestCase { class BaseModelTest extends AnimeClient_TestCase {
public function testBaseModelSanity() public function testBaseModelSanity()

View File

@ -1,5 +1,7 @@
<?php <?php
use \AnimeClient\Config;
class ConfigTest extends AnimeClient_TestCase { class ConfigTest extends AnimeClient_TestCase {
public function setUp() public function setUp()

View File

@ -1,5 +1,7 @@
<?php <?php
use \AnimeClient\Config;
class FunctionsTest extends AnimeClient_TestCase { class FunctionsTest extends AnimeClient_TestCase {
public function setUp() public function setUp()

View File

@ -3,6 +3,8 @@
* Global setup for unit tests * Global setup for unit tests
*/ */
use \AnimeClient\Config;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Mock the default error handler // Mock the default error handler
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------