{"id":8619,"date":"2016-10-25T07:58:56","date_gmt":"2016-10-25T06:58:56","guid":{"rendered":"https:\/\/wp.okra.host\/article\/working-with-laravel-configcache\/"},"modified":"2021-03-07T14:37:15","modified_gmt":"2021-03-07T13:37:15","slug":"working-with-laravel-configcache","status":"publish","type":"ht_kb","link":"https:\/\/kb.okra.host\/de\/article\/working-with-laravel-configcache\/","title":{"rendered":"Arbeiten mit Laravel config:cache"},"content":{"rendered":"<p>Laravel provides a static cache utility\u00a0<a href=\"https:\/\/laravel.com\/docs\/5.3\/configuration\">via Artisan<\/a> to collapse configuration under <code>config\/<\/code> into a single file to boost performance. Configuration may be cached using:<\/p>\n<p><code>php artisan config:cache<\/code><\/p>\n<p>When run from <a href=\"https:\/\/kb.okra.host\/terminal\/is-terminal-access-available\/\">terminal<\/a>, the paths provided may be incorrectly referenced when the application is accessed from the web resulting in application errors.<\/p>\n<h2 id=\"solution\" >Solution<\/h2>\n<p>Overwrite\u00a0<code>bootstrap\/app.php<\/code> to use a custom loader. We&#8217;ll override a couple methods to bypass cache in CLI SAPI mode (<em>php artisan xx:yy<\/em>) and opportunistically regenerate the cache whenever remote changes are pushed upstream\u00a0via git.<\/p>\n<h3 id=\"replacing-bootstrapper\" >Replacing bootstrapper<\/h3>\n<p>Create a new file called <strong>ApplicationWrapper.php<\/strong> in <strong>app\/<\/strong> to provide additional functionality to IlluminateFoundationApplication:<\/p>\n<pre>&lt;?php\r\nnamespace App;\r\n\r\nuse IlluminateFoundationApplication;\r\n\r\nclass ApplicationWrapper extends Application {\r\n\r\n    public function __construct($basePath)\r\n    {\r\n        if (!isset($_SERVER['SITE_ROOT'])) {\r\n            $_SERVER['SITE_ROOT'] = '';\r\n        }\r\n        parent::__construct($basePath);\r\n    }\r\n\r\n   \/**\r\n    * Fake configuration cache response for CLI\r\n    * as paths will always be different\r\n    * \r\n    * @return bool\r\n    *\/\r\n    public function configurationIsCached() {\r\n        if ($this-&gt;runningInConsole()) {\r\n            return false;\r\n        }\r\n        return parent::configurationIsCached();\r\n    }\r\n\r\n   \/**\r\n    * Emulate IlluminateFoundationConsoleConfigCachefire()\r\n    *\r\n    * @return bool\r\n    *\/\r\n    public function doCache() {\r\n       if (!$this-&gt;runningInConsole()) {\r\n           $config = $this-&gt;app['config']-&gt;all();\r\n           $this-&gt;files-&gt;put(\r\n               $this-&gt;getCachedConfigPath(), '&lt;?php return '.var_export($config, true).';'.PHP_EOL\r\n           );\r\n       }\r\n        return true;\r\n    }\r\n\r\n    \/*\r\n     * Override boot to register production config cache\r\n     * @return boolean\r\n     *\/\r\n    public function boot()\r\n    {\r\n        parent::boot();\r\n        if ($this-&gt;environment() !== \"production\") {\r\n           return;\r\n        }\r\n        if (!$this-&gt;runningInConsole()) {\r\n            $app = $this-&gt;app;\r\n            $this-&gt;terminating(function() use ($app) {\r\n                $app-&gt;configurationIsCached() || $app-&gt;doCache();\r\n            });\r\n        } else {\r\n           $path = parent::getCachedConfigPath();\r\n           $this-&gt;terminating(function() use ($path) {\r\n              file_exists($path) &amp;&amp; unlink($path);\r\n           });\r\n        }\r\n\r\n    }\r\n}<\/pre>\n<p>A couple notes:<\/p>\n<ol>\n<li>Cache is done post-boot so that configuration is properly loaded. Caching is done once at the end of the request to reduce overhead.<\/li>\n<li><em>APP_ENV<\/em> mode is assumed to be &#8220;production&#8221;; this is controlled by <a href=\"https:\/\/laravel.com\/docs\/configuration#environment-configuration\">.env<\/a>.<\/li>\n<li><strong>artisan config:cache<\/strong> will create, then immediately unlink config.php in favor of\u00a0generation by the web server<\/li>\n<\/ol>\n<p>Next, adjust <strong>bootstrap\/app.php<\/strong> to instantiate a new <em>ApplicationWrapper<\/em> instance rather than <em>IlluminateFoundationApplication<\/em>:<\/p>\n<p>Change:<\/p>\n<pre>$app = new IlluminateFoundationApplication(\r\n    realpath(__DIR__.'\/..\/')\r\n);<\/pre>\n<p>to<\/p>\n<pre>$app = new AppApplicationWrapper(\r\n    realpath(__DIR__.'\/..\/')\r\n);<\/pre>\n<p>And that&#8217;s it! Run <strong>php artisan config:clear<\/strong>\u00a0to have it automatically regenerate during the next page request.<\/p>\n<h3 id=\"regenerating-cache-with-git\" >Regenerating Cache with git<\/h3>\n<p>Next, create a <em>post-receive<\/em> hook in your git repository on the server. Create <strong>hooks\/post-receive<\/strong> if it does not already exist with the following content:<\/p>\n<pre data-language=\"shell\"><code>#!\/bin\/sh\r\nLIVE=\"\/var\/www\/\"\r\n\r\nread oldrev newrev refname\r\nif [[ $refname = \"refs\/heads\/master\" ]]; then \r\n\u00a0 \u00a0 echo \"===== DEPLOYING TO LIVE SITE =====\" \r\n\u00a0 \u00a0 unset GIT_DIR\r\n\u00a0 \u00a0 cd $LIVE\r\n\u00a0 \u00a0 .\/build.sh\r\n\u00a0 \u00a0 echo \"===== DONE =====\"\r\nfi<\/code><\/pre>\n<p>And <strong>build.sh<\/strong> is a shell script run after every successful\u00a0<em>git push<\/em>\u00a0commit. The following script assumes your Laravel app path is \/var\/www\/laravel-directory and git repository \/var\/www\/git-repository<\/p>\n<pre data-language=\"shell\"><code>#!\/bin\/sh\r\npushd \/var\/www\/laravel-directory\r\ngit pull \/var\/www\/git-repository\r\n# Recompile all class defs into a monolithic file\r\nphp artisan optimize --force\r\nphp artisan config:clear<\/code><\/pre>\n<p>Now the next git push will automatically deploy, generate a class loader, and recompile site configuration:<\/p>\n<pre>$ git push\r\nCounting objects: 5, done.\r\nDelta compression using up to 4 threads.\r\nCompressing objects: 100% (5\/5), done.\r\nWriting objects: 100% (5\/5), 440 bytes | 0 bytes\/s, done.\r\nTotal 5 (delta 4), reused 0 (delta 0)\r\nremote: ===== DEPLOYING TO LIVE SITE =====\r\nremote: \/var\/www\/laravel-production \/var\/www\r\nremote: From .\/..\/git\r\nremote: * branch HEAD -&gt; FETCH_HEAD\r\nremote: Updating b7b99b8..d4f04a5\r\nremote: Fast-forward\r\nremote: config\/filesystems.php | 4 ++--\r\nremote: config\/view.php | 4 ++--\r\nremote: 2 files changed, 4 insertions(+), 4 deletions(-)\r\nremote: Generating optimized class loader\r\nremote: Compiling common classes\r\nremote: Configuration cache cleared!\r\nremote: ===== DONE =====\r\nTo ssh:\/\/apnscp#apnscp.com@luna.apnscp.com\/var\/www\/git\r\n b7b99b8..d4f04a5 master -&gt; master<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel provides a static cache utility\u00a0via Artisan to collapse configuration under config\/ into a single file to boost performance. Configuration may be cached using: php artisan config:cache When run from terminal, the paths provided may be incorrectly referenced when the application is accessed from the web resulting in application errors&#8230;.<\/p>","protected":false},"author":1,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[63],"ht-kb-tag":[],"class_list":["post-8619","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-php"],"_links":{"self":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/comments?post=8619"}],"version-history":[{"count":1,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8619\/revisions"}],"predecessor-version":[{"id":8620,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8619\/revisions\/8620"}],"wp:attachment":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/media?parent=8619"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb-category?post=8619"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb-tag?post=8619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}