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.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 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:     use FileConfigTrait;
 57: 
 58:     /**
 59:      * File extension.
 60:      *
 61:      * @var string
 62:      */
 63:     protected $_extension = '.ini';
 64: 
 65:     /**
 66:      * The section to read, if null all sections will be read.
 67:      *
 68:      * @var string|null
 69:      */
 70:     protected $_section;
 71: 
 72:     /**
 73:      * Build and construct a new ini file parser. The parser can be used to read
 74:      * ini files that are on the filesystem.
 75:      *
 76:      * @param string|null $path Path to load ini config files from. Defaults to CONFIG.
 77:      * @param string|null $section Only get one section, leave null to parse and fetch
 78:      *     all sections in the ini file.
 79:      */
 80:     public function __construct($path = null, $section = null)
 81:     {
 82:         if ($path === null) {
 83:             $path = CONFIG;
 84:         }
 85:         $this->_path = $path;
 86:         $this->_section = $section;
 87:     }
 88: 
 89:     /**
 90:      * Read an ini file and return the results as an array.
 91:      *
 92:      * @param string $key The identifier to read from. If the key has a . it will be treated
 93:      *  as a plugin prefix. The chosen file must be on the engine's path.
 94:      * @return array Parsed configuration values.
 95:      * @throws \Cake\Core\Exception\Exception when files don't exist.
 96:      *  Or when files contain '..' as this could lead to abusive reads.
 97:      */
 98:     public function read($key)
 99:     {
100:         $file = $this->_getFilePath($key, true);
101: 
102:         $contents = parse_ini_file($file, true);
103:         if ($this->_section && isset($contents[$this->_section])) {
104:             $values = $this->_parseNestedValues($contents[$this->_section]);
105:         } else {
106:             $values = [];
107:             foreach ($contents as $section => $attribs) {
108:                 if (is_array($attribs)) {
109:                     $values[$section] = $this->_parseNestedValues($attribs);
110:                 } else {
111:                     $parse = $this->_parseNestedValues([$attribs]);
112:                     $values[$section] = array_shift($parse);
113:                 }
114:             }
115:         }
116: 
117:         return $values;
118:     }
119: 
120:     /**
121:      * parses nested values out of keys.
122:      *
123:      * @param array $values Values to be exploded.
124:      * @return array Array of values exploded
125:      */
126:     protected function _parseNestedValues($values)
127:     {
128:         foreach ($values as $key => $value) {
129:             if ($value === '1') {
130:                 $value = true;
131:             }
132:             if ($value === '') {
133:                 $value = false;
134:             }
135:             unset($values[$key]);
136:             if (strpos($key, '.') !== false) {
137:                 $values = Hash::insert($values, $key, $value);
138:             } else {
139:                 $values[$key] = $value;
140:             }
141:         }
142: 
143:         return $values;
144:     }
145: 
146:     /**
147:      * Dumps the state of Configure data into an ini formatted string.
148:      *
149:      * @param string $key The identifier to write to. If the key has a . it will be treated
150:      *  as a plugin prefix.
151:      * @param array $data The data to convert to ini file.
152:      * @return bool Success.
153:      */
154:     public function dump($key, array $data)
155:     {
156:         $result = [];
157:         foreach ($data as $k => $value) {
158:             $isSection = false;
159:             if ($k[0] !== '[') {
160:                 $result[] = "[$k]";
161:                 $isSection = true;
162:             }
163:             if (is_array($value)) {
164:                 $kValues = Hash::flatten($value, '.');
165:                 foreach ($kValues as $k2 => $v) {
166:                     $result[] = "$k2 = " . $this->_value($v);
167:                 }
168:             }
169:             if ($isSection) {
170:                 $result[] = '';
171:             }
172:         }
173:         $contents = trim(implode("\n", $result));
174: 
175:         $filename = $this->_getFilePath($key);
176: 
177:         return file_put_contents($filename, $contents) > 0;
178:     }
179: 
180:     /**
181:      * Converts a value into the ini equivalent
182:      *
183:      * @param mixed $value Value to export.
184:      * @return string String value for ini file.
185:      */
186:     protected function _value($value)
187:     {
188:         if ($value === null) {
189:             return 'null';
190:         }
191:         if ($value === true) {
192:             return 'true';
193:         }
194:         if ($value === false) {
195:             return 'false';
196:         }
197: 
198:         return (string)$value;
199:     }
200: }
201: 
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