Pelzini

This is the code documentation for the Pelzini project

source of /processor/config.php

Contains the Config class
  1. <?php
  2. /*
  3. Copyright 2015 Josh Heidenreich
  4.  
  5. This file is part of Pelzini.
  6.  
  7. Pelzini is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. Pelzini is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Pelzini. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21.  
  22. /**
  23.  * Contains the {@link Config} class
  24.  *
  25.  * @package Processor
  26.  * @author Josh Heidenreich
  27.  * @since 0.4
  28.  **/
  29.  
  30. /**
  31.  * Stores the processor configuration
  32.  **/
  33. class Config {
  34. protected $project_name;
  35. protected $project_code;
  36. protected $license_text;
  37. protected $transformers = array();
  38. protected $outputters = array();
  39. protected $base_directory;
  40. protected $exclude_directories = array();
  41. protected $docs_directory;
  42.  
  43.  
  44. /**
  45.   * Load and validate a config file
  46.   *
  47.   * @param string $filename The filename to load
  48.   * @return bool True on success, false on failure
  49.   **/
  50. public function load($filename) {
  51. $dpgOutputters = array();
  52. $dpgTransformers = array();
  53. $dpgLanguages = array('php', 'js');
  54.  
  55. require $filename;
  56.  
  57. if (!isset($dpgProjectName)) {
  58. echo "ERROR:\nRequired config option '\$dpgProjectName' not set.\n";
  59. return false;
  60. }
  61.  
  62. if (!isset($dpgProjectCode)) {
  63. echo "ERROR:\nRequired config option '\$dpgProjectCode' not set.\n";
  64. return false;
  65. }
  66.  
  67. // Don't allow chars which will mess up the friendly urls.
  68. if (preg_match('/[^-_A-Za-z0-9]/', $dpgProjectCode)) {
  69. echo "ERROR:\nInvalid characters for '\$dpgProjectCode' specified.\n";
  70. echo "Valid characters are A-Z, a-z, 0-9, dash and underscore.\n";
  71. return false;
  72. }
  73.  
  74. if (@count($dpgOutputters) == 0) {
  75. if (file_exists(__DIR__ . '/../database.config.php')) {
  76. $result = $this->loadSharedDatabaseConfig();
  77. if ($result === null) {
  78. echo "ERROR:\nConfig file 'database.config.php' was found but could not be parsed.\n";
  79. return false;
  80. } else {
  81. $dpgOutputters[] = $result;
  82. }
  83. } else {
  84. echo "ERROR:\nRequired config option '\$dpgOutputters' not set and 'database.config.php' file not found.\n";
  85. return false;
  86. }
  87. }
  88.  
  89. if (!$dpgBaseDirectory or !file_exists($dpgBaseDirectory)) {
  90. echo "ERROR:\nRequired config option '\$dpgBaseDirectory' not set or directory does not exist.\n";
  91. return false;
  92. }
  93.  
  94. $this->project_name = $dpgProjectName;
  95. $this->project_code = $dpgProjectCode;
  96. $this->license_text = $dpgLicenseText;
  97. $this->transformers = $dpgTransformers;
  98. $this->outputters = $dpgOutputters;
  99. $this->base_directory = $dpgBaseDirectory;
  100. $this->exclude_directories = $dpgExcludeDirectories;
  101. $this->docs_directory = $dpgDocsDirectory;
  102. $this->languages = $dpgLanguages;
  103.  
  104. return true;
  105. }
  106.  
  107.  
  108. /**
  109.   * If no outputters have been specified, load the common "database.config.php" file
  110.   * and use the settings in there to create one
  111.   *
  112.   * @return Outputter If a outputter could be parsed from the configuration
  113.   * @return null On error
  114.   */
  115. private function loadSharedDatabaseConfig() {
  116. require __DIR__ . '/../database.config.php';
  117.  
  118. switch ($dvgDatabaseEngine) {
  119. case 'mysql':
  120. return new MysqlOutputter(
  121. $dvgDatabaseSettings['username'],
  122. $dvgDatabaseSettings['password'],
  123. $dvgDatabaseSettings['server'],
  124. $dvgDatabaseSettings['name']
  125. );
  126.  
  127. case 'postgresql':
  128. return new PostgresqlOutputter(
  129. $dvgDatabaseSettings['username'],
  130. $dvgDatabaseSettings['password'],
  131. $dvgDatabaseSettings['server'],
  132. $dvgDatabaseSettings['name']
  133. );
  134.  
  135. case 'sqlite':
  136. return new SqliteOutputter(
  137. $dvgDatabaseSettings['filename']
  138. );
  139.  
  140. default:
  141. return null;
  142. }
  143. }
  144.  
  145.  
  146. /**
  147.   * @return string The name of the project
  148.   **/
  149. public function getProjectName() {
  150. return $this->project_name;
  151. }
  152.  
  153.  
  154. /**
  155.   * @return string The project code, to allow for multiple projects per db
  156.   **/
  157. public function getProjectCode() {
  158. return $this->project_code;
  159. }
  160.  
  161.  
  162. /**
  163.   * @return string The license text to show in the viewer
  164.   **/
  165. public function getLicenseText() {
  166. return $this->license_text;
  167. }
  168.  
  169.  
  170. /**
  171.   * @return array Transformer classes
  172.   **/
  173. public function getTransformers() {
  174. return $this->transformers;
  175. }
  176.  
  177.  
  178. /**
  179.   * @return array Outputter classes
  180.   **/
  181. public function getOutputters() {
  182. return $this->outputters;
  183. }
  184.  
  185.  
  186. /**
  187.   * @return string The base directory for indexing
  188.   **/
  189. public function getBaseDirectory() {
  190. return $this->base_directory;
  191. }
  192.  
  193.  
  194. /**
  195.   * @return string The base directory for indexing
  196.   **/
  197. public function getExcludeDirectories() {
  198. return $this->exclude_directories;
  199. }
  200.  
  201.  
  202. /**
  203.   * @return string The directory containing documentation
  204.   **/
  205. public function getDocsDirectory() {
  206. return $this->docs_directory;
  207. }
  208.  
  209. }
  210.