(dev/cloud-native#3) Container, Extension Classloader - Change CIVICRM_TEMPLATE_COMPI...
authorTim Otten <totten@civicrm.org>
Wed, 10 Jul 2019 05:10:05 +0000 (22:10 -0700)
committerTim Otten <totten@civicrm.org>
Tue, 16 Jul 2019 01:58:12 +0000 (18:58 -0700)
The broader PR seeks to make path computation more intuitive, which requires computing the
path to `templates_c` using a function. This PR replaces the reference to `CIVICRM_TEMPLATE_COMPILEDIR`
with a function-call to `Civi::paths()->getPath()`.

Why change these two files in the same commit? Because they're basically doing the same
thing (writing an executable PHP file to the template cache), and the demonstration
of their safety is basically the same.

Is it safe to change this reference to `CIVICRM_TEMPLATE_COMPILEDIR` in
`Civi\Core\Container::loadContainer()` and `CRM_Extension_ClassLoader::register()/::getCacheFile()`?  Yes, I believe so:

* Look at `Civi\Core\Container::boot()`.
* Observe that it initializes services in two general stages:
    * First, the `$bootServices` (`runtime`, `paths`, `userSystem`, etc)
    * Second, the DB/extension-dependent services (`CRM_Utils_Hook`, `CRM_Extension_System`, `loadContainer()`).
* The first stage services (e.g. `paths`) provide enough information
  to process `Civi::paths()->getPath('[civicrm.compile]/foo')`.
* All the modified lines run as part of the *second* stage (i.e. after
  the `$bootServices` have been initialized).

CRM/Extension/ClassLoader.php
Civi/Core/Container.php

index bba8731510ecbde73aff08e935b0f06edae166f6..4569f6c2e4ee498b2af85609e04db30b22d59fe7 100644 (file)
@@ -77,7 +77,7 @@ class CRM_Extension_ClassLoader {
    */
   public function register() {
     // In pre-installation environments, don't bother with caching.
-    if (!defined('CIVICRM_TEMPLATE_COMPILEDIR') || !defined('CIVICRM_DSN') || defined('CIVICRM_TEST') || \CRM_Utils_System::isInUpgradeMode()) {
+    if (!defined('CIVICRM_DSN') || defined('CIVICRM_TEST') || \CRM_Utils_System::isInUpgradeMode()) {
       return $this->buildClassLoader()->register();
     }
 
@@ -146,7 +146,7 @@ class CRM_Extension_ClassLoader {
    */
   protected function getCacheFile() {
     $envId = \CRM_Core_Config_Runtime::getId();
-    $file = CIVICRM_TEMPLATE_COMPILEDIR . "/CachedExtLoader.{$envId}.php";
+    $file = \Civi::paths()->getPath("[civicrm.compile]/CachedExtLoader.{$envId}.php");
     return $file;
   }
 
index 55c2b92e2b317c6d9b15be97e09ce776ae08e603..9c78f0b3126c57686e909977d6b8125c30a56293 100644 (file)
@@ -46,7 +46,6 @@ class Container {
    *   - CIVICRM_CONTAINER_CACHE -- 'always' [default], 'never', 'auto'
    *   - CIVICRM_DSN
    *   - CIVICRM_DOMAIN_ID
-   *   - CIVICRM_TEMPLATE_COMPILEDIR
    *
    * @return \Symfony\Component\DependencyInjection\ContainerInterface
    */
@@ -58,14 +57,14 @@ class Container {
     $cacheMode = defined('CIVICRM_CONTAINER_CACHE') ? CIVICRM_CONTAINER_CACHE : 'auto';
 
     // In pre-installation environments, don't bother with caching.
-    if (!defined('CIVICRM_TEMPLATE_COMPILEDIR') || !defined('CIVICRM_DSN') || $cacheMode === 'never' || \CRM_Utils_System::isInUpgradeMode()) {
+    if (!defined('CIVICRM_DSN') || $cacheMode === 'never' || \CRM_Utils_System::isInUpgradeMode()) {
       $containerBuilder = $this->createContainer();
       $containerBuilder->compile();
       return $containerBuilder;
     }
 
     $envId = \CRM_Core_Config_Runtime::getId();
-    $file = CIVICRM_TEMPLATE_COMPILEDIR . "/CachedCiviContainer.{$envId}.php";
+    $file = \Civi::paths()->getPath("[civicrm.compile]/CachedCiviContainer.{$envId}.php");
     $containerConfigCache = new ConfigCache($file, $cacheMode === 'auto');
     if (!$containerConfigCache->isFresh()) {
       $containerBuilder = $this->createContainer();