このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
apricot:core:basic-class [2020/05/04 14:35] y2sunlight [Debugクラス] |
apricot:core:basic-class [2020/06/03 11:39] (現在) tanaka [ErrorBagクラス] |
||
---|---|---|---|
行 9: | 行 9: | ||
* Apricot コア | * Apricot コア | ||
* [[apricot: | * [[apricot: | ||
- | * [[apricot: | + | * [[apricot: |
* Apricot 各種基本コアクラス | * Apricot 各種基本コアクラス | ||
* [[apricot: | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
+ | * [[apricot: | ||
+ | * [[apricot: | ||
* [[apricot: | * [[apricot: | ||
- | * [[apricot:app: | + | * [[apricot: |
- | + | ||
- | \\ | + | |
次に、Applicationクラス以外の基本的なコアクラスを作ります。ここで作成する多くのクラスはシングルトンとして実装します。 | 次に、Applicationクラス以外の基本的なコアクラスを作ります。ここで作成する多くのクラスはシングルトンとして実装します。 | ||
行 280: | 行 280: | ||
* name --- ログの名前(既定値は 環境変数APP_NAMEの値) | * name --- ログの名前(既定値は 環境変数APP_NAMEの値) | ||
- | * path --- ログの出力パス(既定値は var/log/) | + | * path --- ログの出力パス(既定値は var/logs/) |
* level --- ログの出力レベル(既定値は ' | * level --- ログの出力レベル(既定値は ' | ||
* max_files --- ログファイルの最大保存数(0は無制限) | * max_files --- ログファイルの最大保存数(0は無制限) | ||
行 646: | 行 646: | ||
* renderer.initialize --- 初期化コードをレンダリングするか否か(既定値はtrue) | * renderer.initialize --- 初期化コードをレンダリングするか否か(既定値はtrue) | ||
* renderer.stacked_data --- スタックデータをレンダリングするか否か(既定値はtrue) | * renderer.stacked_data --- スタックデータをレンダリングするか否か(既定値はtrue) | ||
- | |||
- | \\ | ||
- | |||
- | ===== ORM ====== | ||
- | |||
- | ORマッパーには[[basic-library: | ||
- | |||
- | \\ | ||
- | |||
- | ==== 設定ファイル ==== | ||
- | |||
- | {{fa> | ||
- | <code php idiorm.setting.php> | ||
- | <?php | ||
- | return | ||
- | [ | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ], | ||
- | ]; | ||
- | </ | ||
- | |||
- | 設定はデータベース毎に行います。apricotではSQLiteを使用します。 | ||
- | |||
- | * SQLite.db_file --- SQLiteなどのファイル共有型DBの場合に設定 (既定値は var/ | ||
- | * SQLite.connection_string --- 接続文字列 | ||
- | * SQLite.caching --- キャッシングの有無 | ||
- | * SQLite.logging --- ロギングの有無 | ||
- | |||
- | 詳しくは以下を参照して下さい:\\ | ||
- | https:// | ||
- | |||
- | |||
- | \\ | ||
- | |||
- | ==== 初期設定ファイル ==== | ||
- | |||
- | Idiorm(ORMクラス)には以下の初期設定ファイルが存在します。 | ||
- | |||
- | {{fa> | ||
- | <code php idiorm.setup.php> | ||
- | <?php | ||
- | use Core\Foundation\Security\UserAuth; | ||
- | |||
- | // | ||
- | // ORM(idirom)の初期設定 | ||
- | // | ||
- | return function(): | ||
- | { | ||
- | // データベースファイルの準備 | ||
- | $db_file = config(' | ||
- | |||
- | if (!file_exists($db_path=dirname($db_file))) | ||
- | { | ||
- | mkdir($db_path, | ||
- | } | ||
- | |||
- | // データベース接続 | ||
- | ORM:: | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | { | ||
- | // SQL debug logging | ||
- | \Core\Log:: | ||
- | }, | ||
- | ]); | ||
- | |||
- | // | ||
- | // 初期データの作成 | ||
- | // | ||
- | // TODO: | ||
- | |||
- | // SQLログ開始 | ||
- | ORM:: | ||
- | return true; // Must return true on success | ||
- | }; | ||
- | </ | ||
- | |||
- | 初期設定ファイルでは以下の事をおこないます: | ||
- | |||
- | * データベースの保存フォルダが存在しない場合は作成します | ||
- | * データベースへの接続 | ||
- | * 初期データの作成 (後述します: | ||
- | * SQLログの開始 (logging設定がtrueの場合) | ||
\\ | \\ | ||
行 799: | 行 710: | ||
</ | </ | ||
- | Viewクラスはテンプレートファイルのパス、コンパイル後のHTMLファイルのパス及び実行モードをBladeOneのコンストラクタに渡しているだけです。それらの値は、設定ファイルか(bladeone.setting.php)から取得します。 | + | Viewクラスはテンプレートファイルのパス、コンパイル後のHTMLファイルのパス及び実行モードをBladeOneのコンストラクタに渡しているだけです。それらの値は、設定ファイル(bladeone.setting.php)から取得します。 |
\\ | \\ | ||
行 865: | 行 776: | ||
' | ' | ||
' | ' | ||
- | 'users' | + | 'menu1' |
' | ' | ||
' | ' | ||
行 1023: | 行 934: | ||
</ | </ | ||
- | > この関数名は | + | > この関数名は |
+ | |||
+ | \\ | ||
+ | |||
+ | ===== エラーバッグ ===== | ||
+ | |||
+ | エラーバッグ(ErrorBagクラス)は、入力エラーなどの業務的なエラー(例外ではないエラー)を管理する為のクラスです。 | ||
+ | |||
+ | ==== ErrorBagクラス ==== | ||
+ | |||
+ | エラーバッグには名前を付けることができます。エラーは連想配列で保存されバッグ内の各エラーにはキーが付いています。ErrorBagクラスには以下のメソッドがあります。 | ||
+ | |||
+ | ^メソッド^機能^ | ||
+ | |< | ||
+ | |count(string $name=null): | ||
+ | |has(string $key, string $name=self:: | ||
+ | |get(string $key, string $name=self:: | ||
+ | |all(string $name=null): | ||
+ | |put($errors)|エラー配列の設定| | ||
+ | |||
+ | > | ||
+ | |||
+ | {{fa> | ||
+ | <code php ErrorBag.php> | ||
+ | <?php | ||
+ | namespace Core\Foundation; | ||
+ | |||
+ | /** | ||
+ | * Error Bag Class | ||
+ | */ | ||
+ | class ErrorBag implements \IteratorAggregate | ||
+ | { | ||
+ | public const DEFAULT_NAME = ' | ||
+ | |||
+ | /** | ||
+ | * Bag name | ||
+ | * @var string | ||
+ | */ | ||
+ | private $name; | ||
+ | |||
+ | /** | ||
+ | * Errors | ||
+ | * @var array | ||
+ | */ | ||
+ | private $errors = []; | ||
+ | |||
+ | /** | ||
+ | * Create Error bag | ||
+ | * @param array $errors Associative array | ||
+ | * @param string $name Bag name | ||
+ | */ | ||
+ | public function __construct($errors=null, | ||
+ | { | ||
+ | $this-> | ||
+ | if (isset($errors)) | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Count errors | ||
+ | * @param string $name Bag name | ||
+ | * @return int | ||
+ | */ | ||
+ | public function count(string $name=null): | ||
+ | { | ||
+ | if (!isset($name) || ($this-> | ||
+ | { | ||
+ | return count($this-> | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | return 0; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Checks if a key is present | ||
+ | * @param string $key Error key | ||
+ | * @param string $name Bag name | ||
+ | * @return boolean | ||
+ | */ | ||
+ | public function has(string $key, string $name=self:: | ||
+ | { | ||
+ | if ($this-> | ||
+ | { | ||
+ | return array_key_exists($key, | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Get error a bag | ||
+ | * @param string $key Error key | ||
+ | * @param string $name Bag name | ||
+ | * @return mixed return null if a key is not present | ||
+ | */ | ||
+ | public function get(string $key, string $name=self:: | ||
+ | { | ||
+ | $result = null; | ||
+ | if ($this-> | ||
+ | { | ||
+ | if ($this-> | ||
+ | { | ||
+ | return $this-> | ||
+ | } | ||
+ | } | ||
+ | return $result; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Get all errors | ||
+ | * @param string $name Bag name | ||
+ | * @return array | ||
+ | */ | ||
+ | public function all(string $name=null): | ||
+ | { | ||
+ | if (!isset($name) || ($this-> | ||
+ | { | ||
+ | return $this-> | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | return []; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Put errors | ||
+ | * @param array $error Associative array | ||
+ | */ | ||
+ | public function put($errors) | ||
+ | { | ||
+ | $arr = is_array($errors) ? $errors : (array)$errors; | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * IteratorAggregate Interface | ||
+ | */ | ||
+ | public function getIterator() | ||
+ | { | ||
+ | return new \ArrayIterator($this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
\\ | \\ | ||