FileConfigTrait.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. if (is_file(realpath($file))) {
  55. return realpath($file);
  56. }
  57. throw new Exception(sprintf('Could not load configuration file: %s', $file));
  58. }
  59. }