PageRenderTime 20ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/monica/monica/vendor/zendframework/zendframework/library/Zend/Log/Writer/AbstractWriter.php

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 334 lines | 175 code | 36 blank | 123 comment | 29 complexity | 4f7cd04e831a1423b6829f0802431e2a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Log\Writer;
  10. use Traversable;
  11. use Zend\Log\Exception;
  12. use Zend\Log\Filter;
  13. use Zend\Log\Formatter;
  14. use Zend\Stdlib\ErrorHandler;
  15. abstract class AbstractWriter implements WriterInterface
  16. {
  17. /**
  18. * Filter plugins
  19. *
  20. * @var FilterPluginManager
  21. */
  22. protected $filterPlugins;
  23. /**
  24. * Formatter plugins
  25. *
  26. * @var FormatterPluginManager
  27. */
  28. protected $formatterPlugins;
  29. /**
  30. * Filter chain
  31. *
  32. * @var Filter\FilterInterface[]
  33. */
  34. protected $filters = array();
  35. /**
  36. * Formats the log message before writing
  37. *
  38. * @var Formatter
  39. */
  40. protected $formatter;
  41. /**
  42. * Use Zend\Stdlib\ErrorHandler to report errors during calls to write
  43. *
  44. * @var bool
  45. */
  46. protected $convertWriteErrorsToExceptions = true;
  47. /**
  48. * Error level passed to Zend\Stdlib\ErrorHandler::start for errors reported during calls to write
  49. *
  50. * @var bool
  51. */
  52. protected $errorsToExceptionsConversionLevel = E_WARNING;
  53. /**
  54. * Constructor
  55. *
  56. * Set options for an writer. Accepted options are:
  57. * - filters: array of filters to add to this filter
  58. * - formatter: formatter for this writer
  59. *
  60. * @param array|\Traversable $options
  61. * @return Logger
  62. * @throws Exception\InvalidArgumentException
  63. */
  64. public function __construct($options = null)
  65. {
  66. if ($options instanceof Traversable) {
  67. $options = iterator_to_array($options);
  68. }
  69. if (is_array($options)) {
  70. if(isset($options['filters'])) {
  71. $filters = $options['filters'];
  72. if(is_string($filters) || $filters instanceof Filter\FilterInterface) {
  73. $this->addFilter($filters);
  74. } elseif(is_array($filters)) {
  75. foreach($filters as $filter) {
  76. if(is_string($filter) || $filter instanceof Filter\FilterInterface) {
  77. $this->addFilter($filter);
  78. } elseif(is_array($filter)) {
  79. if(!isset($filter['name'])) {
  80. throw new Exception\InvalidArgumentException('Options must contain a name for the filter');
  81. }
  82. $filterOptions = (isset($filter['options'])) ? $filter['options'] : null;
  83. $this->addFilter($filter['name'], $filterOptions);
  84. }
  85. }
  86. }
  87. }
  88. if(isset($options['formatter'])) {
  89. $formatter = $options['formatter'];
  90. if(is_string($formatter) || $formatter instanceof Formatter\FormatterInterface) {
  91. $this->setFormatter($formatter);
  92. } elseif(is_array($formatter)) {
  93. if(!isset($formatter['name'])) {
  94. throw new Exception\InvalidArgumentException('Options must contain a name for the formatter');
  95. }
  96. $formatterOptions = (isset($formatter['options'])) ? $formatter['options'] : null;
  97. $this->setFormatter($formatter['name'], $formatterOptions);
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Add a filter specific to this writer.
  104. *
  105. * @param int|string|Filter\FilterInterface $filter
  106. * @param array|null $options
  107. * @return AbstractWriter
  108. * @throws Exception\InvalidArgumentException
  109. */
  110. public function addFilter($filter, array $options = null)
  111. {
  112. if (is_int($filter)) {
  113. $filter = new Filter\Priority($filter);
  114. }
  115. if (is_string($filter)) {
  116. $filter = $this->filterPlugin($filter, $options);
  117. }
  118. if (!$filter instanceof Filter\FilterInterface) {
  119. throw new Exception\InvalidArgumentException(sprintf(
  120. 'Writer must implement %s\Filter\FilterInterface; received "%s"',
  121. __NAMESPACE__,
  122. is_object($filter) ? get_class($filter) : gettype($filter)
  123. ));
  124. }
  125. $this->filters[] = $filter;
  126. return $this;
  127. }
  128. /**
  129. * Get filter plugin manager
  130. *
  131. * @return FilterPluginManager
  132. */
  133. public function getFilterPluginManager()
  134. {
  135. if (null === $this->filterPlugins) {
  136. $this->setFilterPluginManager(new FilterPluginManager());
  137. }
  138. return $this->filterPlugins;
  139. }
  140. /**
  141. * Set filter plugin manager
  142. *
  143. * @param string|FilterPluginManager $plugins
  144. * @return self
  145. * @throws Exception\InvalidArgumentException
  146. */
  147. public function setFilterPluginManager($plugins)
  148. {
  149. if (is_string($plugins)) {
  150. $plugins = new $plugins;
  151. }
  152. if (!$plugins instanceof FilterPluginManager) {
  153. throw new Exception\InvalidArgumentException(sprintf(
  154. 'Writer plugin manager must extend %s\FilterPluginManager; received %s',
  155. __NAMESPACE__,
  156. is_object($plugins) ? get_class($plugins) : gettype($plugins)
  157. ));
  158. }
  159. $this->filterPlugins = $plugins;
  160. return $this;
  161. }
  162. /**
  163. * Get filter instance
  164. *
  165. * @param string $name
  166. * @param array|null $options
  167. * @return Filter\FilterInterface
  168. */
  169. public function filterPlugin($name, array $options = null)
  170. {
  171. return $this->getFilterPluginManager()->get($name, $options);
  172. }
  173. /**
  174. * Get formatter plugin manager
  175. *
  176. * @return FormatterPluginManager
  177. */
  178. public function getFormatterPluginManager()
  179. {
  180. if (null === $this->formatterPlugins) {
  181. $this->setFormatterPluginManager(new FormatterPluginManager());
  182. }
  183. return $this->formatterPlugins;
  184. }
  185. /**
  186. * Set formatter plugin manager
  187. *
  188. * @param string|FormatterPluginManager $plugins
  189. * @return self
  190. * @throws Exception\InvalidArgumentException
  191. */
  192. public function setFormatterPluginManager($plugins)
  193. {
  194. if (is_string($plugins)) {
  195. $plugins = new $plugins;
  196. }
  197. if (!$plugins instanceof FormatterPluginManager) {
  198. throw new Exception\InvalidArgumentException(sprintf(
  199. 'Writer plugin manager must extend %s\FormatterPluginManager; received %s',
  200. __NAMESPACE__,
  201. is_object($plugins) ? get_class($plugins) : gettype($plugins)
  202. ));
  203. }
  204. $this->formatterPlugins = $plugins;
  205. return $this;
  206. }
  207. /**
  208. * Get formatter instance
  209. *
  210. * @param string $name
  211. * @param array|null $options
  212. * @return Formatter\FormatterInterface
  213. */
  214. public function formatterPlugin($name, array $options = null)
  215. {
  216. return $this->getFormatterPluginManager()->get($name, $options);
  217. }
  218. /**
  219. * Log a message to this writer.
  220. *
  221. * @param array $event log data event
  222. * @return void
  223. */
  224. public function write(array $event)
  225. {
  226. foreach ($this->filters as $filter) {
  227. if (!$filter->filter($event)) {
  228. return;
  229. }
  230. }
  231. $errorHandlerStarted = false;
  232. if ($this->convertWriteErrorsToExceptions && !ErrorHandler::started()) {
  233. ErrorHandler::start($this->errorsToExceptionsConversionLevel);
  234. $errorHandlerStarted = true;
  235. }
  236. try {
  237. $this->doWrite($event);
  238. } catch (\Exception $e) {
  239. if ($errorHandlerStarted) {
  240. ErrorHandler::stop();
  241. $errorHandlerStarted = false;
  242. }
  243. throw $e;
  244. }
  245. if ($errorHandlerStarted) {
  246. $error = ErrorHandler::stop();
  247. $errorHandlerStarted = false;
  248. if ($error) {
  249. throw new Exception\RuntimeException("Unable to write", 0, $error);
  250. }
  251. }
  252. }
  253. /**
  254. * Set a new formatter for this writer
  255. *
  256. * @param string|Formatter\FormatterInterface $formatter
  257. * @return self
  258. * @throws Exception\InvalidArgumentException
  259. */
  260. public function setFormatter($formatter, array $options = null)
  261. {
  262. if (is_string($formatter)) {
  263. $formatter = $this->formatterPlugin($formatter, $options);
  264. }
  265. if (!$formatter instanceof Formatter\FormatterInterface) {
  266. throw new Exception\InvalidArgumentException(sprintf(
  267. 'Formatter must implement %s\Formatter\FormatterInterface; received "%s"',
  268. __NAMESPACE__,
  269. is_object($formatter) ? get_class($formatter) : gettype($formatter)
  270. ));
  271. }
  272. $this->formatter = $formatter;
  273. return $this;
  274. }
  275. /**
  276. * Set convert write errors to exception flag
  277. *
  278. * @param bool $ignoreWriteErrors
  279. */
  280. public function setConvertWriteErrorsToExceptions($convertErrors)
  281. {
  282. $this->convertWriteErrorsToExceptions = $convertErrors;
  283. }
  284. /**
  285. * Perform shutdown activities such as closing open resources
  286. *
  287. * @return void
  288. */
  289. public function shutdown()
  290. {}
  291. /**
  292. * Write a message to the log
  293. *
  294. * @param array $event log data event
  295. * @return void
  296. */
  297. abstract protected function doWrite(array $event);
  298. }