Last active
August 29, 2015 14:16
-
-
Save n1215/299c039c19efabba6ee9 to your computer and use it in GitHub Desktop.
【baserCMS 】サイトとbaserCMSの設定に責任を持つクラスの導入イメージ
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
<?php | |
class BaserCMS { | |
protected $settings; | |
protected $_server; | |
protected $_env; | |
/** | |
* ファクトリ用のstaticメソッドを定義 | |
* ConfigureやDB・環境変数等、外部への関与はここに集約 | |
*/ | |
public static function createFromConfigAndGlobals() { | |
$baserSettings = array(); | |
$baserSettings['BcEnv'] = Configure::read('BcEnv'); | |
$baserSettings['BcApp'] = Configure::read('BcApp'); | |
return new self($baserSettings, $_SERVER, $_ENV); | |
} | |
/** | |
* コンストラクタはarrayを引数に取る単純なものにし、 | |
* 設定や環境変数は外部から注入(コンストラクタインジェクション) | |
* → テスト時は単に配列を用意してインスタンスを初期化すればよい | |
* → 外部(特に本番環境)への依存がないのでテストしやすい! | |
* 本番利用時は上記のファクトリメソッドを利用することで使い勝手を保つ | |
*/ | |
public function __construct(array $settings, array $_server, array $_env) { | |
$this->settings = $settings; | |
$this->_server = $_server; | |
$this->_env = $_env; | |
} | |
public function getVersion() { | |
//hogehoge | |
} | |
} |
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
<?php | |
//////////////////////bootstrapで初期化する/////////////////////// | |
/** | |
* コンストラクタと別にDBの設定や環境変数を引数にインスタンスを初期化するファクトリメソッドを用意 | |
* DBやConfigure、環境変数との関与を分離しテスト可能性を保つ | |
*/ | |
$site = Site::createFromConfigAndGlobals(); | |
//ConfigureかClassRegistryに登録するなどしてシングルトン扱いする | |
//Constrollerにはこんsトラクタで注入しても良さそう | |
//Viewには変数としてセットする | |
ClassRegistry::addObject('baser.site', $site); | |
////////////////////////以下利用方法の例////////////////////////////// | |
$site = ClassRegistry::getObject('baser.site'); | |
//デプロイパターン | |
if(!defined(BC_DEPLOY_PATTERN)) { | |
define('BC_DEPLOY_PATTERN', $site->baser->getDeployPattern()); | |
} | |
/** | |
* baserUrl取得 | |
*/ | |
define('BC_BASE_URL', $site->baseUrl()); | |
//アップデートチェック | |
$site->baser->isUpdatable(); | |
//メンテナンスモード | |
if ($site->isMaintenanceMode()) { | |
//hogehoge | |
} | |
//バージョン取得 | |
$baserVersion = $site->baser->getVersion(); |
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
<?php | |
class Site { | |
public $baser; | |
protected $configs; | |
/** | |
* ファクトリ用のメソッドを定義 | |
*/ | |
public static function createFromConfigAndGlobals() { | |
$baser = BaserCMS::createFromConfigAndGlobals(); | |
//サイト設定読込 | |
ClassResigtry::init('SiteConfig'); | |
$SiteConfig = ClassRegistry::getObject('SiteConfig'); | |
$siteConfigs = $SiteConfig->findExpanded(); | |
return new self($baser, $siteConfigs); | |
} | |
/** | |
* 同じく設定や環境変数は外部から注入 | |
*/ | |
public function __construct(BaserCMS $baser, array $configs) { | |
$this->baser = $baser; | |
$this->configs = $configs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment