pictcode / lib / Cake / Configure / PhpReader.php @ 6e557b3d
履歴 | 表示 | アノテート | ダウンロード (3.387 KB)
| 1 | 635eef61 | spyder1211 | <?php
 | 
      
|---|---|---|---|
| 2 | /**
 | 
      ||
| 3 |  * PhpReader file
 | 
      ||
| 4 |  *
 | 
      ||
| 5 |  * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
 | 
      ||
| 12 |  * @link          http://book.cakephp.org/2.0/en/development/configuration.html#loading-configuration-files CakePHP(tm) Configuration
 | 
      ||
| 13 |  * @package       Cake.Configure
 | 
      ||
| 14 |  * @since         CakePHP(tm) v 2.0
 | 
      ||
| 15 |  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 | 
      ||
| 16 |  */
 | 
      ||
| 17 | |||
| 18 | App::uses('CakePlugin', 'Core');  | 
      ||
| 19 | |||
| 20 | /**
 | 
      ||
| 21 |  * PHP Reader allows Configure to load configuration values from
 | 
      ||
| 22 |  * files containing simple PHP arrays.
 | 
      ||
| 23 |  *
 | 
      ||
| 24 |  * Files compatible with PhpReader should define a `$config` variable, that
 | 
      ||
| 25 |  * contains all of the configuration data contained in the file.
 | 
      ||
| 26 |  *
 | 
      ||
| 27 |  * @package       Cake.Configure
 | 
      ||
| 28 |  */
 | 
      ||
| 29 | class PhpReader implements ConfigReaderInterface {  | 
      ||
| 30 | |||
| 31 | /**
 | 
      ||
| 32 |  * The path this reader finds files on.
 | 
      ||
| 33 |  *
 | 
      ||
| 34 |  * @var string
 | 
      ||
| 35 |  */
 | 
      ||
| 36 | protected $_path = null;  | 
      ||
| 37 | |||
| 38 | /**
 | 
      ||
| 39 |  * Constructor for PHP Config file reading.
 | 
      ||
| 40 |  *
 | 
      ||
| 41 |  * @param string $path The path to read config files from. Defaults to APP . 'Config' . DS
 | 
      ||
| 42 |  */
 | 
      ||
| 43 | public function __construct($path = null) {  | 
      ||
| 44 | if (!$path) {  | 
      ||
| 45 | $path = APP . 'Config' . DS;  | 
      ||
| 46 | }  | 
      ||
| 47 | $this->_path = $path;  | 
      ||
| 48 | }  | 
      ||
| 49 | |||
| 50 | /**
 | 
      ||
| 51 |  * Read a config file and return its contents.
 | 
      ||
| 52 |  *
 | 
      ||
| 53 |  * Files with `.` in the name will be treated as values in plugins. Instead of reading from
 | 
      ||
| 54 |  * the initialized path, plugin keys will be located using CakePlugin::path().
 | 
      ||
| 55 |  *
 | 
      ||
| 56 |  * @param string $key The identifier to read from. If the key has a . it will be treated
 | 
      ||
| 57 |  *  as a plugin prefix.
 | 
      ||
| 58 |  * @return array Parsed configuration values.
 | 
      ||
| 59 |  * @throws ConfigureException when files don't exist or they don't contain `$config`.
 | 
      ||
| 60 |  *  Or when files contain '..' as this could lead to abusive reads.
 | 
      ||
| 61 |  */
 | 
      ||
| 62 | public function read($key) {  | 
      ||
| 63 | if (strpos($key, '..') !== false) {  | 
      ||
| 64 | throw new ConfigureException(__d('cake_dev', 'Cannot load configuration files with ../ in them.'));  | 
      ||
| 65 | }  | 
      ||
| 66 | |||
| 67 | $file = $this->_getFilePath($key);  | 
      ||
| 68 | if (!is_file($file)) {  | 
      ||
| 69 | throw new ConfigureException(__d('cake_dev', 'Could not load configuration file: %s', $file));  | 
      ||
| 70 | }  | 
      ||
| 71 | |||
| 72 | include $file;  | 
      ||
| 73 | if (!isset($config)) {  | 
      ||
| 74 | throw new ConfigureException(__d('cake_dev', 'No variable %s found in %s', '$config', $file));  | 
      ||
| 75 | }  | 
      ||
| 76 | return $config;  | 
      ||
| 77 | }  | 
      ||
| 78 | |||
| 79 | /**
 | 
      ||
| 80 |  * Converts the provided $data into a string of PHP code that can
 | 
      ||
| 81 |  * be used saved into a file and loaded later.
 | 
      ||
| 82 |  *
 | 
      ||
| 83 |  * @param string $key The identifier to write to. If the key has a . it will be treated
 | 
      ||
| 84 |  *  as a plugin prefix.
 | 
      ||
| 85 |  * @param array $data Data to dump.
 | 
      ||
| 86 |  * @return int Bytes saved.
 | 
      ||
| 87 |  */
 | 
      ||
| 88 | public function dump($key, $data) {  | 
      ||
| 89 | $contents = '<?php' . "\n" . '$config = ' . var_export($data, true) . ';';  | 
      ||
| 90 | |||
| 91 | $filename = $this->_getFilePath($key);  | 
      ||
| 92 | return file_put_contents($filename, $contents);  | 
      ||
| 93 | }  | 
      ||
| 94 | |||
| 95 | /**
 | 
      ||
| 96 |  * Get file path
 | 
      ||
| 97 |  *
 | 
      ||
| 98 |  * @param string $key The identifier to write to. If the key has a . it will be treated
 | 
      ||
| 99 |  *  as a plugin prefix.
 | 
      ||
| 100 |  * @return string Full file path
 | 
      ||
| 101 |  */
 | 
      ||
| 102 | protected function _getFilePath($key) {  | 
      ||
| 103 | if (substr($key, -4) === '.php') {  | 
      ||
| 104 | $key = substr($key, 0, -4);  | 
      ||
| 105 | }  | 
      ||
| 106 | list($plugin, $key) = pluginSplit($key);  | 
      ||
| 107 | $key .= '.php';  | 
      ||
| 108 | |||
| 109 | if ($plugin) {  | 
      ||
| 110 | $file = CakePlugin::path($plugin) . 'Config' . DS . $key;  | 
      ||
| 111 |                 } else {
 | 
      ||
| 112 | $file = $this->_path . $key;  | 
      ||
| 113 | }  | 
      ||
| 114 | |||
| 115 | return $file;  | 
      ||
| 116 | }  | 
      ||
| 117 | |||
| 118 | }  |