FileConfigTrait.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Core\Configure;
  17. use Cake\Core\Exception\Exception;
  18. use Cake\Core\Plugin;
  19. /**
  20. * Trait providing utility methods for file based config engines.
  21. */
  22. trait FileConfigTrait
  23. {
  24. /**
  25. * The path this engine finds files on.
  26. *
  27. * @var string
  28. */
  29. protected $_path = '';
  30. /**
  31. * Get file path
  32. *
  33. * @param string $key The identifier to write to. If the key has a . it will be treated
  34. * as a plugin prefix.
  35. * @param bool $checkExists Whether to check if file exists. Defaults to false.
  36. * @return string Full file path
  37. * @throws \Cake\Core\Exception\Exception When files don't exist or when
  38. * files contain '..' as this could lead to abusive reads.
  39. */
  40. protected function _getFilePath($key, $checkExists = false)
  41. {
  42. if (strpos($key, '..') !== false) {
  43. throw new Exception('Cannot load/dump configuration files with ../ in them.');
  44. }
  45. list($plugin, $key) = pluginSplit($key);
  46. if ($plugin) {
  47. $file = Plugin::configPath($plugin) . $key;
  48. } else {
  49. $file = $this->_path . $key;
  50. }
  51. $file .= $this->_extension;
  52. if (!$checkExists || is_file($file)) {
  53. return $file;
  54. }
  55. $realPath = realpath($file);
  56. if ($realPath !== false && is_file($realPath)) {
  57. return $realPath;
  58. }
  59. throw new Exception(sprintf('Could not load configuration file: %s', $file));
  60. }
  61. }