Skip to content

Commit

Permalink
Namespacify
Browse files Browse the repository at this point in the history
  • Loading branch information
danwdart committed Jan 18, 2018
1 parent df647e6 commit d0f0c48
Show file tree
Hide file tree
Showing 20 changed files with 119 additions and 73 deletions.
12 changes: 8 additions & 4 deletions src/php/Chaplin/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@
* @version GIT: $Id$
* @link https://github.com/danwdart/projectchaplin
**/
class Chaplin_Auth extends Zend_Auth
namespace Chaplin;

use Zend_Auth as ZendAuth;

class Auth extends ZendAuth
{
public static function getInstance()
{
{
if (null === self::$_instance) {
self::$_instance = new self();
}
}

return self::$_instance;
}

public static function inject(Zend_Auth $auth)
public static function inject(ZendAuth $auth)
{
self::$_instance = $auth;
}
Expand Down
22 changes: 15 additions & 7 deletions src/php/Chaplin/Auth/Adapter/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@
* @version GIT: $Id$
* @link https://github.com/danwdart/projectchaplin
**/
class Chaplin_Auth_Adapter_Database implements Zend_Auth_Adapter_Interface
namespace Chaplin\Auth\Adapter;

use Chaplin\Auth\Identity;
use Chaplin_Dao_Exception_User_NotFound as ExceptionUserNotFound;
use Chaplin_Gateway as Gateway;
use Zend_Auth_Adapter_Interface as AdapterInterface;
use Zend_Auth_Result as Result;

class Database implements AdapterInterface
{
private $_strUsername;
private $_strPassword;
Expand All @@ -36,19 +44,19 @@ public function __construct($strUsername, $strPassword)
public function authenticate()
{
try {
$modelUser = Chaplin_Gateway::getInstance()
$modelUser = Gateway::getInstance()
->getUser()
->getByUsernameAndPassword(
$this->_strUsername,
$this->_strPassword
);

$identity = new Chaplin_Auth_Identity($modelUser);
$identity = new Identity($modelUser);

return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
} catch(Chaplin_Dao_Exception_User_NotFound $e) {
return new Zend_Auth_Result(
Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
return new Result(Result::SUCCESS, $identity);
} catch(ExceptionUserNotFound $e) {
return new Result(
Result::FAILURE_CREDENTIAL_INVALID,
null,
array($e->getMessage())
);
Expand Down
8 changes: 6 additions & 2 deletions src/php/Chaplin/Auth/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
* @version GIT: $Id$
* @link https://github.com/danwdart/projectchaplin
**/
class Chaplin_Auth_Identity
namespace Chaplin\Auth;

use Chaplin_Model_User as ModelUser;

class Identity
{
private $_modelUser;

public function __construct(Chaplin_Model_User $modelUser)
public function __construct(ModelUser $modelUser)
{
$this->_modelUser = $modelUser;
}
Expand Down
4 changes: 3 additions & 1 deletion src/php/Chaplin/Cache/Backend/Exception/PhpRedisMissing.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
* @version GIT: $Id$
* @link https://github.com/danwdart/projectchaplin
**/
class Chaplin_Cache_Backend_Exception_PhpRedisMissing extends Exception
namespace Chaplin\Cache\Backend\Exception;

class PhpRedisMissing extends \Exception
{
const MESSAGE = 'PhpRedis was not included in the options array of the backend';

Expand Down
47 changes: 27 additions & 20 deletions src/php/Chaplin/Cache/Backend/PhpRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@

/**
* Redis adapter for Zend_Cache
*
*
* @author Soin Stoiana
* @author Colin Mollenhour
* @version 0.0.1
**/
class Chaplin_Cache_Backend_PhpRedis extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
namespace Chaplin\Cache\Backend;

use Redis;
use Zend_Cache as Cache;
use Zend_Cache_Backend as Backend;
use Zend_Cache_Backend_ExtendedInterface as BackendInterface;
use Zend_Config as Config;

class PhpRedis extends Backend implements BackendInterface
{

const SET_IDS = 'zc:ids';
Expand Down Expand Up @@ -46,34 +54,33 @@ class Chaplin_Cache_Backend_PhpRedis extends Zend_Cache_Backend implements Zend_
* Contruct Zend_Cache Redis backend
*
* @param array $options
* @return \Chaplin_Cache_Backend_PhpRedis
**/
public function __construct($options = array())
{
if ($options instanceof Zend_Config) {
if ($options instanceof Config) {
$options = $options->toArray();
}

if (isset($options['phpredis'])) {
$this->_redis = $options['phpredis'];
} else {
if (empty($options['server']) ) {
Zend_Cache::throwException('Redis \'server\' not specified.');
Cache::throwException('Redis \'server\' not specified.');
}

if (empty($options['port']) ) {
Zend_Cache::throwException('Redis \'port\' not specified.');
Cache::throwException('Redis \'port\' not specified.');
}

$this->_redis = new Redis;
if (! $this->_redis->connect($options['server'], $options['port']) ) {
Zend_Cache::throwException("Could not connect to Redis server {$options['server']}:{$options['port']}");
Cache::throwException("Could not connect to Redis server {$options['server']}:{$options['port']}");
}
}

if (! empty($options['database'])) {
$this->_redis->select((int) $options['database']) or
Zend_Cache::throwException('The redis database could not be selected.');
$this->_redis->select((int) $options['database']) or
Cache::throwException('The redis database could not be selected.');
}

if (isset($options['notMatchingTags']) ) {
Expand Down Expand Up @@ -373,17 +380,17 @@ protected function _collectGarbage()
* @throws Zend_Cache_Exception
* @return boolean True if no problem
**/
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
public function clean($mode = Cache::CLEANING_MODE_ALL, $tags = array())
{
if ($tags && ! is_array($tags)) {
$tags = array($tags);
}

if ($mode == Zend_Cache::CLEANING_MODE_ALL) {
if ($mode == Cache::CLEANING_MODE_ALL) {
return ($this->_redis->flushDb() == 'OK');
}

if ($mode == Zend_Cache::CLEANING_MODE_OLD) {
if ($mode == Cache::CLEANING_MODE_OLD) {
$this->_collectGarbage();
return true;
}
Expand All @@ -396,23 +403,23 @@ public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())

switch ($mode)
{
case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
case Cache::CLEANING_MODE_MATCHING_TAG:

$this->_removeByMatchingTags($tags);
break;

case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
case Cache::CLEANING_MODE_NOT_MATCHING_TAG:

$this->_removeByNotMatchingTags($tags);
break;

case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
case Cache::CLEANING_MODE_MATCHING_ANY_TAG:

$this->_removeByMatchingAnyTags($tags);
break;

default:
Zend_Cache::throwException('Invalid mode for clean() method: '.$mode);
Cache::throwException('Invalid mode for clean() method: '.$mode);
}
return (bool) $result;
}
Expand All @@ -439,7 +446,7 @@ public function setDirectives($directives)
parent::setDirectives($directives);
$lifetime = $this->getLifetime(false);
if ($lifetime > 2592000) {
Zend_Cache::throwException('Redis backend has a limit of 30 days (2592000 seconds) for the lifetime');
Cache::throwException('Redis backend has a limit of 30 days (2592000 seconds) for the lifetime');
}
}

Expand Down Expand Up @@ -496,7 +503,7 @@ public function getIdsMatchingTags($tags = array())
public function getIdsNotMatchingTags($tags = array())
{
if (! $this->_notMatchingTags) {
Zend_Cache::throwException("notMatchingTags is currently disabled.");
Cache::throwException("notMatchingTags is currently disabled.");
}
return (array) $this->_redisVariadic('sDiff', self::SET_IDS, $this->_preprocessTagIds($tags));
}
Expand Down Expand Up @@ -547,7 +554,7 @@ public function getMetadatas($id)

return array(
'expire' => time() + $ttl,
'tags' => $tags,
'tags' => $tags,
'mtime' => $mtime,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
* @version GIT: $Id$
* @link https://github.com/danwdart/projectchaplin
**/
abstract class Chaplin_Cache_Abstract
namespace Chaplin\Cache;

use Zend_Cache_Core as ZendCache;

abstract class CacheAbstract
{
private $_zendCache;
public function setCache(Zend_Cache_Core $zendCache = null)

public function setCache(ZendCache $zendCache = null)
{
$this->_zendCache = $zendCache;
}
Expand Down
33 changes: 20 additions & 13 deletions src/php/Chaplin/Cache/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,26 @@
* @version GIT: $Id$
* @link https://github.com/danwdart/projectchaplin
**/
class Chaplin_Cache_Http_Client
extends Chaplin_Cache_Abstract
implements Chaplin_Http_Interface

namespace Chaplin\Cache\Http;

use Chaplin\Cache\CacheAbstract;
use Chaplin_Http_Interface as HttpInterface;
use Chaplin_Log as Log;
use Zend_Cache_Core as Cache;
use Zend_Log as ZendLog;

class Client extends CacheAbstract implements HttpInterface
{
private $_objHttpClient;

public function __construct(Chaplin_Http_Interface $objHttpClient, Zend_Cache_Core $cacheHttpClient = null)
public function __construct(HttpInterface $objHttpClient, Cache $cacheHttpClient = null)
{
$this->_objHttpClient = $objHttpClient;
$this->setCache($cacheHttpClient);
}
public function getPageBody($strURL, $intLogPriority = Zend_Log::ERR)

public function getPageBody($strURL, $intLogPriority = ZendLog::ERR)
{
$cacheKey = $this->_getCacheKey(__METHOD__, $strURL);
if (false === ($response = $this->_cacheLoadKey($cacheKey))) {
Expand All @@ -44,7 +51,7 @@ public function getPageBody($strURL, $intLogPriority = Zend_Log::ERR)
return $response;
}

public function getObject($strURL, $intLogPriority = Zend_Log::ERR)
public function getObject($strURL, $intLogPriority = ZendLog::ERR)
{
$cacheKey = $this->_getCacheKey(__METHOD__, $strURL);
if (false === ($response = $this->_cacheLoadKey($cacheKey))) {
Expand All @@ -53,7 +60,7 @@ public function getObject($strURL, $intLogPriority = Zend_Log::ERR)
}
return $response;
}

public function scrapeXPath($strURL, $strXPath)
{
$cacheKey = $this->_getCacheKey(__METHOD__, $strURL . 'X' . $strXPath);
Expand All @@ -64,24 +71,24 @@ public function scrapeXPath($strURL, $strXPath)
return $response;
}

public function getHttpResponse($strURL, $intLogPriority = Zend_Log::ERR, $bCache = true)
public function getHttpResponse($strURL, $intLogPriority = ZendLog::ERR, $bCache = true)
{
if(!$bCache) {
return $this->_objHttpClient->getHttpResponse($strURL, $intLogPriority);
}
Chaplin_Log::getInstance()->log('Cache: trying to load ('.$strURL.')', $intLogPriority);

Log::getInstance()->log('Cache: trying to load ('.$strURL.')', $intLogPriority);
$cacheKey = $this->_getCacheKey(__METHOD__, $strURL);
if (false === ($response = $this->_cacheLoadKey($cacheKey))) {
$response = $this->_objHttpClient->getHttpResponse($strURL, $intLogPriority);
if(200 == $response->getStatus()) {
$this->_cacheSaveKey($cacheKey, $response);
}
}
Chaplin_Log::getInstance()->log('Cache: retrieved ('.$response->getBody().')', $intLogPriority);
Log::getInstance()->log('Cache: retrieved ('.$response->getBody().')', $intLogPriority);
return $response;
}

public function parseRawXPath($strData, $strXPath)
{
return $this->_objHttpClient->parseRawXPath($strData, $strXPath);
Expand Down
4 changes: 3 additions & 1 deletion src/php/Chaplin/Controller/Plugin/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* @version GIT: $Id$
* @link https://github.com/danwdart/projectchaplin
**/
use Chaplin\Auth;

class Chaplin_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
private $_acl;
Expand Down Expand Up @@ -57,7 +59,7 @@ public function preDispatch(Zend_Controller_Request_Abstract $request)
$strController = $request->getControllerName();
$strAction = $request->getActionName();

$auth = Chaplin_Auth::getInstance();
$auth = Auth::getInstance();
$resource = $strModule.'/'.$strController;
$role = $auth->hasIdentity() ?
$auth->getIdentity()->getUser()->getUserType()->getUserType():
Expand Down
7 changes: 4 additions & 3 deletions src/php/Chaplin/Model/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @version GIT: $Id$
* @link https://github.com/danwdart/projectchaplin
**/
use Chaplin\Auth;
use Misd\Linkify\Linkify;

class Chaplin_Model_Video extends Chaplin_Model_Field_Hash
Expand Down Expand Up @@ -277,14 +278,14 @@ public function getYourVote()

public function isMine()
{
if (!Chaplin_Auth::getInstance()->hasIdentity()) {
if (!Auth::getInstance()->hasIdentity()) {
return false;
}
if (Chaplin_Auth::getInstance()->getIdentity()->getUser()->isGod()) {
if (Auth::getInstance()->getIdentity()->getUser()->isGod()) {
// God users own everything, mwuhahaha
return true;
}
return Chaplin_Auth::getInstance()
return Auth::getInstance()
->getIdentity()
->getUser()
->getUsername() ==
Expand Down
Loading

0 comments on commit d0f0c48

Please sign in to comment.