vendor/contao/core-bundle/src/Resources/contao/library/Contao/DcaLoader.php line 115

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. /**
  11.  * Loads a set of DCA files
  12.  *
  13.  * The class loads the DCA files of a certain table and stores a combined
  14.  * version in the cache directory.
  15.  *
  16.  * Usage:
  17.  *
  18.  *     $dca = new DcaLoader('tl_user');
  19.  *     $dca->load();
  20.  */
  21. class DcaLoader extends Controller
  22. {
  23.     /**
  24.      * @var array
  25.      */
  26.     protected static $arrLoaded = array();
  27.     /**
  28.      * Table name
  29.      * @var string
  30.      */
  31.     protected $strTable;
  32.     /**
  33.      * Store the table name
  34.      *
  35.      * @param string $strTable The table name
  36.      *
  37.      * @throws \Exception If $strTable is empty
  38.      */
  39.     public function __construct($strTable)
  40.     {
  41.         if (!$strTable)
  42.         {
  43.             throw new \Exception('The table name must not be empty');
  44.         }
  45.         if (Validator::isInsecurePath($strTable))
  46.         {
  47.             throw new \InvalidArgumentException('The table name contains invalid characters');
  48.         }
  49.         parent::__construct();
  50.         $this->strTable $strTable;
  51.     }
  52.     /**
  53.      * Load a set of DCA files
  54.      *
  55.      * @param boolean $blnNoCache If true, the cache will be bypassed
  56.      */
  57.     public function load($blnNoCache=false)
  58.     {
  59.         try
  60.         {
  61.             $this->loadDcaFiles($blnNoCache);
  62.         }
  63.         catch (\Throwable $e)
  64.         {
  65.             unset(static::$arrLoaded['dcaFiles'][$this->strTable]);
  66.             throw $e;
  67.         }
  68.     }
  69.     /**
  70.      * Load the DCA files
  71.      *
  72.      * @param boolean $blnNoCache
  73.      */
  74.     private function loadDcaFiles($blnNoCache)
  75.     {
  76.         // Return if the data has been loaded already
  77.         if (!$blnNoCache && isset(static::$arrLoaded['dcaFiles'][$this->strTable]))
  78.         {
  79.             return;
  80.         }
  81.         static::$arrLoaded['dcaFiles'][$this->strTable] = true// see #6145
  82.         $strCacheDir System::getContainer()->getParameter('kernel.cache_dir');
  83.         // Try to load from cache
  84.         if (file_exists($strCacheDir '/contao/dca/' $this->strTable '.php'))
  85.         {
  86.             include $strCacheDir '/contao/dca/' $this->strTable '.php';
  87.         }
  88.         else
  89.         {
  90.             try
  91.             {
  92.                 $files System::getContainer()->get('contao.resource_locator')->locate('dca/' $this->strTable '.php'nullfalse);
  93.             }
  94.             catch (\InvalidArgumentException $e)
  95.             {
  96.                 $files = array();
  97.             }
  98.             foreach ($files as $file)
  99.             {
  100.                 include $file;
  101.             }
  102.         }
  103.         // Set the ptable dynamically
  104.         $this->setDynamicPTable();
  105.         // HOOK: allow loading custom settings
  106.         if (isset($GLOBALS['TL_HOOKS']['loadDataContainer']) && \is_array($GLOBALS['TL_HOOKS']['loadDataContainer']))
  107.         {
  108.             foreach ($GLOBALS['TL_HOOKS']['loadDataContainer'] as $callback)
  109.             {
  110.                 $this->import($callback[0]);
  111.                 $this->{$callback[0]}->{$callback[1]}($this->strTable);
  112.             }
  113.         }
  114.         $projectDir System::getContainer()->getParameter('kernel.project_dir');
  115.         // Local configuration file
  116.         if (file_exists($projectDir '/system/config/dcaconfig.php'))
  117.         {
  118.             trigger_deprecation('contao/core-bundle''4.3''Using the "dcaconfig.php" file has been deprecated and will no longer work in Contao 5.0. Create custom DCA files in the "contao/dca" folder instead.');
  119.             include $projectDir '/system/config/dcaconfig.php';
  120.         }
  121.         $this->addDefaultLabels($blnNoCache);
  122.     }
  123.     /**
  124.      * Add the default labels (see #509)
  125.      *
  126.      * @param boolean $blnNoCache
  127.      */
  128.     private function addDefaultLabels($blnNoCache)
  129.     {
  130.         // Operations
  131.         foreach (array('global_operations''operations') as $key)
  132.         {
  133.             if (!isset($GLOBALS['TL_DCA'][$this->strTable]['list'][$key]))
  134.             {
  135.                 continue;
  136.             }
  137.             foreach ($GLOBALS['TL_DCA'][$this->strTable]['list'][$key] as $k=>&$v)
  138.             {
  139.                 if (isset($v['label']))
  140.                 {
  141.                     continue;
  142.                 }
  143.                 if (isset($GLOBALS['TL_LANG'][$this->strTable][$k]) || !isset($GLOBALS['TL_LANG']['DCA'][$k]))
  144.                 {
  145.                     $v['label'] = &$GLOBALS['TL_LANG'][$this->strTable][$k];
  146.                 }
  147.                 elseif (isset($GLOBALS['TL_LANG']['DCA'][$k]))
  148.                 {
  149.                     $v['label'] = &$GLOBALS['TL_LANG']['DCA'][$k];
  150.                 }
  151.             }
  152.             unset($v);
  153.         }
  154.         // Fields
  155.         if (isset($GLOBALS['TL_DCA'][$this->strTable]['fields']))
  156.         {
  157.             foreach ($GLOBALS['TL_DCA'][$this->strTable]['fields'] as $k=>&$v)
  158.             {
  159.                 if (isset($v['label']))
  160.                 {
  161.                     continue;
  162.                 }
  163.                 $v['label'] = &$GLOBALS['TL_LANG'][$this->strTable][$k];
  164.             }
  165.             unset($v);
  166.         }
  167.     }
  168.     /**
  169.      * Sets the parent table for the current table, if enabled and not set.
  170.      */
  171.     private function setDynamicPTable(): void
  172.     {
  173.         if (!($GLOBALS['TL_DCA'][$this->strTable]['config']['dynamicPtable'] ?? null) || !isset($GLOBALS['BE_MOD']))
  174.         {
  175.             return;
  176.         }
  177.         if (!$do Input::get('do'))
  178.         {
  179.             return;
  180.         }
  181.         foreach (array_merge(...array_values($GLOBALS['BE_MOD'])) as $key => $module)
  182.         {
  183.             if ($do !== $key || !isset($module['tables']) || !\is_array($module['tables']))
  184.             {
  185.                 continue;
  186.             }
  187.             foreach ($module['tables'] as $table)
  188.             {
  189.                 Controller::loadDataContainer($table);
  190.                 $ctable $GLOBALS['TL_DCA'][$table]['config']['ctable'] ?? array();
  191.                 if (\in_array($this->strTable$ctabletrue))
  192.                 {
  193.                     $GLOBALS['TL_DCA'][$this->strTable]['config']['ptable'] = $table;
  194.                     return;
  195.                 }
  196.             }
  197.         }
  198.     }
  199. }
  200. class_alias(DcaLoader::class, 'DcaLoader');