もがき系プログラマの日常

もがき系エンジニアの勉強したこと、日常のこと、気になっている技術、備忘録などを紹介するブログです。

cakephp3のDebugkitが使えないと思ったら、たいしたことない理由だった

DebugKitが中々出ないという事象に悩まされていました。

config/bootstrapには

<?php

...

if (Configure::read('debug')) {
    Plugin::load('DebugKit', ['bootstrap' => true, 'routes' => true]);
}

って書いてるし、もちろんdebugはtrue。

それでも出ない。

sqlliteがないだとか、なんだかんだと探していても、結局わからずじまいだったので、コアを漁って調べたところ、しょうもない理由でした。

結果としては、debug_kit/config/bootstrap.phpの以下の行で引っかかって出てませんでした。

<?php
/**
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\Event\EventManager;
use Cake\Log\Log;
use Cake\ORM\Query;
use Cake\Routing\DispatcherFactory;
use DebugKit\DebugSql;
use DebugKit\Middleware\DebugKitMiddleware;
use DebugKit\Routing\Filter\DebugBarFilter;
use DebugKit\ToolbarService;

$service = new ToolbarService(EventManager::instance(), (array)Configure::read('DebugKit'));

if (!$service->isEnabled() || php_sapi_name() === 'cli' || php_sapi_name() === 'phpdbg') {
   // $servide->isEnabled() が falseとなって出なかった
    return;
}

ToolbarServiceのisEnabled()はなんだろうと思ってみるとこれ。

<?php

...

    /**
     * Check whether or not debug kit is enabled.
     *
     * @return bool
     */
    public function isEnabled()
    {
        $enabled = (bool)Configure::read('debug');

        if ($enabled && !$this->isSuspiciouslyProduction()) {
            return true;
        }
        $force = $this->getConfig('forceEnable');
        if (is_callable($force)) {
            return $force();
        }

        return $force;
    }

そして問題となっていたメソッドは isSuspiciouslyProduction() というやつ。

<?php

...

    /**
     * Returns true if this applications is being executed on a domain with a TLD
     * that is commonly associated with a production environment.
     *
     * @return bool
     */
    protected function isSuspiciouslyProduction()
    {
        $host = explode('.', parse_url('http://' . env('HTTP_HOST'), PHP_URL_HOST));
        $first = current($host);
        $isIP = is_numeric(implode('', $host));

        if (count($host) === 1) {
            return false;
        }

        if ($isIP && in_array($first, ['192', '10', '127'])) {
            // Accessing the app by private IP, this is safe
            return false;
        }

        $tld = end($host);
        $safeTLD = ["localhost", "dev", "invalid", "test", "example", "local"];

        return !in_array($tld, $safeTLD);
    }

見た感じ、今のURLが開発環境のものかを判定してる感じだと思います。

判定材料は、この、isSuspiciouslyProduction() が独断で決めていますが、まぁ大体こんな感じが開発サーバーだよねって感で決めてるみたいです(それとも規約があるのかな?)

現状だと新たにisSuspiciouslyProduction()が判断する開発用ドメインをとるしかないという感じです。

http://www.atmarkit.co.jp/fwin2k/win2ktips/801exampledom/exampledom.html

↑こんなのがありました。知らなかった。勉強になりました。

それに、https://book.cakephp.org/3.0/ja/debug-kit.htmlでも記載している通り、DebugKitはローカル開発環境でのみ動作するように作られているようです。




現状お仕事でお手伝いしている企業は、各個人のローカル上での開発ではなく、開発サーバーを立ててそこに各自vhostを切って作業しています。

その際のURLが、この isSuspiciouslyProduction() が判断する 開発サーバー とは違っていたためDebugkitが出なかったということのようです。

すごい親切な設計でいいとは思うんですが、個人的にConfigure::read('debug')の判定があるので、それで判断させてほしいです。。

それとも設定値だけ変えればなんとかなるよ!みたいな事ありますかね?

もしあったらぜひ教えてください。。

ではでは。

疲れた。