0
0

[オレオレ10] ヘルパー関数

Posted at

解説

この辺で開発効率を上げるために便利なヘルパー関数を作っていきます。
まずは、そのヘルパー関数が使える仕組みを実装していきます。
※ヘルパー関数はグローバル関数として扱われます。関数名は将来的にも重複しない、重複の可能性が低い名前をつけます。

今回はディレクトリのパスを返すヘルパー関数とデバッグ用の関数をつくります。

目次

  • Helper.php作成
  • path.php作成
  • app.php作成
  • index.php修正
  • View.php修正

Helper.php作成

$ cd /opt/project/stampede/
$ touch supplier/stampede/Helper.php
Helper.php
<?php

namespace Stampede;

class Helper
{
    /**
     * 
     * @var \Stampede\Helper
     */
    private static $instance = null;

    /**
     * 
     * @var string 
     */
    public $root = '';

    /**
     * 
     * 
     */
    private function __construct()
    {
    }

    /**
     * 
     * 
     */
    public static function getInstance()
    {
        if (is_null(self::$instance) === true) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    /**
     * 
     * 
     */
    public function initPath(string $root)
    {
        $this->root = $root;
    }

    /**
     * 
     * 
     */
    public function read(string $dir)
    {
        foreach (glob($dir . '*.php') as $file) {
            require_once($file);
        }
    }
}

path.php作成

$ mkdir helpers
$ touch helpers/path.php
path.php
<?php

use Stampede\Helper;

/**
 * 
 * 
 */
function base_path(string $path = '')
{
    $helper = Helper::getInstance();

    if (empty($path) === true) {
        return $helper->root;
    } else {
        return $helper->root . $path;
    }
}

/**
 * 
 * 
 */
function app_path(string $path = '')
{
    $helper = Helper::getInstance();

    if (empty($path) === true) {
        return $helper->root . 'app/';
    } else {
        return $helper->root . 'app/' . $path;
    }
}

/**
 * 
 * 
 */
function view_path(string $path = '')
{
    $helper = Helper::getInstance();

    if (empty($path) === true) {
        return $helper->root . 'views/';
    } else {
        return $helper->root . 'views/' . $path;
    }
}

app.php作成

$ cd /opt/project/stampede/
$ touch helpers/app.php
app.php
<?php

/**
 * 
 * 
 */
function dump($value)
{
    if (headers_sent() === false) {
        header('Content-Type: text/plain; charset=UTF8');
    }
    echo print_r($value, true) . "\n";
}

/**
 * 
 * 
 */
function dd($value)
{
    if (headers_sent() === false) {
        header('Content-Type: text/plain; charset=UTF8');
    }
    echo print_r($value, true) . "\n";
    
    exit;
}

index.php作成

index.php
<?php

ini_set('display_errors', "On");

require_once('../autoLoader.php');

use Stampede\Helper;    // 追加
use Stampede\App\Http;
use Stampede\App\Http\View;

//
$root = '/opt/project/stampede/';

// オートローダー起動
$autoloader = new autoLoader($root);
$autoloader->register();

// ヘルパー関数読み込み
$helper = Helper::getInstance();    // 追加
$helper->initPath($root);           // 追加
$helper->read($root . 'helpers/');  // 追加

dump(base_path());    // 追加
dd(view_path());      // 追加

// ビュークラスのインスタンス取得
$view = View::getInstance();

//
$app = Http::getInstance();
$app->bootstrap($view);

exit;

View.php修正

ヘルパー関数でviewsディレクトリパスを取得できるようになったので、べた書き部分を関数に置き換えます。

View.php
    // 略
    /**
     * 
     * 
     */
    private $templateDir = '';    // 変更

    /**
     * コンストラクタ
     * 
     * @return void
     */
    private function __construct()
    {
        $this->templateDir = view_path();    // 追加
    }

前の記事
[オレオレ09] MVCのC コントローラー
次の記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0