概要
Yiiのデバッグツールに現在のbranchを表示するようにしてみた。
デバッグツールのカスタマイズ
デバッグツールバーとデバッガの あなた自身のパネルを作る を元にカスタマイズしました。
ファイル作成
yiisoft/yii2-debug/src/panels
に GitPanel.php を作成します。
ベースは こちらの ViewsPanel.php。
現在のbranchを取得する処理
PHPで現在のgitブランチを取得する こちらの記事から処理を実装。
<?php
namespace yii\debug\panels;
use Yii;
use yii\debug\Panel;
class GitPanel extends Panel
{
private $currentBranch;
public $dotGitPath;
public function init()
{
parent::init();
$this->setGitPath();
$this->currentBranch = $this->getCurrentBranch();
}
/**
* git関連のファイルパス設定
*/
private function setGitPath()
{
Yii::setAlias('@gitHEAD', $this->dotGitPath . '/HEAD');
}
/**
* 現在のbranchを取得する
*
* @return string
*/
private function getCurrentBranch(): string
{
$gitPath = Yii::getAlias('@gitHEAD');
return trim(implode('/', array_slice(explode('/', file_get_contents($gitPath)), 2)), "\n");
}
}
Yiiのデバッグツールにブランチ名を表示させる
GitPanelクラスに下記の関数を追加する。
/**
* {@inheritdoc}
*
* @return string
*/
public function getSummary(): string
{
$url = $this->getUrl();
$html = '<div class="yii-debug-toolbar__block">';
$html .= '<a href=' . $url . '>';
$html .= '<span class="yii-debug-toolbar__label yii-debug-toolbar__label_info">';
$html .= $this->currentBranch;
$html .= '</span></a></div>';
return $html;
}
/**
* {@inheritdoc}
*/
public function save()
{
return $this->currentBranch;
}
getSummary()
がツールバーに表示されるhtmlを作成する処理。
save()
が実行されることで内容がデータファイルに保存される。
設定
デバッグツールの拡張機能を利用するために main-local.php
に以下の設定を記述。
'modules'=>[
'debug' => [
'class' => 'yii\debug\Module',
'panels' => [
'git' => [
'class' => 'yii\debug\panels\GitPanel',
'dotGitPath' => '.gitディレクトリのパスを記述 例) /var/www/html/.git',
],
],
'allowedIPs' => ['*']
],
]