/classes/evidev/moodle/plugins/sanitychecker/SanityCheckerProvider.php

https://bitbucket.org/eviweb/moodle-local_sanitychecker · PHP · 154 lines · 58 code · 13 blank · 83 comment · 2 complexity · c916b09ad56742ce34abd23a45b605be MD5 · raw file

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * The MIT License
  5. *
  6. * Copyright 2013 Eric VILLARD <dev@eviweb.fr>.
  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 deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * 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 FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. * @package local_sanitychecker
  27. * @author Eric VILLARD <dev@eviweb.fr>
  28. * @copyright (c) 2013 Eric VILLARD <dev@eviweb.fr>
  29. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  30. */
  31. namespace evidev\moodle\plugins\sanitychecker;
  32. /**
  33. * Sanity checker provider
  34. *
  35. * @package local_sanitychecker
  36. * @author Eric VILLARD <dev@eviweb.fr>
  37. * @copyright (c) 2013 Eric VILLARD <dev@eviweb.fr>
  38. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  39. */
  40. final class SanityCheckerProvider
  41. {
  42. /**
  43. * moodle global objects collection
  44. *
  45. * @var array
  46. */
  47. private $globals;
  48. /**
  49. * path where to find sanity related services
  50. *
  51. * @var string
  52. */
  53. private $servicepath;
  54. /**
  55. * collection of \evidev\moodle\plugins\SanityChecker implementations
  56. *
  57. * @var \ArrayObject
  58. */
  59. private $services;
  60. /**
  61. * constructor
  62. *
  63. * @param \moodle_database $db instance of the global database object
  64. */
  65. private function __construct(\moodle_database $db)
  66. {
  67. // define the service path
  68. $this->servicepath = str_replace(
  69. str_replace('\\', DIRECTORY_SEPARATOR, __NAMESPACE__),
  70. '',
  71. __DIR__
  72. );
  73. $this->servicepath.= 'META-INF'.DIRECTORY_SEPARATOR.
  74. 'services'.DIRECTORY_SEPARATOR.
  75. 'evidev.moodle.plugins.sanitychecker';
  76. // create the collection of moodle global objects
  77. $this->globals = array(
  78. 'db' => $db
  79. );
  80. $this->loadService();
  81. }
  82. /**
  83. * factory method
  84. *
  85. * @param \moodle_database $db instance of the global database object
  86. * @return \evidev\moodle\plugins\sanitychecker\SanityCheckerProvider
  87. */
  88. public static function create(\moodle_database $db)
  89. {
  90. return new static($db);
  91. }
  92. /**
  93. * get an iterator of services
  94. *
  95. * @return \ArrayIterator
  96. */
  97. public function iterator()
  98. {
  99. return $this->services->getIterator();
  100. }
  101. /**
  102. * load \evidev\moodle\plugins\SanityChecker implementations
  103. */
  104. private function loadService()
  105. {
  106. $this->services = new \ArrayObject(array());
  107. $it = new \ArrayIterator($this->parseFile($this->servicepath));
  108. while ($it->valid()) {
  109. $class = $it->current();
  110. $impl = $class::create();
  111. if ($impl instanceof DatabaseSanityChecker) {
  112. $impl->setDBObject($this->globals['db']);
  113. }
  114. $this->services->append($impl);
  115. $it->next();
  116. }
  117. }
  118. /**
  119. * parses the service file
  120. *
  121. * @param string $file file to parse
  122. * @return array returns the list of service implementations
  123. */
  124. private function parseFile($file)
  125. {
  126. $content = preg_replace(
  127. // removes comments
  128. '/[\s]*\#[^\n\r]+/',
  129. '',
  130. preg_replace(
  131. // removes blank lines
  132. '/^[\s\r\n]+$/',
  133. '',
  134. // gets file content
  135. file_get_contents($file)
  136. )
  137. );
  138. // splits lines
  139. return preg_split('/[\n\r]+/', $content, null, PREG_SPLIT_NO_EMPTY);
  140. }
  141. }