CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.7 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.7
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • IniConfig
  • JsonConfig
  • PhpConfig
  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         2.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Core\Configure\Engine;
 16: 
 17: use Cake\Core\Configure\ConfigEngineInterface;
 18: use Cake\Core\Configure\FileConfigTrait;
 19: use Cake\Utility\Hash;
 20: 
 21: /**
 22:  * Ini file configuration engine.
 23:  *
 24:  * Since IniConfig uses parse_ini_file underneath, you should be aware that this
 25:  * class shares the same behavior, especially with regards to boolean and null values.
 26:  *
 27:  * In addition to the native `parse_ini_file` features, IniConfig also allows you
 28:  * to create nested array structures through usage of `.` delimited names. This allows
 29:  * you to create nested arrays structures in an ini config file. For example:
 30:  *
 31:  * `db.password = secret` would turn into `['db' => ['password' => 'secret']]`
 32:  *
 33:  * You can nest properties as deeply as needed using `.`'s. In addition to using `.` you
 34:  * can use standard ini section notation to create nested structures:
 35:  *
 36:  * ```
 37:  * [section]
 38:  * key = value
 39:  * ```
 40:  *
 41:  * Once loaded into Configure, the above would be accessed using:
 42:  *
 43:  * `Configure::read('section.key');
 44:  *
 45:  * You can also use `.` separated values in section names to create more deeply
 46:  * nested structures.
 47:  *
 48:  * IniConfig also manipulates how the special ini values of
 49:  * 'yes', 'no', 'on', 'off', 'null' are handled. These values will be
 50:  * converted to their boolean equivalents.
 51:  *
 52:  * @see https://secure.php.net/parse_ini_file
 53:  */
 54: class IniConfig implements ConfigEngineInterface
 55: {
 56: 
 57:     use FileConfigTrait;
 58: 
 59:     /**
 60:      * File extension.
 61:      *
 62:      * @var string
 63:      */
 64:     protected $_extension = '.ini';
 65: 
 66:     /**
 67:      * The section to read, if null all sections will be read.
 68:      *
 69:      * @var string|null
 70:      */
 71:     protected $_section;
 72: 
 73:     /**
 74:      * Build and construct a new ini file parser. The parser can be used to read
 75:      * ini files that are on the filesystem.
 76:      *
 77:      * @param string|null $path Path to load ini config files from. Defaults to CONFIG.
 78:      * @param string|null $section Only get one section, leave null to parse and fetch
 79:      *     all sections in the ini file.
 80:      */
 81:     public function __construct($path = null, $section = null)
 82:     {
 83:         if ($path === null) {
 84:             $path = CONFIG;
 85:         }
 86:         $this->_path = $path;
 87:         $this->_section = $section;
 88:     }
 89: 
 90:     /**
 91:      * Read an ini file and return the results as an array.
 92:      *
 93:      * @param string $key The identifier to read from. If the key has a . it will be treated
 94:      *  as a plugin prefix. The chosen file must be on the engine's path.
 95:      * @return array Parsed configuration values.
 96:      * @throws \Cake\Core\Exception\Exception when files don't exist.
 97:      *  Or when files contain '..' as this could lead to abusive reads.
 98:      */
 99:     public function read($key)
100:     {
101:         $file = $this->_getFilePath($key, true);
102: 
103:         $contents = parse_ini_file($file, true);
104:         if ($this->_section && isset($contents[$this->_section])) {
105:             $values = $this->_parseNestedValues($contents[$this->_section]);
106:         } else {
107:             $values = [];
108:             foreach ($contents as $section => $attribs) {
109:                 if (is_array($attribs)) {
110:                     $values[$section] = $this->_parseNestedValues($attribs);
111:                 } else {
112:                     $parse = $this->_parseNestedValues([$attribs]);
113:                     $values[$section] = array_shift($parse);
114:                 }
115:             }
116:         }
117: 
118:         return $values;
119:     }
120: 
121:     /**
122:      * parses nested values out of keys.
123:      *
124:      * @param array $values Values to be exploded.
125:      * @return array Array of values exploded
126:      */
127:     protected function _parseNestedValues($values)
128:     {
129:         foreach ($values as $key => $value) {
130:             if ($value === '1') {
131:                 $value = true;
132:             }
133:             if ($value === '') {
134:                 $value = false;
135:             }
136:             unset($values[$key]);
137:             if (strpos($key, '.') !== false) {
138:                 $values = Hash::insert($values, $key, $value);
139:             } else {
140:                 $values[$key] = $value;
141:             }
142:         }
143: 
144:         return $values;
145:     }
146: 
147:     /**
148:      * Dumps the state of Configure data into an ini formatted string.
149:      *
150:      * @param string $key The identifier to write to. If the key has a . it will be treated
151:      *  as a plugin prefix.
152:      * @param array $data The data to convert to ini file.
153:      * @return bool Success.
154:      */
155:     public function dump($key, array $data)
156:     {
157:         $result = [];
158:         foreach ($data as $k => $value) {
159:             $isSection = false;
160:             if ($k[0] !== '[') {
161:                 $result[] = "[$k]";
162:                 $isSection = true;
163:             }
164:             if (is_array($value)) {
165:                 $kValues = Hash::flatten($value, '.');
166:                 foreach ($kValues as $k2 => $v) {
167:                     $result[] = "$k2 = " . $this->_value($v);
168:                 }
169:             }
170:             if ($isSection) {
171:                 $result[] = '';
172:             }
173:         }
174:         $contents = trim(implode("\n", $result));
175: 
176:         $filename = $this->_getFilePath($key);
177: 
178:         return file_put_contents($filename, $contents) > 0;
179:     }
180: 
181:     /**
182:      * Converts a value into the ini equivalent
183:      *
184:      * @param mixed $value Value to export.
185:      * @return string String value for ini file.
186:      */
187:     protected function _value($value)
188:     {
189:         if ($value === null) {
190:             return 'null';
191:         }
192:         if ($value === true) {
193:             return 'true';
194:         }
195:         if ($value === false) {
196:             return 'false';
197:         }
198: 
199:         return (string)$value;
200:     }
201: }
202: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs