PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zend-log/src/Writer/AbstractWriter.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 353 lines | 184 code | 36 blank | 133 comment | 31 complexity | 72d260b94de14dcb983e43d365f98d03 MD5 | raw file
  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-2015 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\FormatterInterface
  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 a 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. * @throws Exception\InvalidArgumentException
  62. */
  63. public function __construct($options = null)
  64. {
  65. if ($options instanceof Traversable) {
  66. $options = iterator_to_array($options);
  67. }
  68. if (is_array($options)) {
  69. if (isset($options['filters'])) {
  70. $filters = $options['filters'];
  71. if (is_int($filters) || is_string($filters) || $filters instanceof Filter\FilterInterface) {
  72. $this->addFilter($filters);
  73. } elseif (is_array($filters)) {
  74. foreach ($filters as $filter) {
  75. if (is_int($filter) || is_string($filter) || $filter instanceof Filter\FilterInterface) {
  76. $this->addFilter($filter);
  77. } elseif (is_array($filter)) {
  78. if (!isset($filter['name'])) {
  79. throw new Exception\InvalidArgumentException('Options must contain a name for the filter');
  80. }
  81. $filterOptions = (isset($filter['options'])) ? $filter['options'] : null;
  82. $this->addFilter($filter['name'], $filterOptions);
  83. }
  84. }
  85. }
  86. }
  87. if (isset($options['formatter'])) {
  88. $formatter = $options['formatter'];
  89. if (is_string($formatter) || $formatter instanceof Formatter\FormatterInterface) {
  90. $this->setFormatter($formatter);
  91. } elseif (is_array($formatter)) {
  92. if (!isset($formatter['name'])) {
  93. throw new Exception\InvalidArgumentException('Options must contain a name for the formatter');
  94. }
  95. $formatterOptions = (isset($formatter['options'])) ? $formatter['options'] : null;
  96. $this->setFormatter($formatter['name'], $formatterOptions);
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * Add a filter specific to this writer.
  103. *
  104. * @param int|string|Filter\FilterInterface $filter
  105. * @param array|null $options
  106. * @return AbstractWriter
  107. * @throws Exception\InvalidArgumentException
  108. */
  109. public function addFilter($filter, array $options = null)
  110. {
  111. if (is_int($filter)) {
  112. $filter = new Filter\Priority($filter);
  113. }
  114. if (is_string($filter)) {
  115. $filter = $this->filterPlugin($filter, $options);
  116. }
  117. if (!$filter instanceof Filter\FilterInterface) {
  118. throw new Exception\InvalidArgumentException(sprintf(
  119. 'Filter must implement %s\Filter\FilterInterface; received "%s"',
  120. __NAMESPACE__,
  121. is_object($filter) ? get_class($filter) : gettype($filter)
  122. ));
  123. }
  124. $this->filters[] = $filter;
  125. return $this;
  126. }
  127. /**
  128. * Get filter plugin manager
  129. *
  130. * @return FilterPluginManager
  131. */
  132. public function getFilterPluginManager()
  133. {
  134. if (null === $this->filterPlugins) {
  135. $this->setFilterPluginManager(new FilterPluginManager());
  136. }
  137. return $this->filterPlugins;
  138. }
  139. /**
  140. * Set filter plugin manager
  141. *
  142. * @param string|FilterPluginManager $plugins
  143. * @return self
  144. * @throws Exception\InvalidArgumentException
  145. */
  146. public function setFilterPluginManager($plugins)
  147. {
  148. if (is_string($plugins)) {
  149. $plugins = new $plugins;
  150. }
  151. if (!$plugins instanceof FilterPluginManager) {
  152. throw new Exception\InvalidArgumentException(sprintf(
  153. 'Writer plugin manager must extend %s\FilterPluginManager; received %s',
  154. __NAMESPACE__,
  155. is_object($plugins) ? get_class($plugins) : gettype($plugins)
  156. ));
  157. }
  158. $this->filterPlugins = $plugins;
  159. return $this;
  160. }
  161. /**
  162. * Get filter instance
  163. *
  164. * @param string $name
  165. * @param array|null $options
  166. * @return Filter\FilterInterface
  167. */
  168. public function filterPlugin($name, array $options = null)
  169. {
  170. return $this->getFilterPluginManager()->get($name, $options);
  171. }
  172. /**
  173. * Get formatter plugin manager
  174. *
  175. * @return FormatterPluginManager
  176. */
  177. public function getFormatterPluginManager()
  178. {
  179. if (null === $this->formatterPlugins) {
  180. $this->setFormatterPluginManager(new FormatterPluginManager());
  181. }
  182. return $this->formatterPlugins;
  183. }
  184. /**
  185. * Set formatter plugin manager
  186. *
  187. * @param string|FormatterPluginManager $plugins
  188. * @return self
  189. * @throws Exception\InvalidArgumentException
  190. */
  191. public function setFormatterPluginManager($plugins)
  192. {
  193. if (is_string($plugins)) {
  194. $plugins = new $plugins;
  195. }
  196. if (!$plugins instanceof FormatterPluginManager) {
  197. throw new Exception\InvalidArgumentException(
  198. 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. }
  205. $this->formatterPlugins = $plugins;
  206. return $this;
  207. }
  208. /**
  209. * Get formatter instance
  210. *
  211. * @param string $name
  212. * @param array|null $options
  213. * @return Formatter\FormatterInterface
  214. */
  215. public function formatterPlugin($name, array $options = null)
  216. {
  217. return $this->getFormatterPluginManager()->get($name, $options);
  218. }
  219. /**
  220. * Log a message to this writer.
  221. *
  222. * @param array $event log data event
  223. * @return void
  224. */
  225. public function write(array $event)
  226. {
  227. foreach ($this->filters as $filter) {
  228. if (!$filter->filter($event)) {
  229. return;
  230. }
  231. }
  232. $errorHandlerStarted = false;
  233. if ($this->convertWriteErrorsToExceptions && !ErrorHandler::started()) {
  234. ErrorHandler::start($this->errorsToExceptionsConversionLevel);
  235. $errorHandlerStarted = true;
  236. }
  237. try {
  238. $this->doWrite($event);
  239. } catch (\Exception $e) {
  240. if ($errorHandlerStarted) {
  241. ErrorHandler::stop();
  242. }
  243. throw $e;
  244. }
  245. if ($errorHandlerStarted) {
  246. $error = ErrorHandler::stop();
  247. if ($error) {
  248. throw new Exception\RuntimeException("Unable to write", 0, $error);
  249. }
  250. }
  251. }
  252. /**
  253. * Set a new formatter for this writer
  254. *
  255. * @param string|Formatter\FormatterInterface $formatter
  256. * @param array|null $options
  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. * Get formatter
  277. *
  278. * @return Formatter\FormatterInterface
  279. */
  280. protected function getFormatter()
  281. {
  282. return $this->formatter;
  283. }
  284. /**
  285. * Check if the writer has a formatter
  286. *
  287. * @return bool
  288. */
  289. protected function hasFormatter()
  290. {
  291. return $this->formatter instanceof Formatter\FormatterInterface;
  292. }
  293. /**
  294. * Set convert write errors to exception flag
  295. *
  296. * @param bool $convertErrors
  297. */
  298. public function setConvertWriteErrorsToExceptions($convertErrors)
  299. {
  300. $this->convertWriteErrorsToExceptions = $convertErrors;
  301. }
  302. /**
  303. * Perform shutdown activities such as closing open resources
  304. *
  305. * @return void
  306. */
  307. public function shutdown()
  308. {
  309. }
  310. /**
  311. * Write a message to the log
  312. *
  313. * @param array $event log data event
  314. * @return void
  315. */
  316. abstract protected function doWrite(array $event);
  317. }