FileConfigTrait.php 2.0 KB

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