Ground Sunlight

Windowsで作る - PHPプログラミングの開発環境

ユーザ用ツール

サイト用ツール


apricot:core:basic-class

差分

このページの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:core:top|Apricot コア作成の準備]]     * [[apricot:core:top|Apricot コア作成の準備]]
-    * [[apricot:core:applocation-class|Apricot アプリケーションクラス]]+    * [[apricot:core:application-class|Apricot アプリケーションクラス]]
     * Apricot 各種基本コアクラス     * Apricot 各種基本コアクラス
     * [[apricot:core:request-class|Apricot リクエストクラス]]     * [[apricot:core:request-class|Apricot リクエストクラス]]
     * [[apricot:core:response-class|Apricot レスポンスクラス]]     * [[apricot:core:response-class|Apricot レスポンスクラス]]
 +    * [[apricot:core:base-controller|Apricot ベースコントローラ]]
 +    * [[apricot:core:completion|Apricot コアの完成]]
   * [[apricot:app:top|Apricot アプリ]]   * [[apricot:app:top|Apricot アプリ]]
-  * [[apricot:app:ext|Apricot 拡張]] +  * [[apricot:ext:middleware|Apricot 拡張]]
- +
-\\+
  
 次に、Applicationクラス以外の基本的なコアクラスを作ります。ここで作成する多くのクラスはシングルトンとして実装します。 次に、Applicationクラス以外の基本的なコアクラスを作ります。ここで作成する多くのクラスはシングルトンとして実装します。
行 280: 行 280:
  
   * name --- ログの名前(既定値は 環境変数APP_NAMEの値)   * name --- ログの名前(既定値は 環境変数APP_NAMEの値)
-  * path --- ログの出力パス(既定値は var/log/)+  * path --- ログの出力パス(既定値は var/logs/)
   * level --- ログの出力レベル(既定値は 'debug')   * level --- ログの出力レベル(既定値は 'debug')
   * 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:idiorm:1.5|Idiorm]]を使用します。Idiormは元々シングルトンとして実装してあるのでそのまま使えます。使い方やメソッドについてはIdiormの[[https://idiorm.readthedocs.io/en/latest/|マニュアル]]を参照して下さい。 
- 
-\\ 
- 
-==== 設定ファイル ==== 
- 
-{{fa>folder-open-o}} ** /apricot/config/setting ** 
-<code php idiorm.setting.php> 
-<?php 
-return 
-[ 
-    'sqlite' => [ 
-        'db_file' => var_dir('db/apricot.sqlite'), 
-        'connection_string' => 'sqlite:'.var_dir('db/apricot.sqlite'), 
-        'caching' => true, 
-        'logging' => true, 
-    ], 
-]; 
-</code> 
- 
-設定はデータベース毎に行います。apricotではSQLiteを使用します。 
- 
-  * SQLite.db_file --- SQLiteなどのファイル共有型DBの場合に設定 (既定値は var/db/apricot.sqlite') 
-  * SQLite.connection_string --- 接続文字列 
-  * SQLite.caching --- キャッシングの有無 
-  * SQLite.logging --- ロギングの有無 
- 
-詳しくは以下を参照して下さい:\\  
-https://idiorm.readthedocs.io/en/latest/configuration.html#id1 
- 
- 
-\\ 
- 
-==== 初期設定ファイル ==== 
- 
-Idiorm(ORMクラス)には以下の初期設定ファイルが存在します。 
- 
-{{fa>folder-open-o}} ** /apricot/config/setup ** 
-<code php idiorm.setup.php> 
-<?php 
-use Core\Foundation\Security\UserAuth; 
- 
-//------------------------------------------------------------------- 
-// ORM(idirom)の初期設定 
-//------------------------------------------------------------------- 
-return function():bool 
-{ 
-    // データベースファイルの準備 
-    $db_file = config('idiorm.sqlite.db_file'); 
- 
-    if (!file_exists($db_path=dirname($db_file))) 
-    { 
-        mkdir($db_path,null,true); 
-    } 
- 
-    // データベース接続 
-    ORM::configure([ 
-        'connection_string' => config('idiorm.sqlite.connection_string'), 
-        'caching' => config('idiorm.sqlite.caching',false), 
-        'logging' => false, 
-        'logger' => function($log_string, $query_time) 
-        { 
-            // SQL debug logging 
-            \Core\Log::info("SQL",[$log_string]); 
-        }, 
-    ]); 
- 
-    //------------------------------------------- 
-    // 初期データの作成 
-    //------------------------------------------- 
-    // TODO: 
- 
-    // SQLログ開始 
-    ORM::configure('logging' , config('idiorm.sqlite.logging',false)); 
-    return true; // Must return true on success 
-}; 
-</code> 
- 
-初期設定ファイルでは以下の事をおこないます: 
- 
-  * データベースの保存フォルダが存在しない場合は作成します 
-  * データベースへの接続 
-  * 初期データの作成 (後述します:今のところはTODO:です) 
-  * SQLログの開始 (logging設定がtrueの場合) 
  
 \\ \\
行 799: 行 710:
 </code> </code>
  
-Viewクラスはテンプレートファイルのパス、コンパイル後のHTMLファイルのパス及び実行モードをBladeOneのコンストラクタに渡しているだけです。それらの値は、設定ファイル(bladeone.setting.php)から取得します。+Viewクラスはテンプレートファイルのパス、コンパイル後のHTMLファイルのパス及び実行モードをBladeOneのコンストラクタに渡しているだけです。それらの値は、設定ファイル(bladeone.setting.php)から取得します。
    
 \\ \\
行 865: 行 776:
         'title'=>env('APP_NAME'),         'title'=>env('APP_NAME'),
         'menu'=>[         'menu'=>[
-            'users'=>'Users',+            'menu1'=>'Menu1',
             'menu2'=>'Menu2',             'menu2'=>'Menu2',
             'menu3'=>'Menu3',             'menu3'=>'Menu3',
行 1023: 行 934:
 </code> </code>
  
-> この関数名は **__** です。2つ並んだアンダースコアはPythonプログラマーの間では ''dunders'' (double underscoreの意) と呼ばれ特別なクラス内メンバに付加されますが、ここではそのような意味はなくトランスレータを表す関数名としてLaravelに準じました。+> この関数名は <nowiki>__</nowiki> です。2つ並んだアンダースコアはPythonプログラマーの間では ''dunders'' (double underscoreの意) と呼ばれ特別なクラス内メンバに付加されますが、ここではそのような意味はなくトランスレータを表す関数名としてLaravelに準じました。 
 + 
 +\\ 
 + 
 +===== エラーバッグ ===== 
 + 
 +エラーバッグ(ErrorBagクラス)は、入力エラーなどの業務的なエラー(例外ではないエラー)を管理する為のクラスです。 
 + 
 +==== ErrorBagクラス ==== 
 + 
 +エラーバッグには名前を付けることができます。エラーは連想配列で保存されバッグ内の各エラーにはキーが付いています。ErrorBagクラスには以下のメソッドがあります。  
 + 
 +^メソッド^機能^ 
 +|<nowiki>__</nowiki>construct($errors=null, string $name=self::DEFAULT_NAME)|エラーバッグの生成| 
 +|count(string $name=null):int|エラー数の取得| 
 +|has(string $key, string $name=self::DEFAULT_NAME):bool|キーによるエラーの存在確認| 
 +|get(string $key, string $name=self::DEFAULT_NAME)|キーによるエラーの取得| 
 +|all(string $name=null):array|全てのエラーの取得| 
 +|put($errors)|エラー配列の設定| 
 + 
 +>エラーバッグは[[https://www.php.net/manual/ja/class.iteratoraggregate.php|IteratorAggregateインターフェース]]を実装してるのでforeach()などのIteratorを使用した構文が使用できます。但し、Countable インターフェイス は実装していないので、count関数ではなくErrorBag@countメソッドを使用して下さい。 
 + 
 +{{fa>folder-open-o}} ** /apricot/core/Foundation ** 
 +<code php ErrorBag.php> 
 +<?php 
 +namespace Core\Foundation; 
 + 
 +/** 
 + * Error Bag Class 
 + */ 
 +class ErrorBag implements \IteratorAggregate 
 +
 +    public const DEFAULT_NAME = 'error'; 
 + 
 +    /** 
 +     * 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, string $name=self::DEFAULT_NAME) 
 +    { 
 +        $this->name = $name; 
 +        if (isset($errors)) 
 +        { 
 +            $this->put($errors); 
 +        } 
 +    } 
 + 
 +    /** 
 +     * Count errors 
 +     * @param string $name Bag name 
 +     * @return int 
 +     */ 
 +    public function count(string $name=null):int 
 +    { 
 +        if (!isset($name) || ($this->name==$name)) 
 +        { 
 +            return count($this->errors); 
 +        } 
 +        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::DEFAULT_NAME):bool 
 +    { 
 +        if ($this->name==$name) 
 +        { 
 +            return array_key_exists($key, $this->errors); 
 +        } 
 +        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::DEFAULT_NAME) 
 +    { 
 +        $result = null; 
 +        if ($this->name==$name) 
 +        { 
 +            if ($this->has($key, $name)) 
 +            { 
 +                return $this->errors[$key]; 
 +            } 
 +        } 
 +        return $result; 
 +    } 
 + 
 +    /** 
 +     * Get all errors 
 +     * @param string $name Bag name 
 +     * @return array 
 +     */ 
 +    public function all(string $name=null):array 
 +    { 
 +        if (!isset($name) || ($this->name==$name)) 
 +        { 
 +            return $this->errors; 
 +        } 
 +        else 
 +        { 
 +            return []; 
 +        } 
 +    } 
 + 
 +    /** 
 +     * Put errors 
 +     * @param array $error Associative array 
 +     */ 
 +    public function put($errors) 
 +    { 
 +        $arr = is_array($errors) ? $errors : (array)$errors; 
 +        $this->errors = $arr; 
 +    } 
 + 
 +    /** 
 +     * IteratorAggregate Interface 
 +     */ 
 +    public function getIterator() 
 +    { 
 +        return new \ArrayIterator($this->errors); 
 +    } 
 +
 +</code>
  
 \\ \\
  
apricot/core/basic-class.1588570547.txt.gz · 最終更新: 2020/05/04 14:35 by y2sunlight