PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/robmorgan/phinx/src/Phinx/Config/Config.php

https://gitlab.com/alexandresgv/siteentec
PHP | 383 lines | 252 code | 31 blank | 100 comment | 15 complexity | 1625aa49a73df6eebed73941d7d0540c MD5 | raw file
  1. <?php
  2. /**
  3. * Phinx
  4. *
  5. * (The MIT license)
  6. * Copyright (c) 2015 Rob Morgan
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated * documentation files (the "Software"), to
  10. * deal in the Software without restriction, including without limitation the
  11. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. *
  26. * @package Phinx
  27. * @subpackage Phinx\Config
  28. */
  29. namespace Phinx\Config;
  30. use Symfony\Component\Yaml\Yaml;
  31. /**
  32. * Phinx configuration class.
  33. *
  34. * @package Phinx
  35. * @author Rob Morgan
  36. */
  37. class Config implements ConfigInterface
  38. {
  39. /**
  40. * @var array
  41. */
  42. private $values = array();
  43. /**
  44. * @var string
  45. */
  46. protected $configFilePath;
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function __construct(array $configArray, $configFilePath = null)
  51. {
  52. $this->configFilePath = $configFilePath;
  53. $this->values = $this->replaceTokens($configArray);
  54. }
  55. /**
  56. * Create a new instance of the config class using a Yaml file path.
  57. *
  58. * @param string $configFilePath Path to the Yaml File
  59. * @throws \RuntimeException
  60. * @return Config
  61. */
  62. public static function fromYaml($configFilePath)
  63. {
  64. $configFile = file_get_contents($configFilePath);
  65. $configArray = Yaml::parse($configFile);
  66. if (!is_array($configArray)) {
  67. throw new \RuntimeException(sprintf(
  68. 'File \'%s\' must be valid YAML',
  69. $configFilePath
  70. ));
  71. }
  72. return new static($configArray, $configFilePath);
  73. }
  74. /**
  75. * Create a new instance of the config class using a JSON file path.
  76. *
  77. * @param string $configFilePath Path to the JSON File
  78. * @throws \RuntimeException
  79. * @return Config
  80. */
  81. public static function fromJson($configFilePath)
  82. {
  83. $configArray = json_decode(file_get_contents($configFilePath), true);
  84. if (!is_array($configArray)) {
  85. throw new \RuntimeException(sprintf(
  86. 'File \'%s\' must be valid JSON',
  87. $configFilePath
  88. ));
  89. }
  90. return new static($configArray, $configFilePath);
  91. }
  92. /**
  93. * Create a new instance of the config class using a PHP file path.
  94. *
  95. * @param string $configFilePath Path to the PHP File
  96. * @throws \RuntimeException
  97. * @return Config
  98. */
  99. public static function fromPhp($configFilePath)
  100. {
  101. ob_start();
  102. /** @noinspection PhpIncludeInspection */
  103. $configArray = include($configFilePath);
  104. // Hide console output
  105. ob_end_clean();
  106. if (!is_array($configArray)) {
  107. throw new \RuntimeException(sprintf(
  108. 'PHP file \'%s\' must return an array',
  109. $configFilePath
  110. ));
  111. }
  112. return new static($configArray, $configFilePath);
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function getEnvironments()
  118. {
  119. if (isset($this->values) && isset($this->values['environments'])) {
  120. $environments = array();
  121. foreach ($this->values['environments'] as $key => $value) {
  122. if (is_array($value)) {
  123. $environments[$key] = $value;
  124. }
  125. }
  126. return $environments;
  127. }
  128. return null;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getEnvironment($name)
  134. {
  135. $environments = $this->getEnvironments();
  136. if (isset($environments[$name])) {
  137. if (isset($this->values['environments']['default_migration_table'])) {
  138. $environments[$name]['default_migration_table'] =
  139. $this->values['environments']['default_migration_table'];
  140. }
  141. return $environments[$name];
  142. }
  143. return null;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function hasEnvironment($name)
  149. {
  150. return (null !== $this->getEnvironment($name));
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getDefaultEnvironment()
  156. {
  157. // The $PHINX_ENVIRONMENT variable overrides all other default settings
  158. $env = getenv('PHINX_ENVIRONMENT');
  159. if (!empty($env)) {
  160. if ($this->hasEnvironment($env)) {
  161. return $env;
  162. }
  163. throw new \RuntimeException(sprintf(
  164. 'The environment configuration (read from $PHINX_ENVIRONMENT) for \'%s\' is missing',
  165. $env
  166. ));
  167. }
  168. // if the user has configured a default database then use it,
  169. // providing it actually exists!
  170. if (isset($this->values['environments']['default_database'])) {
  171. if ($this->getEnvironment($this->values['environments']['default_database'])) {
  172. return $this->values['environments']['default_database'];
  173. }
  174. throw new \RuntimeException(sprintf(
  175. 'The environment configuration for \'%s\' is missing',
  176. $this->values['environments']['default_database']
  177. ));
  178. }
  179. // else default to the first available one
  180. if (is_array($this->getEnvironments()) && count($this->getEnvironments()) > 0) {
  181. $names = array_keys($this->getEnvironments());
  182. return $names[0];
  183. }
  184. throw new \RuntimeException('Could not find a default environment');
  185. }
  186. /**
  187. * Get the aliased value from a supplied alias.
  188. *
  189. * @param string $alias
  190. *
  191. * @return string|null
  192. */
  193. public function getAlias($alias){
  194. return !empty($this->values['aliases'][$alias]) ? $this->values['aliases'][$alias] : null;
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function getConfigFilePath()
  200. {
  201. return $this->configFilePath;
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function getMigrationPath()
  207. {
  208. if (!isset($this->values['paths']['migrations'])) {
  209. throw new \UnexpectedValueException('Migrations path missing from config file');
  210. }
  211. return $this->values['paths']['migrations'];
  212. }
  213. /**
  214. * Gets the base class name for migrations.
  215. *
  216. * @param boolean $dropNamespace Return the base migration class name without the namespace.
  217. * @return string
  218. */
  219. public function getMigrationBaseClassName($dropNamespace = true)
  220. {
  221. $className = !isset($this->values['migration_base_class']) ? 'Phinx\Migration\AbstractMigration' : $this->values['migration_base_class'];
  222. return $dropNamespace ? substr(strrchr($className, '\\'), 1) : $className;
  223. }
  224. /**
  225. * {@inheritdoc}
  226. */
  227. public function getSeedPath()
  228. {
  229. if (!isset($this->values['paths']['seeds'])) {
  230. throw new \UnexpectedValueException('Seeds path missing from config file');
  231. }
  232. return $this->values['paths']['seeds'];
  233. }
  234. /**
  235. * Get the template file name.
  236. *
  237. * @return string|false
  238. */
  239. public function getTemplateFile()
  240. {
  241. if (!isset($this->values['templates']['file'])) {
  242. return false;
  243. }
  244. return $this->values['templates']['file'];
  245. }
  246. /**
  247. * Get the template class name.
  248. *
  249. * @return string|false
  250. */
  251. public function getTemplateClass()
  252. {
  253. if (!isset($this->values['templates']['class'])) {
  254. return false;
  255. }
  256. return $this->values['templates']['class'];
  257. }
  258. /**
  259. * Replace tokens in the specified array.
  260. *
  261. * @param array $arr Array to replace
  262. * @return array
  263. */
  264. protected function replaceTokens(array $arr)
  265. {
  266. // Get environment variables
  267. // $_ENV is empty because variables_order does not include it normally
  268. $tokens = array();
  269. foreach ($_SERVER as $varname => $varvalue) {
  270. if (0 === strpos($varname, 'PHINX_')) {
  271. $tokens['%%' . $varname . '%%'] = $varvalue;
  272. }
  273. }
  274. // Phinx defined tokens (override env tokens)
  275. $tokens['%%PHINX_CONFIG_PATH%%'] = $this->getConfigFilePath();
  276. $tokens['%%PHINX_CONFIG_DIR%%'] = dirname($this->getConfigFilePath());
  277. // Recurse the array and replace tokens
  278. return $this->recurseArrayForTokens($arr, $tokens);
  279. }
  280. /**
  281. * Recurse an array for the specified tokens and replace them.
  282. *
  283. * @param array $arr Array to recurse
  284. * @param array $tokens Array of tokens to search for
  285. * @return array
  286. */
  287. protected function recurseArrayForTokens($arr, $tokens)
  288. {
  289. $out = array();
  290. foreach ($arr as $name => $value) {
  291. if (is_array($value)) {
  292. $out[$name] = $this->recurseArrayForTokens($value, $tokens);
  293. continue;
  294. }
  295. if (is_string($value)) {
  296. foreach ($tokens as $token => $tval) {
  297. $value = str_replace($token, $tval, $value);
  298. }
  299. $out[$name] = $value;
  300. continue;
  301. }
  302. $out[$name] = $value;
  303. }
  304. return $out;
  305. }
  306. /**
  307. * {@inheritdoc}
  308. */
  309. public function offsetSet($id, $value)
  310. {
  311. $this->values[$id] = $value;
  312. }
  313. /**
  314. * {@inheritdoc}
  315. */
  316. public function offsetGet($id)
  317. {
  318. if (!array_key_exists($id, $this->values)) {
  319. throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
  320. }
  321. return $this->values[$id] instanceof \Closure ? $this->values[$id]($this) : $this->values[$id];
  322. }
  323. /**
  324. * {@inheritdoc}
  325. */
  326. public function offsetExists($id)
  327. {
  328. return isset($this->values[$id]);
  329. }
  330. /**
  331. * {@inheritdoc}
  332. */
  333. public function offsetUnset($id)
  334. {
  335. unset($this->values[$id]);
  336. }
  337. }