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

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.         $this->loadDcaFiles($blnNoCache);
  60.     }
  61.     /**
  62.      * Load the DCA files
  63.      *
  64.      * @param boolean $blnNoCache
  65.      */
  66.     private function loadDcaFiles($blnNoCache)
  67.     {
  68.         // Return if the data has been loaded already
  69.         if (!$blnNoCache && isset(static::$arrLoaded['dcaFiles'][$this->strTable]))
  70.         {
  71.             return;
  72.         }
  73.         static::$arrLoaded['dcaFiles'][$this->strTable] = true// see #6145
  74.         $strCacheDir System::getContainer()->getParameter('kernel.cache_dir');
  75.         // Try to load from cache
  76.         if (file_exists($strCacheDir '/contao/dca/' $this->strTable '.php'))
  77.         {
  78.             include $strCacheDir '/contao/dca/' $this->strTable '.php';
  79.         }
  80.         else
  81.         {
  82.             try
  83.             {
  84.                 $files System::getContainer()->get('contao.resource_locator')->locate('dca/' $this->strTable '.php'nullfalse);
  85.             }
  86.             catch (\InvalidArgumentException $e)
  87.             {
  88.                 $files = array();
  89.             }
  90.             foreach ($files as $file)
  91.             {
  92.                 include $file;
  93.             }
  94.         }
  95.         // Set the ptable dynamically
  96.         $this->setDynamicPTable();
  97.         // HOOK: allow loading custom settings
  98.         if (isset($GLOBALS['TL_HOOKS']['loadDataContainer']) && \is_array($GLOBALS['TL_HOOKS']['loadDataContainer']))
  99.         {
  100.             foreach ($GLOBALS['TL_HOOKS']['loadDataContainer'] as $callback)
  101.             {
  102.                 $this->import($callback[0]);
  103.                 $this->{$callback[0]}->{$callback[1]}($this->strTable);
  104.             }
  105.         }
  106.         $projectDir System::getContainer()->getParameter('kernel.project_dir');
  107.         // Local configuration file
  108.         if (file_exists($projectDir '/system/config/dcaconfig.php'))
  109.         {
  110.             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.');
  111.             include $projectDir '/system/config/dcaconfig.php';
  112.         }
  113.         $this->addDefaultLabels($blnNoCache);
  114.     }
  115.     /**
  116.      * Add the default labels (see #509)
  117.      *
  118.      * @param boolean $blnNoCache
  119.      */
  120.     private function addDefaultLabels($blnNoCache)
  121.     {
  122.         // Operations
  123.         foreach (array('global_operations''operations') as $key)
  124.         {
  125.             if (!isset($GLOBALS['TL_DCA'][$this->strTable]['list'][$key]))
  126.             {
  127.                 continue;
  128.             }
  129.             foreach ($GLOBALS['TL_DCA'][$this->strTable]['list'][$key] as $k=>&$v)
  130.             {
  131.                 if (isset($v['label']))
  132.                 {
  133.                     continue;
  134.                 }
  135.                 if (isset($GLOBALS['TL_LANG'][$this->strTable][$k]) || !isset($GLOBALS['TL_LANG']['DCA'][$k]))
  136.                 {
  137.                     $v['label'] = &$GLOBALS['TL_LANG'][$this->strTable][$k];
  138.                 }
  139.                 elseif (isset($GLOBALS['TL_LANG']['DCA'][$k]))
  140.                 {
  141.                     $v['label'] = &$GLOBALS['TL_LANG']['DCA'][$k];
  142.                 }
  143.             }
  144.             unset($v);
  145.         }
  146.         // Fields
  147.         if (isset($GLOBALS['TL_DCA'][$this->strTable]['fields']))
  148.         {
  149.             foreach ($GLOBALS['TL_DCA'][$this->strTable]['fields'] as $k=>&$v)
  150.             {
  151.                 if (isset($v['label']))
  152.                 {
  153.                     continue;
  154.                 }
  155.                 $v['label'] = &$GLOBALS['TL_LANG'][$this->strTable][$k];
  156.             }
  157.             unset($v);
  158.         }
  159.     }
  160.     /**
  161.      * Sets the parent table for the current table, if enabled and not set.
  162.      */
  163.     private function setDynamicPTable(): void
  164.     {
  165.         if (!($GLOBALS['TL_DCA'][$this->strTable]['config']['dynamicPtable'] ?? null) || !isset($GLOBALS['BE_MOD']))
  166.         {
  167.             return;
  168.         }
  169.         if (!$do Input::get('do'))
  170.         {
  171.             return;
  172.         }
  173.         foreach (array_merge(...array_values($GLOBALS['BE_MOD'])) as $key => $module)
  174.         {
  175.             if ($do !== $key || !isset($module['tables']) || !\is_array($module['tables']))
  176.             {
  177.                 continue;
  178.             }
  179.             foreach ($module['tables'] as $table)
  180.             {
  181.                 Controller::loadDataContainer($table);
  182.                 $ctable $GLOBALS['TL_DCA'][$table]['config']['ctable'] ?? array();
  183.                 if (\in_array($this->strTable$ctabletrue))
  184.                 {
  185.                     $GLOBALS['TL_DCA'][$this->strTable]['config']['ptable'] = $table;
  186.                     return;
  187.                 }
  188.             }
  189.         }
  190.     }
  191. }
  192. class_alias(DcaLoader::class, 'DcaLoader');