Php.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\g11n\catalog\adapter;
  9. use lithium\core\ConfigException;
  10. /**
  11. * The `Php` class is an adapter for reading from PHP files which hold g11n data
  12. * in form of arrays.
  13. *
  14. * An example PHP file must contain a `return` statement which returns an array if the
  15. * the file is included.
  16. *
  17. * {{{
  18. * <?php
  19. * return array(
  20. * 'postalCode' => '\d+',
  21. * 'phone' => '\d+\-\d+'
  22. * );
  23. * ?>
  24. * }}}
  25. *
  26. * The adapter works with a directory structure below. The example shows the structure
  27. * for the directory as given by the `'path'` configuration setting. It is similar to
  28. * the one used by the the `Gettext` adapter.
  29. *
  30. * {{{
  31. * resources/g11n/php
  32. * ├── <locale>
  33. * | ├── message
  34. * | | ├── default.php
  35. * | | └── <scope>.php
  36. * | ├── validation
  37. * | | └── ...
  38. * | └── ...
  39. * ├── <locale>
  40. * | └── ...
  41. * ├── message_default.php
  42. * ├── message_<scope>.php
  43. * ├── validation_default.php
  44. * ├── validation_<scope>.php
  45. * └── ...
  46. * }}}
  47. *
  48. * @see lithium\g11n\catalog\adapter\Gettext
  49. */
  50. class Php extends \lithium\g11n\catalog\Adapter {
  51. /**
  52. * Constructor.
  53. *
  54. * @param array $config Available configuration options are:
  55. * - `'path'`: The path to the directory holding the data.
  56. */
  57. public function __construct(array $config = array()) {
  58. $defaults = array('path' => null);
  59. parent::__construct($config + $defaults);
  60. }
  61. /**
  62. * Initializer. Checks if the configured path exists.
  63. *
  64. * @return void
  65. * @throws \Exception
  66. */
  67. protected function _init() {
  68. parent::_init();
  69. if (!is_dir($this->_config['path'])) {
  70. $message = "Php directory does not exist at path `{$this->_config['path']}`.";
  71. throw new ConfigException($message);
  72. }
  73. }
  74. /**
  75. * Reads data.
  76. *
  77. * @param string $category A category.
  78. * @param string $locale A locale identifier.
  79. * @param string $scope The scope for the current operation.
  80. * @return array
  81. */
  82. public function read($category, $locale, $scope) {
  83. $path = $this->_config['path'];
  84. $file = $this->_file($category, $locale, $scope);
  85. $data = array();
  86. if (file_exists($file)) {
  87. foreach (require $file as $id => $translated) {
  88. $data = $this->_merge($data, compact('id', 'translated'));
  89. }
  90. }
  91. return $data;
  92. }
  93. /**
  94. * Helper method for transforming a category, locale and scope into a filename.
  95. *
  96. * @param string $category Category name.
  97. * @param string $locale Locale identifier.
  98. * @param string $scope Current operation scope.
  99. * @return string Filename.
  100. */
  101. protected function _file($category, $locale, $scope) {
  102. $path = $this->_config['path'];
  103. $scope = $scope ?: 'default';
  104. if (($pos = strpos($category, 'Template')) !== false) {
  105. $category = substr($category, 0, $pos);
  106. return "{$path}/{$category}_{$scope}.php";
  107. }
  108. return "{$path}/{$locale}/{$category}/{$scope}.php";
  109. }
  110. }
  111. ?>