はじめに
DrupalのPHP構成の確認画面をご存知ですか?
こちらのパスから確認することができます。
/admin/reports/status/php
通常であればインスタンスやコンテナの設定でPHP構成の管理を巻き取ることが多いのかなと思います。
ただ、
「本番環境・テスト環境ではメモリの制限それぞれ設定したい」
「開発環境ではメモリをフルに使いたい」
ってこと結構ありますよね。
(環境変数を使えば簡単にできるよって意見もあるかと思いますが...)
今回は、アプリ側でのPHP構成の変更方法をご紹介します。
設定場所
設定場所はなんと!
Drupal標準で用意されています。
Drupal標準..?
軽くissueを調べてみましたが、恐らくsettings.php
or settings.local.php
に記載するのが標準のようです。
(もっといい方法あるよって方はコメントください。)
Drupalが用意しているdefault.settings.php
にこんな記載がありました。
サンプルですね。
/**
* PHP settings:
*
* To see what PHP settings are possible, including whether they can be set at
* runtime (by using ini_set()), read the PHP documentation:
* http://php.net/manual/ini.list.php
* See \Drupal\Core\DrupalKernel::bootEnvironment() for required runtime
* settings and the .htaccess file for non-runtime settings.
* Settings defined there should not be duplicated here so as to avoid conflict
* issues.
*/
/**
* If you encounter a situation where users post a large amount of text, and
* the result is stripped out upon viewing but can still be edited, Drupal's
* output filter may not have sufficient memory to process it. If you
* experience this issue, you may wish to uncomment the following two lines
* and increase the limits of these variables. For more information, see
* http://php.net/manual/pcre.configuration.php.
*/
# ini_set('pcre.backtrack_limit', 200000);
# ini_set('pcre.recursion_limit', 200000);
僕だったらっていうカスタマイズ設定
もちろん、プロジェクト・要件によって変わってくると思いますが、こんな感じのフォーマットはいかがでしょうか?
是非参考に。
/**
* PHP settings:
*
* To see what PHP settings are possible, including whether they can
* be set at runtime (ie., when ini_set() occurs), read the PHP
* documentation at http://www.php.net/manual/en/ini.php#ini.list
* and take a look at the .htaccess file to see which non-runtime
* settings are used there. Settings defined here should not be
* duplicated there so as to avoid conflict issues.
*/
switch (DRUPAL_ROOT) {
// Prod環境の場合
case ('/data/www/prod/web'):
ini_set('post_max_size', '256M');
ini_set('upload_max_filesize', '16M');
ini_set('memory', '256M');
break;
// Stage・Test環境の場合
case ('/data/www/stage/web'):
case ('/data/www/tst/web'):
ini_set('post_max_size', '256M');
ini_set('upload_max_filesize', '16M');
ini_set('memory', '128M');
break;
case ('/data/www/dev/web'):
ini_set('memory', '128M');
break;
// Landoの場合
case ('/app/web'):
ini_set('memory', -1);
break;
default:
ini_set('default_charset', 'UTF-8');
break;
}
まとめ と ちょっと解説
さすがにここまでやるのであれば、環境変数等でやったほうがいいのでは?という意見が出てくるかと思いますが...
あくまで手段と知っておいていいとは思います。
特に開発環境では ini_set('memory', -1);
という設定を記述することで、開発作業をスムーズに進めることができます。
(もちろん、ローカルで単体テストを行うときは設定を外す必要がありますが...)
PHPでの分岐を入れる事が可能ですので、
環境ごと...
IPアドレス...
地域...
settings.php
への記述で、シチュエーションに応じたアプリケーションの設定を動的な変更が可能です。
結構魅力的ですよね。