PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/app/cache/dev/classes.php

https://github.com/silvanei/treinaweb-symfony2-basico
PHP | 6458 lines | 6458 code | 0 blank | 0 comment | 781 complexity | 460ab38af96583ae9f2c44d2ac335ce0 MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. namespace Monolog\Formatter
  3. {
  4. interface FormatterInterface
  5. {
  6. public function format(array $record);
  7. public function formatBatch(array $records);
  8. }
  9. }
  10. namespace Monolog\Formatter
  11. {
  12. use Exception;
  13. class NormalizerFormatter implements FormatterInterface
  14. {
  15. const SIMPLE_DATE ="Y-m-d H:i:s";
  16. protected $dateFormat;
  17. public function __construct($dateFormat = null)
  18. {
  19. $this->dateFormat = $dateFormat ?: static::SIMPLE_DATE;
  20. }
  21. public function format(array $record)
  22. {
  23. return $this->normalize($record);
  24. }
  25. public function formatBatch(array $records)
  26. {
  27. foreach ($records as $key => $record) {
  28. $records[$key] = $this->format($record);
  29. }
  30. return $records;
  31. }
  32. protected function normalize($data)
  33. {
  34. if (null === $data || is_scalar($data)) {
  35. return $data;
  36. }
  37. if (is_array($data) || $data instanceof \Traversable) {
  38. $normalized = array();
  39. $count = 1;
  40. foreach ($data as $key => $value) {
  41. if ($count++ >= 1000) {
  42. $normalized['...'] ='Over 1000 items, aborting normalization';
  43. break;
  44. }
  45. $normalized[$key] = $this->normalize($value);
  46. }
  47. return $normalized;
  48. }
  49. if ($data instanceof \DateTime) {
  50. return $data->format($this->dateFormat);
  51. }
  52. if (is_object($data)) {
  53. if ($data instanceof Exception) {
  54. return $this->normalizeException($data);
  55. }
  56. return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data, true));
  57. }
  58. if (is_resource($data)) {
  59. return'[resource]';
  60. }
  61. return'[unknown('.gettype($data).')]';
  62. }
  63. protected function normalizeException(Exception $e)
  64. {
  65. $data = array('class'=> get_class($e),'message'=> $e->getMessage(),'file'=> $e->getFile().':'.$e->getLine(),
  66. );
  67. $trace = $e->getTrace();
  68. foreach ($trace as $frame) {
  69. if (isset($frame['file'])) {
  70. $data['trace'][] = $frame['file'].':'.$frame['line'];
  71. } else {
  72. $data['trace'][] = json_encode($frame);
  73. }
  74. }
  75. if ($previous = $e->getPrevious()) {
  76. $data['previous'] = $this->normalizeException($previous);
  77. }
  78. return $data;
  79. }
  80. protected function toJson($data, $ignoreErrors = false)
  81. {
  82. if ($ignoreErrors) {
  83. if (version_compare(PHP_VERSION,'5.4.0','>=')) {
  84. return @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  85. }
  86. return @json_encode($data);
  87. }
  88. if (version_compare(PHP_VERSION,'5.4.0','>=')) {
  89. return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  90. }
  91. return json_encode($data);
  92. }
  93. }
  94. }
  95. namespace Monolog\Formatter
  96. {
  97. use Exception;
  98. class LineFormatter extends NormalizerFormatter
  99. {
  100. const SIMPLE_FORMAT ="[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
  101. protected $format;
  102. protected $allowInlineLineBreaks;
  103. public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false)
  104. {
  105. $this->format = $format ?: static::SIMPLE_FORMAT;
  106. $this->allowInlineLineBreaks = $allowInlineLineBreaks;
  107. parent::__construct($dateFormat);
  108. }
  109. public function format(array $record)
  110. {
  111. $vars = parent::format($record);
  112. $output = $this->format;
  113. foreach ($vars['extra'] as $var => $val) {
  114. if (false !== strpos($output,'%extra.'.$var.'%')) {
  115. $output = str_replace('%extra.'.$var.'%', $this->replaceNewlines($this->convertToString($val)), $output);
  116. unset($vars['extra'][$var]);
  117. }
  118. }
  119. foreach ($vars as $var => $val) {
  120. if (false !== strpos($output,'%'.$var.'%')) {
  121. $output = str_replace('%'.$var.'%', $this->replaceNewlines($this->convertToString($val)), $output);
  122. }
  123. }
  124. return $output;
  125. }
  126. public function formatBatch(array $records)
  127. {
  128. $message ='';
  129. foreach ($records as $record) {
  130. $message .= $this->format($record);
  131. }
  132. return $message;
  133. }
  134. protected function normalizeException(Exception $e)
  135. {
  136. $previousText ='';
  137. if ($previous = $e->getPrevious()) {
  138. do {
  139. $previousText .=', '.get_class($previous).': '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine();
  140. } while ($previous = $previous->getPrevious());
  141. }
  142. return'[object] ('.get_class($e).': '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')';
  143. }
  144. protected function convertToString($data)
  145. {
  146. if (null === $data || is_bool($data)) {
  147. return var_export($data, true);
  148. }
  149. if (is_scalar($data)) {
  150. return (string) $data;
  151. }
  152. if (version_compare(PHP_VERSION,'5.4.0','>=')) {
  153. return $this->toJson($data, true);
  154. }
  155. return str_replace('\\/','/', @json_encode($data));
  156. }
  157. protected function replaceNewlines($str)
  158. {
  159. if ($this->allowInlineLineBreaks) {
  160. return $str;
  161. }
  162. return preg_replace('{[\r\n]+}',' ', $str);
  163. }
  164. }
  165. }
  166. namespace Monolog\Handler
  167. {
  168. use Monolog\Formatter\FormatterInterface;
  169. interface HandlerInterface
  170. {
  171. public function isHandling(array $record);
  172. public function handle(array $record);
  173. public function handleBatch(array $records);
  174. public function pushProcessor($callback);
  175. public function popProcessor();
  176. public function setFormatter(FormatterInterface $formatter);
  177. public function getFormatter();
  178. }
  179. }
  180. namespace Monolog\Handler
  181. {
  182. use Monolog\Logger;
  183. use Monolog\Formatter\FormatterInterface;
  184. use Monolog\Formatter\LineFormatter;
  185. abstract class AbstractHandler implements HandlerInterface
  186. {
  187. protected $level = Logger::DEBUG;
  188. protected $bubble = true;
  189. protected $formatter;
  190. protected $processors = array();
  191. public function __construct($level = Logger::DEBUG, $bubble = true)
  192. {
  193. $this->level = $level;
  194. $this->bubble = $bubble;
  195. }
  196. public function isHandling(array $record)
  197. {
  198. return $record['level'] >= $this->level;
  199. }
  200. public function handleBatch(array $records)
  201. {
  202. foreach ($records as $record) {
  203. $this->handle($record);
  204. }
  205. }
  206. public function close()
  207. {
  208. }
  209. public function pushProcessor($callback)
  210. {
  211. if (!is_callable($callback)) {
  212. throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
  213. }
  214. array_unshift($this->processors, $callback);
  215. return $this;
  216. }
  217. public function popProcessor()
  218. {
  219. if (!$this->processors) {
  220. throw new \LogicException('You tried to pop from an empty processor stack.');
  221. }
  222. return array_shift($this->processors);
  223. }
  224. public function setFormatter(FormatterInterface $formatter)
  225. {
  226. $this->formatter = $formatter;
  227. return $this;
  228. }
  229. public function getFormatter()
  230. {
  231. if (!$this->formatter) {
  232. $this->formatter = $this->getDefaultFormatter();
  233. }
  234. return $this->formatter;
  235. }
  236. public function setLevel($level)
  237. {
  238. $this->level = $level;
  239. return $this;
  240. }
  241. public function getLevel()
  242. {
  243. return $this->level;
  244. }
  245. public function setBubble($bubble)
  246. {
  247. $this->bubble = $bubble;
  248. return $this;
  249. }
  250. public function getBubble()
  251. {
  252. return $this->bubble;
  253. }
  254. public function __destruct()
  255. {
  256. try {
  257. $this->close();
  258. } catch (\Exception $e) {
  259. }
  260. }
  261. protected function getDefaultFormatter()
  262. {
  263. return new LineFormatter();
  264. }
  265. }
  266. }
  267. namespace Monolog\Handler
  268. {
  269. abstract class AbstractProcessingHandler extends AbstractHandler
  270. {
  271. public function handle(array $record)
  272. {
  273. if (!$this->isHandling($record)) {
  274. return false;
  275. }
  276. $record = $this->processRecord($record);
  277. $record['formatted'] = $this->getFormatter()->format($record);
  278. $this->write($record);
  279. return false === $this->bubble;
  280. }
  281. abstract protected function write(array $record);
  282. protected function processRecord(array $record)
  283. {
  284. if ($this->processors) {
  285. foreach ($this->processors as $processor) {
  286. $record = call_user_func($processor, $record);
  287. }
  288. }
  289. return $record;
  290. }
  291. }
  292. }
  293. namespace Monolog\Handler
  294. {
  295. use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
  296. use Monolog\Handler\FingersCrossed\ActivationStrategyInterface;
  297. use Monolog\Logger;
  298. class FingersCrossedHandler extends AbstractHandler
  299. {
  300. protected $handler;
  301. protected $activationStrategy;
  302. protected $buffering = true;
  303. protected $bufferSize;
  304. protected $buffer = array();
  305. protected $stopBuffering;
  306. public function __construct($handler, $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true)
  307. {
  308. if (null === $activationStrategy) {
  309. $activationStrategy = new ErrorLevelActivationStrategy(Logger::WARNING);
  310. }
  311. if (!$activationStrategy instanceof ActivationStrategyInterface) {
  312. $activationStrategy = new ErrorLevelActivationStrategy($activationStrategy);
  313. }
  314. $this->handler = $handler;
  315. $this->activationStrategy = $activationStrategy;
  316. $this->bufferSize = $bufferSize;
  317. $this->bubble = $bubble;
  318. $this->stopBuffering = $stopBuffering;
  319. }
  320. public function isHandling(array $record)
  321. {
  322. return true;
  323. }
  324. public function handle(array $record)
  325. {
  326. if ($this->processors) {
  327. foreach ($this->processors as $processor) {
  328. $record = call_user_func($processor, $record);
  329. }
  330. }
  331. if ($this->buffering) {
  332. $this->buffer[] = $record;
  333. if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) {
  334. array_shift($this->buffer);
  335. }
  336. if ($this->activationStrategy->isHandlerActivated($record)) {
  337. if ($this->stopBuffering) {
  338. $this->buffering = false;
  339. }
  340. if (!$this->handler instanceof HandlerInterface) {
  341. if (!is_callable($this->handler)) {
  342. throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
  343. }
  344. $this->handler = call_user_func($this->handler, $record, $this);
  345. if (!$this->handler instanceof HandlerInterface) {
  346. throw new \RuntimeException("The factory callable should return a HandlerInterface");
  347. }
  348. }
  349. $this->handler->handleBatch($this->buffer);
  350. $this->buffer = array();
  351. }
  352. } else {
  353. $this->handler->handle($record);
  354. }
  355. return false === $this->bubble;
  356. }
  357. public function reset()
  358. {
  359. $this->buffering = true;
  360. }
  361. }
  362. }
  363. namespace Monolog\Handler\FingersCrossed
  364. {
  365. interface ActivationStrategyInterface
  366. {
  367. public function isHandlerActivated(array $record);
  368. }
  369. }
  370. namespace Monolog\Handler\FingersCrossed
  371. {
  372. class ErrorLevelActivationStrategy implements ActivationStrategyInterface
  373. {
  374. private $actionLevel;
  375. public function __construct($actionLevel)
  376. {
  377. $this->actionLevel = $actionLevel;
  378. }
  379. public function isHandlerActivated(array $record)
  380. {
  381. return $record['level'] >= $this->actionLevel;
  382. }
  383. }
  384. }
  385. namespace Monolog\Handler
  386. {
  387. use Monolog\Logger;
  388. class StreamHandler extends AbstractProcessingHandler
  389. {
  390. protected $stream;
  391. protected $url;
  392. private $errorMessage;
  393. protected $filePermission;
  394. public function __construct($stream, $level = Logger::DEBUG, $bubble = true, $filePermission = null)
  395. {
  396. parent::__construct($level, $bubble);
  397. if (is_resource($stream)) {
  398. $this->stream = $stream;
  399. } else {
  400. $this->url = $stream;
  401. }
  402. $this->filePermission = $filePermission;
  403. }
  404. public function close()
  405. {
  406. if (is_resource($this->stream)) {
  407. fclose($this->stream);
  408. }
  409. $this->stream = null;
  410. }
  411. protected function write(array $record)
  412. {
  413. if (!is_resource($this->stream)) {
  414. if (!$this->url) {
  415. throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
  416. }
  417. $this->errorMessage = null;
  418. set_error_handler(array($this,'customErrorHandler'));
  419. $this->stream = fopen($this->url,'a');
  420. if ($this->filePermission !== null) {
  421. @chmod($this->url, $this->filePermission);
  422. }
  423. restore_error_handler();
  424. if (!is_resource($this->stream)) {
  425. $this->stream = null;
  426. throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url));
  427. }
  428. }
  429. fwrite($this->stream, (string) $record['formatted']);
  430. }
  431. private function customErrorHandler($code, $msg)
  432. {
  433. $this->errorMessage = preg_replace('{^fopen\(.*?\): }','', $msg);
  434. }
  435. }
  436. }
  437. namespace Monolog\Handler
  438. {
  439. use Monolog\Logger;
  440. class TestHandler extends AbstractProcessingHandler
  441. {
  442. protected $records = array();
  443. protected $recordsByLevel = array();
  444. public function getRecords()
  445. {
  446. return $this->records;
  447. }
  448. public function hasEmergency($record)
  449. {
  450. return $this->hasRecord($record, Logger::EMERGENCY);
  451. }
  452. public function hasAlert($record)
  453. {
  454. return $this->hasRecord($record, Logger::ALERT);
  455. }
  456. public function hasCritical($record)
  457. {
  458. return $this->hasRecord($record, Logger::CRITICAL);
  459. }
  460. public function hasError($record)
  461. {
  462. return $this->hasRecord($record, Logger::ERROR);
  463. }
  464. public function hasWarning($record)
  465. {
  466. return $this->hasRecord($record, Logger::WARNING);
  467. }
  468. public function hasNotice($record)
  469. {
  470. return $this->hasRecord($record, Logger::NOTICE);
  471. }
  472. public function hasInfo($record)
  473. {
  474. return $this->hasRecord($record, Logger::INFO);
  475. }
  476. public function hasDebug($record)
  477. {
  478. return $this->hasRecord($record, Logger::DEBUG);
  479. }
  480. public function hasEmergencyRecords()
  481. {
  482. return isset($this->recordsByLevel[Logger::EMERGENCY]);
  483. }
  484. public function hasAlertRecords()
  485. {
  486. return isset($this->recordsByLevel[Logger::ALERT]);
  487. }
  488. public function hasCriticalRecords()
  489. {
  490. return isset($this->recordsByLevel[Logger::CRITICAL]);
  491. }
  492. public function hasErrorRecords()
  493. {
  494. return isset($this->recordsByLevel[Logger::ERROR]);
  495. }
  496. public function hasWarningRecords()
  497. {
  498. return isset($this->recordsByLevel[Logger::WARNING]);
  499. }
  500. public function hasNoticeRecords()
  501. {
  502. return isset($this->recordsByLevel[Logger::NOTICE]);
  503. }
  504. public function hasInfoRecords()
  505. {
  506. return isset($this->recordsByLevel[Logger::INFO]);
  507. }
  508. public function hasDebugRecords()
  509. {
  510. return isset($this->recordsByLevel[Logger::DEBUG]);
  511. }
  512. protected function hasRecord($record, $level)
  513. {
  514. if (!isset($this->recordsByLevel[$level])) {
  515. return false;
  516. }
  517. if (is_array($record)) {
  518. $record = $record['message'];
  519. }
  520. foreach ($this->recordsByLevel[$level] as $rec) {
  521. if ($rec['message'] === $record) {
  522. return true;
  523. }
  524. }
  525. return false;
  526. }
  527. protected function write(array $record)
  528. {
  529. $this->recordsByLevel[$record['level']][] = $record;
  530. $this->records[] = $record;
  531. }
  532. }
  533. }
  534. namespace Psr\Log
  535. {
  536. interface LoggerInterface
  537. {
  538. public function emergency($message, array $context = array());
  539. public function alert($message, array $context = array());
  540. public function critical($message, array $context = array());
  541. public function error($message, array $context = array());
  542. public function warning($message, array $context = array());
  543. public function notice($message, array $context = array());
  544. public function info($message, array $context = array());
  545. public function debug($message, array $context = array());
  546. public function log($level, $message, array $context = array());
  547. }
  548. }
  549. namespace Monolog
  550. {
  551. use Monolog\Handler\HandlerInterface;
  552. use Monolog\Handler\StreamHandler;
  553. use Psr\Log\LoggerInterface;
  554. use Psr\Log\InvalidArgumentException;
  555. class Logger implements LoggerInterface
  556. {
  557. const DEBUG = 100;
  558. const INFO = 200;
  559. const NOTICE = 250;
  560. const WARNING = 300;
  561. const ERROR = 400;
  562. const CRITICAL = 500;
  563. const ALERT = 550;
  564. const EMERGENCY = 600;
  565. const API = 1;
  566. protected static $levels = array(
  567. 100 =>'DEBUG',
  568. 200 =>'INFO',
  569. 250 =>'NOTICE',
  570. 300 =>'WARNING',
  571. 400 =>'ERROR',
  572. 500 =>'CRITICAL',
  573. 550 =>'ALERT',
  574. 600 =>'EMERGENCY',
  575. );
  576. protected static $timezone;
  577. protected $name;
  578. protected $handlers;
  579. protected $processors;
  580. public function __construct($name, array $handlers = array(), array $processors = array())
  581. {
  582. $this->name = $name;
  583. $this->handlers = $handlers;
  584. $this->processors = $processors;
  585. }
  586. public function getName()
  587. {
  588. return $this->name;
  589. }
  590. public function pushHandler(HandlerInterface $handler)
  591. {
  592. array_unshift($this->handlers, $handler);
  593. }
  594. public function popHandler()
  595. {
  596. if (!$this->handlers) {
  597. throw new \LogicException('You tried to pop from an empty handler stack.');
  598. }
  599. return array_shift($this->handlers);
  600. }
  601. public function pushProcessor($callback)
  602. {
  603. if (!is_callable($callback)) {
  604. throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
  605. }
  606. array_unshift($this->processors, $callback);
  607. }
  608. public function popProcessor()
  609. {
  610. if (!$this->processors) {
  611. throw new \LogicException('You tried to pop from an empty processor stack.');
  612. }
  613. return array_shift($this->processors);
  614. }
  615. public function addRecord($level, $message, array $context = array())
  616. {
  617. if (!$this->handlers) {
  618. $this->pushHandler(new StreamHandler('php://stderr', static::DEBUG));
  619. }
  620. if (!static::$timezone) {
  621. static::$timezone = new \DateTimeZone(date_default_timezone_get() ?:'UTC');
  622. }
  623. $record = array('message'=> (string) $message,'context'=> $context,'level'=> $level,'level_name'=> static::getLevelName($level),'channel'=> $this->name,'datetime'=> \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), static::$timezone)->setTimezone(static::$timezone),'extra'=> array(),
  624. );
  625. $handlerKey = null;
  626. foreach ($this->handlers as $key => $handler) {
  627. if ($handler->isHandling($record)) {
  628. $handlerKey = $key;
  629. break;
  630. }
  631. }
  632. if (null === $handlerKey) {
  633. return false;
  634. }
  635. foreach ($this->processors as $processor) {
  636. $record = call_user_func($processor, $record);
  637. }
  638. while (isset($this->handlers[$handlerKey]) &&
  639. false === $this->handlers[$handlerKey]->handle($record)) {
  640. $handlerKey++;
  641. }
  642. return true;
  643. }
  644. public function addDebug($message, array $context = array())
  645. {
  646. return $this->addRecord(static::DEBUG, $message, $context);
  647. }
  648. public function addInfo($message, array $context = array())
  649. {
  650. return $this->addRecord(static::INFO, $message, $context);
  651. }
  652. public function addNotice($message, array $context = array())
  653. {
  654. return $this->addRecord(static::NOTICE, $message, $context);
  655. }
  656. public function addWarning($message, array $context = array())
  657. {
  658. return $this->addRecord(static::WARNING, $message, $context);
  659. }
  660. public function addError($message, array $context = array())
  661. {
  662. return $this->addRecord(static::ERROR, $message, $context);
  663. }
  664. public function addCritical($message, array $context = array())
  665. {
  666. return $this->addRecord(static::CRITICAL, $message, $context);
  667. }
  668. public function addAlert($message, array $context = array())
  669. {
  670. return $this->addRecord(static::ALERT, $message, $context);
  671. }
  672. public function addEmergency($message, array $context = array())
  673. {
  674. return $this->addRecord(static::EMERGENCY, $message, $context);
  675. }
  676. public static function getLevels()
  677. {
  678. return array_flip(static::$levels);
  679. }
  680. public static function getLevelName($level)
  681. {
  682. if (!isset(static::$levels[$level])) {
  683. throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels)));
  684. }
  685. return static::$levels[$level];
  686. }
  687. public function isHandling($level)
  688. {
  689. $record = array('level'=> $level,
  690. );
  691. foreach ($this->handlers as $handler) {
  692. if ($handler->isHandling($record)) {
  693. return true;
  694. }
  695. }
  696. return false;
  697. }
  698. public function log($level, $message, array $context = array())
  699. {
  700. if (is_string($level) && defined(__CLASS__.'::'.strtoupper($level))) {
  701. $level = constant(__CLASS__.'::'.strtoupper($level));
  702. }
  703. return $this->addRecord($level, $message, $context);
  704. }
  705. public function debug($message, array $context = array())
  706. {
  707. return $this->addRecord(static::DEBUG, $message, $context);
  708. }
  709. public function info($message, array $context = array())
  710. {
  711. return $this->addRecord(static::INFO, $message, $context);
  712. }
  713. public function notice($message, array $context = array())
  714. {
  715. return $this->addRecord(static::NOTICE, $message, $context);
  716. }
  717. public function warn($message, array $context = array())
  718. {
  719. return $this->addRecord(static::WARNING, $message, $context);
  720. }
  721. public function warning($message, array $context = array())
  722. {
  723. return $this->addRecord(static::WARNING, $message, $context);
  724. }
  725. public function err($message, array $context = array())
  726. {
  727. return $this->addRecord(static::ERROR, $message, $context);
  728. }
  729. public function error($message, array $context = array())
  730. {
  731. return $this->addRecord(static::ERROR, $message, $context);
  732. }
  733. public function crit($message, array $context = array())
  734. {
  735. return $this->addRecord(static::CRITICAL, $message, $context);
  736. }
  737. public function critical($message, array $context = array())
  738. {
  739. return $this->addRecord(static::CRITICAL, $message, $context);
  740. }
  741. public function alert($message, array $context = array())
  742. {
  743. return $this->addRecord(static::ALERT, $message, $context);
  744. }
  745. public function emerg($message, array $context = array())
  746. {
  747. return $this->addRecord(static::EMERGENCY, $message, $context);
  748. }
  749. public function emergency($message, array $context = array())
  750. {
  751. return $this->addRecord(static::EMERGENCY, $message, $context);
  752. }
  753. }
  754. }
  755. namespace Sensio\Bundle\FrameworkExtraBundle\Configuration
  756. {
  757. interface ConfigurationInterface
  758. {
  759. public function getAliasName();
  760. public function allowArray();
  761. }
  762. }
  763. namespace Sensio\Bundle\FrameworkExtraBundle\Configuration
  764. {
  765. abstract class ConfigurationAnnotation implements ConfigurationInterface
  766. {
  767. public function __construct(array $values)
  768. {
  769. foreach ($values as $k => $v) {
  770. if (!method_exists($this, $name ='set'.$k)) {
  771. throw new \RuntimeException(sprintf('Unknown key "%s" for annotation "@%s".', $k, get_class($this)));
  772. }
  773. $this->$name($v);
  774. }
  775. }
  776. }
  777. }
  778. namespace Symfony\Component\EventDispatcher
  779. {
  780. interface EventSubscriberInterface
  781. {
  782. public static function getSubscribedEvents();
  783. }
  784. }
  785. namespace Sensio\Bundle\FrameworkExtraBundle\EventListener
  786. {
  787. use Doctrine\Common\Annotations\Reader;
  788. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  789. use Symfony\Component\HttpKernel\KernelEvents;
  790. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  791. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
  792. use Doctrine\Common\Util\ClassUtils;
  793. class ControllerListener implements EventSubscriberInterface
  794. {
  795. protected $reader;
  796. public function __construct(Reader $reader)
  797. {
  798. $this->reader = $reader;
  799. }
  800. public function onKernelController(FilterControllerEvent $event)
  801. {
  802. if (!is_array($controller = $event->getController())) {
  803. return;
  804. }
  805. $className = class_exists('Doctrine\Common\Util\ClassUtils') ? ClassUtils::getClass($controller[0]) : get_class($controller[0]);
  806. $object = new \ReflectionClass($className);
  807. $method = $object->getMethod($controller[1]);
  808. $classConfigurations = $this->getConfigurations($this->reader->getClassAnnotations($object));
  809. $methodConfigurations = $this->getConfigurations($this->reader->getMethodAnnotations($method));
  810. $configurations = array();
  811. foreach (array_merge(array_keys($classConfigurations), array_keys($methodConfigurations)) as $key) {
  812. if (!array_key_exists($key, $classConfigurations)) {
  813. $configurations[$key] = $methodConfigurations[$key];
  814. } elseif (!array_key_exists($key, $methodConfigurations)) {
  815. $configurations[$key] = $classConfigurations[$key];
  816. } else {
  817. if (is_array($classConfigurations[$key])) {
  818. if (!is_array($methodConfigurations[$key])) {
  819. throw new \UnexpectedValueException('Configurations should both be an array or both not be an array');
  820. }
  821. $configurations[$key] = array_merge($classConfigurations[$key], $methodConfigurations[$key]);
  822. } else {
  823. $configurations[$key] = $methodConfigurations[$key];
  824. }
  825. }
  826. }
  827. $request = $event->getRequest();
  828. foreach ($configurations as $key => $attributes) {
  829. $request->attributes->set($key, $attributes);
  830. }
  831. }
  832. protected function getConfigurations(array $annotations)
  833. {
  834. $configurations = array();
  835. foreach ($annotations as $configuration) {
  836. if ($configuration instanceof ConfigurationInterface) {
  837. if ($configuration->allowArray()) {
  838. $configurations['_'.$configuration->getAliasName()][] = $configuration;
  839. } else {
  840. $configurations['_'.$configuration->getAliasName()] = $configuration;
  841. }
  842. }
  843. }
  844. return $configurations;
  845. }
  846. public static function getSubscribedEvents()
  847. {
  848. return array(
  849. KernelEvents::CONTROLLER =>'onKernelController',
  850. );
  851. }
  852. }
  853. }
  854. namespace Sensio\Bundle\FrameworkExtraBundle\EventListener
  855. {
  856. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  857. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  858. use Symfony\Component\HttpKernel\KernelEvents;
  859. use Symfony\Component\HttpFoundation\Response;
  860. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  861. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  862. class HttpCacheListener implements EventSubscriberInterface
  863. {
  864. private $lastModifiedDates;
  865. private $etags;
  866. private $expressionLanguage;
  867. public function __construct()
  868. {
  869. $this->lastModifiedDates = new \SplObjectStorage();
  870. $this->etags = new \SplObjectStorage();
  871. }
  872. public function onKernelController(FilterControllerEvent $event)
  873. {
  874. $request = $event->getRequest();
  875. if (!$configuration = $request->attributes->get('_cache')) {
  876. return;
  877. }
  878. $response = new Response();
  879. $lastModifiedDate ='';
  880. if ($configuration->getLastModified()) {
  881. $lastModifiedDate = $this->getExpressionLanguage()->evaluate($configuration->getLastModified(), $request->attributes->all());
  882. $response->setLastModified($lastModifiedDate);
  883. }
  884. $etag ='';
  885. if ($configuration->getETag()) {
  886. $etag = hash('sha256', $this->getExpressionLanguage()->evaluate($configuration->getETag(), $request->attributes->all()));
  887. $response->setETag($etag);
  888. }
  889. if ($response->isNotModified($request)) {
  890. $event->setController(function () use ($response) {
  891. return $response;
  892. });
  893. } else {
  894. if ($etag) {
  895. $this->etags[$request] = $etag;
  896. }
  897. if ($lastModifiedDate) {
  898. $this->lastModifiedDates[$request] = $lastModifiedDate;
  899. }
  900. }
  901. }
  902. public function onKernelResponse(FilterResponseEvent $event)
  903. {
  904. $request = $event->getRequest();
  905. if (!$configuration = $request->attributes->get('_cache')) {
  906. return;
  907. }
  908. $response = $event->getResponse();
  909. if (!$response->isSuccessful()) {
  910. return;
  911. }
  912. if (null !== $configuration->getSMaxAge()) {
  913. $response->setSharedMaxAge($configuration->getSMaxAge());
  914. }
  915. if (null !== $configuration->getMaxAge()) {
  916. $response->setMaxAge($configuration->getMaxAge());
  917. }
  918. if (null !== $configuration->getExpires()) {
  919. $date = \DateTime::createFromFormat('U', strtotime($configuration->getExpires()), new \DateTimeZone('UTC'));
  920. $response->setExpires($date);
  921. }
  922. if (null !== $configuration->getVary()) {
  923. $response->setVary($configuration->getVary());
  924. }
  925. if ($configuration->isPublic()) {
  926. $response->setPublic();
  927. }
  928. if (isset($this->lastModifiedDates[$request])) {
  929. $response->setLastModified($this->lastModifiedDates[$request]);
  930. unset($this->lastModifiedDates[$request]);
  931. }
  932. if (isset($this->etags[$request])) {
  933. $response->setETag($this->etags[$request]);
  934. unset($this->etags[$request]);
  935. }
  936. $event->setResponse($response);
  937. }
  938. public static function getSubscribedEvents()
  939. {
  940. return array(
  941. KernelEvents::CONTROLLER =>'onKernelController',
  942. KernelEvents::RESPONSE =>'onKernelResponse',
  943. );
  944. }
  945. private function getExpressionLanguage()
  946. {
  947. if (null === $this->expressionLanguage) {
  948. if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
  949. throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
  950. }
  951. $this->expressionLanguage = new ExpressionLanguage();
  952. }
  953. return $this->expressionLanguage;
  954. }
  955. }
  956. }
  957. namespace Sensio\Bundle\FrameworkExtraBundle\EventListener
  958. {
  959. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  960. use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterManager;
  961. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  962. use Symfony\Component\HttpKernel\KernelEvents;
  963. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  964. class ParamConverterListener implements EventSubscriberInterface
  965. {
  966. protected $manager;
  967. public function __construct(ParamConverterManager $manager)
  968. {
  969. $this->manager = $manager;
  970. }
  971. public function onKernelController(FilterControllerEvent $event)
  972. {
  973. $controller = $event->getController();
  974. $request = $event->getRequest();
  975. $configurations = array();
  976. if ($configuration = $request->attributes->get('_converters')) {
  977. foreach (is_array($configuration) ? $configuration : array($configuration) as $configuration) {
  978. $configurations[$configuration->getName()] = $configuration;
  979. }
  980. }
  981. if (is_array($controller)) {
  982. $r = new \ReflectionMethod($controller[0], $controller[1]);
  983. } else {
  984. $r = new \ReflectionFunction($controller);
  985. }
  986. foreach ($r->getParameters() as $param) {
  987. if (!$param->getClass() || $param->getClass()->isInstance($request)) {
  988. continue;
  989. }
  990. $name = $param->getName();
  991. if (!isset($configurations[$name])) {
  992. $configuration = new ParamConverter(array());
  993. $configuration->setName($name);
  994. $configuration->setClass($param->getClass()->getName());
  995. $configurations[$name] = $configuration;
  996. } elseif (null === $configurations[$name]->getClass()) {
  997. $configurations[$name]->setClass($param->getClass()->getName());
  998. }
  999. $configurations[$name]->setIsOptional($param->isOptional());
  1000. }
  1001. $this->manager->apply($request, $configurations);
  1002. }
  1003. public static function getSubscribedEvents()
  1004. {
  1005. return array(
  1006. KernelEvents::CONTROLLER =>'onKernelController',
  1007. );
  1008. }
  1009. }
  1010. }
  1011. namespace Sensio\Bundle\FrameworkExtraBundle\EventListener
  1012. {
  1013. use Sensio\Bundle\FrameworkExtraBundle\Security\ExpressionLanguage;
  1014. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  1015. use Symfony\Component\HttpKernel\KernelEvents;
  1016. use Symfony\Component\HttpFoundation\Request;
  1017. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  1018. use Symfony\Component\ExpressionLanguage\Expression;
  1019. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  1020. use Symfony\Component\Security\Core\SecurityContextInterface;
  1021. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  1022. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  1023. class SecurityListener implements EventSubscriberInterface
  1024. {
  1025. private $securityContext;
  1026. private $language;
  1027. private $trustResolver;
  1028. private $roleHierarchy;
  1029. public function __construct(SecurityContextInterface $securityContext = null, ExpressionLanguage $language = null, AuthenticationTrustResolverInterface $trustResolver = null, RoleHierarchyInterface $roleHierarchy = null)
  1030. {
  1031. $this->securityContext = $securityContext;
  1032. $this->language = $language;
  1033. $this->trustResolver = $trustResolver;
  1034. $this->roleHierarchy = $roleHierarchy;
  1035. }
  1036. public function onKernelController(FilterControllerEvent $event)
  1037. {
  1038. $request = $event->getRequest();
  1039. if (!$configuration = $request->attributes->get('_security')) {
  1040. return;
  1041. }
  1042. if (null === $this->securityContext || null === $this->trustResolver) {
  1043. throw new \LogicException('To use the @Security tag, you need to install the Symfony Security bundle.');
  1044. }
  1045. if (!$this->language->evaluate($configuration->getExpression(), $this->getVariables($request))) {
  1046. throw new AccessDeniedException(sprintf('Expression "%s" denied access.', $configuration->getExpression()));
  1047. }
  1048. }
  1049. private function getVariables(Request $request)
  1050. {
  1051. $token = $this->securityContext->getToken();
  1052. if (null !== $this->roleHierarchy) {
  1053. $roles = $this->roleHierarchy->getReachableRoles($token->getRoles());
  1054. } else {
  1055. $roles = $token->getRoles();
  1056. }
  1057. $variables = array('token'=> $token,'user'=> $token->getUser(),'object'=> $request,'request'=> $request,'roles'=> array_map(function ($role) { return $role->getRole(); }, $roles),'trust_resolver'=> $this->trustResolver,'security_context'=> $this->securityContext,
  1058. );
  1059. return array_merge($request->attributes->all(), $variables);
  1060. }
  1061. public static function getSubscribedEvents()
  1062. {
  1063. return array(KernelEvents::CONTROLLER =>'onKernelController');
  1064. }
  1065. }
  1066. }
  1067. namespace Sensio\Bundle\FrameworkExtraBundle\EventListener
  1068. {
  1069. use Symfony\Component\DependencyInjection\ContainerInterface;
  1070. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  1071. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  1072. use Symfony\Component\HttpKernel\KernelEvents;
  1073. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  1074. use Symfony\Component\HttpFoundation\StreamedResponse;
  1075. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  1076. class TemplateListener implements EventSubscriberInterface
  1077. {
  1078. protected $container;
  1079. public function __construct(ContainerInterface $container)
  1080. {
  1081. $this->container = $container;
  1082. }
  1083. public function onKernelController(FilterControllerEvent $event)
  1084. {
  1085. if (!is_array($controller = $event->getController())) {
  1086. return;
  1087. }
  1088. $request = $event->getRequest();
  1089. if (!$configuration = $request->attributes->get('_template')) {
  1090. return;
  1091. }
  1092. if (!$configuration->getTemplate()) {
  1093. $guesser = $this->container->get('sensio_framework_extra.view.guesser');
  1094. $configuration->setTemplate($guesser->guessTemplateName($controller, $request, $configuration->getEngine()));
  1095. }
  1096. $request->attributes->set('_template', $configuration->getTemplate());
  1097. $request->attributes->set('_template_vars', $configuration->getVars());
  1098. $request->attributes->set('_template_streamable', $configuration->isStreamable());
  1099. if (!$configuration->getVars()) {
  1100. $r = new \ReflectionObject($controller[0]);
  1101. $vars = array();
  1102. foreach ($r->getMethod($controller[1])->getParameters() as $param) {
  1103. $vars[] = $param->getName();
  1104. }
  1105. $request->attributes->set('_template_default_vars', $vars);
  1106. }
  1107. }
  1108. public function onKernelView(GetResponseForControllerResultEvent $event)
  1109. {
  1110. $request = $event->getRequest();
  1111. $parameters = $event->getControllerResult();
  1112. $templating = $this->container->get('templating');
  1113. if (null === $parameters) {
  1114. if (!$vars = $request->attributes->get('_template_vars')) {
  1115. if (!$vars = $request->attributes->get('_template_default_vars')) {
  1116. return;
  1117. }
  1118. }
  1119. $parameters = array();
  1120. foreach ($vars as $var) {
  1121. $parameters[$var] = $request->attributes->get($var);
  1122. }
  1123. }
  1124. if (!is_array($parameters)) {
  1125. return $parameters;
  1126. }
  1127. if (!$template = $request->attributes->get('_template')) {
  1128. return $parameters;
  1129. }
  1130. if (!$request->attributes->get('_template_streamable')) {
  1131. $event->setResponse($templating->renderResponse($template, $parameters));
  1132. } else {
  1133. $callback = function () use ($templating, $template, $parameters) {
  1134. return $templating->stream($template, $parameters);
  1135. };
  1136. $event->setResponse(new StreamedResponse($callback));
  1137. }
  1138. }
  1139. public static function getSubscribedEvents()
  1140. {
  1141. return array(
  1142. KernelEvents::CONTROLLER => array('onKernelController', -128),
  1143. KernelEvents::VIEW =>'onKernelView',
  1144. );
  1145. }
  1146. }
  1147. }
  1148. namespace Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter
  1149. {
  1150. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  1151. use Symfony\Component\HttpFoundation\Request;
  1152. interface ParamConverterInterface
  1153. {
  1154. public function apply(Request $request, ParamConverter $configuration);
  1155. public function supports(ParamConverter $configuration);
  1156. }
  1157. }
  1158. namespace Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter
  1159. {
  1160. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  1161. use Symfony\Component\HttpFoundation\Request;
  1162. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  1163. use DateTime;
  1164. class DateTimeParamConverter implements ParamConverterInterface
  1165. {
  1166. public function apply(Request $request, ParamConverter $configuration)
  1167. {
  1168. $param = $configuration->getName();
  1169. if (!$request->attributes->has($param)) {
  1170. return false;
  1171. }
  1172. $options = $configuration->getOptions();
  1173. $value = $request->attributes->get($param);
  1174. if (!$value && $configuration->isOptional()) {
  1175. return false;
  1176. }
  1177. $date = isset($options['format'])
  1178. ? DateTime::createFromFormat($options['format'], $value)
  1179. : new DateTime($value);
  1180. if (!$date) {
  1181. throw new NotFoundHttpException('Invalid date given.');
  1182. }
  1183. $request->attributes->set($param, $date);
  1184. return true;
  1185. }
  1186. public function supports(ParamConverter $configuration)
  1187. {
  1188. if (null === $configuration->getClass()) {
  1189. return false;
  1190. }
  1191. return"DateTime"=== $configuration->getClass();
  1192. }
  1193. }
  1194. }
  1195. namespace Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter
  1196. {
  1197. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  1198. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  1199. use Symfony\Component\HttpFoundation\Request;
  1200. use Doctrine\Common\Persistence\ManagerRegistry;
  1201. use Doctrine\ORM\NoResultException;
  1202. class DoctrineParamConverter implements ParamConverterInterface
  1203. {
  1204. protected $registry;
  1205. public function __construct(ManagerRegistry $registry = null)
  1206. {
  1207. $this->registry = $registry;
  1208. }
  1209. public function apply(Request $request, ParamConverter $configuration)
  1210. {
  1211. $name = $configuration->getName();
  1212. $class = $configuration->getClass();
  1213. $options = $this->getOptions($configuration);
  1214. if (null === $request->attributes->get($name, false)) {
  1215. $configuration->setIsOptional(true);
  1216. }
  1217. if (false === $object = $this->find($class, $request, $options, $name)) {
  1218. if (false === $object = $this->findOneBy($class, $request, $options)) {
  1219. if ($configuration->isOptional()) {
  1220. $object = null;
  1221. } else {
  1222. throw new \LogicException('Unable to guess how to get a Doctrine instance from the request information.');
  1223. }
  1224. }
  1225. }
  1226. if (null === $object && false === $configuration->isOptional()) {
  1227. throw new NotFoundHttpException(sprintf('%s object not found.', $class));
  1228. }
  1229. $request->attributes->set($name, $object);
  1230. return true;
  1231. }
  1232. protected function find($class, Request $request, $options, $name)
  1233. {
  1234. if ($options['mapping'] || $options['exclude']) {
  1235. return false;
  1236. }
  1237. $id = $this->getIdentifier($request, $options, $name);
  1238. if (false === $id || null === $id) {
  1239. return false;
  1240. }
  1241. if (isset($options['repository_method'])) {
  1242. $method = $options['repository_method'];
  1243. } else {
  1244. $method ='find';
  1245. }
  1246. try {
  1247. return $this->getManager($options['entity_manager'], $class)->getRepository($class)->$method($id);
  1248. } catch (NoResultException $e) {
  1249. return null;
  1250. }
  1251. }
  1252. protected function getIdentifier(Request $request, $options, $name)
  1253. {
  1254. if (isset($options['id'])) {
  1255. if (!is_array($options['id'])) {
  1256. $name = $options['id'];
  1257. } elseif (is_array($options['id'])) {
  1258. $id = array();
  1259. foreach ($options['id'] as $field) {
  1260. $id[$field] = $request->attributes->get($field);
  1261. }
  1262. return $id;
  1263. }
  1264. }
  1265. if ($request->attributes->has($name)) {
  1266. return $request->attributes->get($name);
  1267. }
  1268. if ($request->attributes->has('id')) {
  1269. return $request->attributes->get('id');
  1270. }
  1271. return false;
  1272. }
  1273. protected function findOneBy($class, Request $request, $options)
  1274. {
  1275. if (!$options['mapping']) {
  1276. $keys = $request->attributes->keys();
  1277. $options['mapping'] = $keys ? array_combine($keys, $keys) : array();
  1278. }
  1279. foreach ($options['exclude'] as $exclude) {
  1280. unset($options['mapping'][$exclude]);
  1281. }
  1282. if (!$options['mapping']) {
  1283. return false;
  1284. }
  1285. $criteria = array();
  1286. $em = $this->getManager($options['entity_manager'], $class);
  1287. $metadata = $em->getClassMetadata($class);
  1288. foreach ($options['mapping'] as $attribute => $field) {
  1289. if ($metadata->hasField($field) || ($metadata->hasAssociation($field) && $metadata->isSingleValuedAssociation($field))) {
  1290. $criteria[$field] = $request->attributes->get($attribute);
  1291. }
  1292. }
  1293. if ($options['strip_null']) {
  1294. $criteria = array_filter($criteria, function ($value) { return !is_null($value); });
  1295. }
  1296. if (!$criteria) {
  1297. return false;
  1298. }
  1299. if (isset($options['repository_method'])) {
  1300. $method = $options['repository_method'];
  1301. } else {
  1302. $method ='findOneBy';
  1303. }
  1304. try {
  1305. return $em->getRepository($class)->$method($criteria);
  1306. } catch (NoResultException $e) {
  1307. return null;
  1308. }
  1309. }
  1310. public function supports(ParamConverter $configuration)
  1311. {
  1312. if (null === $this->registry || !count($this->registry->getManagers())) {
  1313. return false;
  1314. }
  1315. if (null === $configuration->getClass()) {
  1316. return false;
  1317. }
  1318. $options = $this->getOptions($configuration);
  1319. $em = $this->getManager($options['entity_manager'], $configuration->getClass());
  1320. if (null === $em) {
  1321. return false;
  1322. }
  1323. return ! $em->getMetadataFactory()->isTransient($configuration->getClass());
  1324. }
  1325. protected function getOptions(ParamConverter $configuration)
  1326. {
  1327. return array_replace(array('entity_manager'=> null,'exclude'=> array(),'mapping'=> array(),'strip_null'=> false,
  1328. ), $configuration->getOptions());
  1329. }
  1330. private function getManager($name, $class)
  1331. {
  1332. if (null === $name) {
  1333. return $this->registry->getManagerForClass($class);
  1334. }
  1335. return $this->registry->getManager($name);
  1336. }
  1337. }
  1338. }
  1339. namespace Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter
  1340. {
  1341. use Symfony\Component\HttpFoundation\Request;
  1342. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
  1343. class ParamConverterManager
  1344. {
  1345. protected $converters = array();
  1346. protected $namedConverters = array();
  1347. public function apply(Request $request, $configurations)
  1348. {
  1349. if (is_object($configurations)) {
  1350. $configurations = array($configurations);
  1351. }
  1352. foreach ($configurations as $configuration) {
  1353. $this->applyConverter($request, $configuration);
  1354. }
  1355. }
  1356. protected function applyConverter(Request $request, ConfigurationInterface $configuration)
  1357. {
  1358. $value = $request->attributes->get($configuration->getName());
  1359. $className = $configuration->getClass();
  1360. if (is_object($value) && $value instanceof $className) {
  1361. return;
  1362. }
  1363. if ($converterName = $configuration->getConverter()) {
  1364. if (!isset($this->namedConverters[$converterName])) {
  1365. throw new \RuntimeException(sprintf("No converter named '%s' found for conversion of parameter '%s'.",
  1366. $converterName, $configuration->getName()
  1367. ));
  1368. }
  1369. $converter = $this->namedConverters[$converterName];
  1370. if (!$converter->supports($configuration)) {
  1371. throw new \RuntimeException(sprintf("Converter '%s' does not support conversion of parameter '%s'.",
  1372. $converterName, $configuration->getName()
  1373. ));
  1374. }
  1375. $converter->apply($request, $configuration);
  1376. return;
  1377. }
  1378. foreach ($this->all() as $converter) {
  1379. if ($converter->supports($configuration)) {
  1380. if ($converter->apply($request, $configuration)) {
  1381. return;
  1382. }
  1383. }
  1384. }
  1385. }
  1386. public function add(ParamConverterInterface $converter, $priority = 0, $name = null)
  1387. {
  1388. if ($priority !== null) {
  1389. if (!isset($this->converters[$priority])) {
  1390. $this->converters[$priority] = array();
  1391. }
  1392. $this->converters[$priority][] = $converter;
  1393. }
  1394. if (null !== $name) {
  1395. $this->namedConverters[$name] = $converter;
  1396. }
  1397. }
  1398. public function all()
  1399. {
  1400. krsort($this->converters);
  1401. $converters = array();
  1402. foreach ($this->converters as $all) {
  1403. $converters = array_merge($converters, $all);
  1404. }
  1405. return $converters;
  1406. }
  1407. }
  1408. }
  1409. namespace Symfony\Component\HttpKernel\Log
  1410. {
  1411. interface DebugLoggerInterface
  1412. {
  1413. public function getLogs();
  1414. public function countErrors();
  1415. }
  1416. }
  1417. namespace Symfony\Bridge\Monolog\Handler
  1418. {
  1419. use Monolog\Logger;
  1420. use Monolog\Handler\TestHandler;
  1421. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  1422. class DebugHandler extends TestHandler implements DebugLoggerInterface
  1423. {
  1424. public function getLogs()
  1425. {
  1426. $records = array();
  1427. foreach ($this->records as $record) {
  1428. $records[] = array('timestamp'=> $record['datetime']->getTimestamp(),'message'=> $record['message'],'priority'=> $record['level'],'priorityName'=> $record['level_name'],'context'=> $record['context'],
  1429. );
  1430. }
  1431. return $records;
  1432. }
  1433. public function countErrors()
  1434. {
  1435. $cnt = 0;
  1436. $levels = array(Logger::ERROR, Logger::CRITICAL, Logger::ALERT, Logger::EMERGENCY);
  1437. foreach ($levels as $level) {
  1438. if (isset($this->recordsByLevel[$level])) {
  1439. $cnt += count($this->recordsByLevel[$level]);
  1440. }
  1441. }
  1442. return $cnt;
  1443. }
  1444. }
  1445. }
  1446. namespace Symfony\Component\HttpKernel\Log
  1447. {
  1448. use Psr\Log\LoggerInterface as PsrLogger;
  1449. interface LoggerInterface extends PsrLogger
  1450. {
  1451. public function emerg($message, array $context = array());
  1452. public function crit($message, array $context = array());
  1453. public function err($message, array $context = array());
  1454. public function warn($message, array $context = array());
  1455. }
  1456. }
  1457. namespace Symfony\Bridge\Monolog
  1458. {
  1459. use Monolog\Logger as BaseLogger;
  1460. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  1461. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  1462. class Logger extends BaseLogger implements LoggerInterface, DebugLoggerInterface
  1463. {
  1464. public function emerg($message, array $context = array())
  1465. {
  1466. return parent::addRecord(BaseLogger::EMERGENCY, $message, $context);
  1467. }
  1468. public function crit($message, array $context = array())
  1469. {
  1470. return parent::addRecord(BaseLogger::CRITICAL, $message, $context);
  1471. }
  1472. public function err($message, array $context = array())
  1473. {
  1474. return parent::addRecord(BaseLogger::ERROR, $message, $context);
  1475. }
  1476. public function warn($message, array $context = array())
  1477. {
  1478. return parent::addRecord(BaseLogger::WARNING, $message, $context);
  1479. }
  1480. public function getLogs()
  1481. {
  1482. if ($logger = $this->getDebugLogger()) {
  1483. return $logger->getLogs();
  1484. }
  1485. return array();
  1486. }
  1487. public function countErrors()
  1488. {
  1489. if ($logger = $this->getDebugLogger()) {
  1490. return $logger->countErrors();
  1491. }
  1492. return 0;
  1493. }
  1494. private function getDebugLogger()
  1495. {
  1496. foreach ($this->handlers as $handler) {
  1497. if ($handler instanceof DebugLoggerInterface) {
  1498. return $handler;
  1499. }
  1500. }
  1501. }
  1502. }
  1503. }
  1504. namespace Assetic
  1505. {
  1506. interface ValueSupplierInterface
  1507. {
  1508. public function getValues();
  1509. }
  1510. }
  1511. namespace Symfony\Bundle\AsseticBundle
  1512. {
  1513. use Assetic\ValueSupplierInterface;
  1514. use Symfony\Component\DependencyInjection\ContainerInterface;
  1515. class DefaultValueSupplier implements ValueSupplierInterface
  1516. {
  1517. protected $container;
  1518. public function __construct(ContainerInterface $container)
  1519. {
  1520. $this->container = $container;
  1521. }
  1522. public function getValues()
  1523. {
  1524. if (!$this->container->isScopeActive('request')) {
  1525. return array();
  1526. }
  1527. $request = $this->container->get('request');
  1528. return array('locale'=> $request->getLocale(),'env'=> $this->container->getParameter('kernel.environment'),
  1529. );
  1530. }
  1531. }
  1532. }
  1533. namespace Assetic\Factory
  1534. {
  1535. use Assetic\Asset\AssetCollection;
  1536. use Assetic\Asset\AssetCollectionInterface;
  1537. use Assetic\Asset\AssetInterface;
  1538. use Assetic\Asset\AssetReference;
  1539. use Assetic\Asset\FileAsset;
  1540. use Assetic\Asset\GlobAsset;
  1541. use Assetic\Asset\HttpAsset;
  1542. use Assetic\AssetManager;
  1543. use Assetic\Factory\Worker\WorkerInterface;
  1544. use Assetic\FilterManager;
  1545. class AssetFactory
  1546. {
  1547. private $root;
  1548. private $debug;
  1549. private $output;
  1550. private $workers;
  1551. private $am;
  1552. private $fm;
  1553. public function __construct($root, $debug = false)
  1554. {
  1555. $this->root = rtrim($root,'/');
  1556. $this->debug = $debug;
  1557. $this->output ='assetic/*';
  1558. $this->workers = array();
  1559. }
  1560. public function setDebug($debug)
  1561. {
  1562. $this->debug = $debug;
  1563. }
  1564. public function isDebug()
  1565. {
  1566. return $this->debug;
  1567. }
  1568. public function setDefaultOutput($output)
  1569. {
  1570. $this->output = $output;
  1571. }
  1572. public function addWorker(WorkerInterface $worker)
  1573. {
  1574. $this->workers[] = $worker;
  1575. }
  1576. public function getAssetManager()
  1577. {
  1578. return $this->am;
  1579. }
  1580. public function setAssetManager(AssetManager $am)
  1581. {
  1582. $this->am = $am;
  1583. }
  1584. public function getFilterManager()
  1585. {
  1586. return $this->fm;
  1587. }
  1588. public function setFilterManager(FilterManager $fm)
  1589. {
  1590. $this->fm = $fm;
  1591. }
  1592. public function createAsset($inputs = array(), $filters = array(), array $options = array())
  1593. {
  1594. if (!is_array($inputs)) {
  1595. $inputs = array($inputs);
  1596. }
  1597. if (!is_array($filters)) {
  1598. $filters = array($filters);
  1599. }
  1600. if (!isset($options['output'])) {
  1601. $options['output'] = $this->output;
  1602. }
  1603. if (!isset($options['vars'])) {
  1604. $options['vars'] = array();
  1605. }
  1606. if (!isset($options['debug'])) {
  1607. $options['debug'] = $this->debug;
  1608. }
  1609. if (!isset($options['root'])) {
  1610. $options['root'] = array($this->root);
  1611. } else {
  1612. if (!is_array($options['root'])) {
  1613. $options['root'] = array($options['root']);
  1614. }
  1615. $options['root'][] = $this->root;
  1616. }
  1617. if (!isset($options['name'])) {
  1618. $options['name'] = $this->generateAssetName($inputs, $filters, $options);
  1619. }
  1620. $asset = $this->createAssetCollection(array(), $options);
  1621. $extensions = array();
  1622. foreach ($inputs as $input) {
  1623. if (is_array($input)) {
  1624. $asset->add(call_user_func_array(array($this,'createAsset'), $input));
  1625. } else {
  1626. $asset->add($this->parseInput($input, $options));
  1627. $extensions[pathinfo($input, PATHINFO_EXTENSION)] = true;
  1628. }
  1629. }
  1630. foreach ($filters as $filter) {
  1631. if ('?'!= $filter[0]) {
  1632. $asset->ensureFilter($this->getFilter($filter));
  1633. } elseif (!$options['debug']) {
  1634. $asset->ensureFilter($this->getFilter(substr($filter, 1)));
  1635. }
  1636. }
  1637. if (!empty($options['vars'])) {
  1638. $toAdd = array();
  1639. foreach ($options['vars'] as $var) {
  1640. if (false !== strpos($options['output'],'{'.$var.'}')) {
  1641. continue;
  1642. }
  1643. $toAdd[] ='{'.$var.'}';
  1644. }
  1645. if ($toAdd) {
  1646. $options['output'] = str_replace('*','*.'.implode('.', $toAdd), $options['output']);
  1647. }
  1648. }
  1649. if (1 == count($extensions) && !pathinfo($options['output'], PATHINFO_EXTENSION) && $extension = key($extensions)) {
  1650. $options['output'] .='.'.$extension;
  1651. }
  1652. $asset->setTargetPath(str_replace('*', $options['name'], $options['output']));
  1653. return $this->applyWorkers($asset);
  1654. }
  1655. public function generateAssetName($inputs, $filters, $options = array())
  1656. {
  1657. foreach (array_diff(array_keys($options), array('output','debug','root')) as $key) {
  1658. unset($options[$key]);
  1659. }
  1660. ksort($options);
  1661. return substr(sha1(serialize($inputs).serialize($filters).serialize($options)), 0, 7);
  1662. }
  1663. protected function parseInput($input, array $options = array())
  1664. {
  1665. if ('@'== $input[0]) {
  1666. return $this->createAssetReference(substr($input, 1));
  1667. }
  1668. if (false !== strpos($input,'://') || 0 === strpos($input,'//')) {
  1669. return $this->createHttpAsset($input, $options['vars']);
  1670. }
  1671. if (self::isAbsolutePath($input)) {
  1672. if ($root = self::findRootDir($input, $options['root'])) {
  1673. $path = ltrim(substr($input, strlen($root)),'/');
  1674. } else {
  1675. $path = null;
  1676. }
  1677. } else {
  1678. $root = $this->root;
  1679. $path = $input;
  1680. $input = $this->root.'/'.$path;
  1681. }
  1682. if (false !== strpos($input,'*')) {
  1683. return $this->createGlobAsset($input, $root, $options['vars']);
  1684. }
  1685. return $this->createFileAsset($input, $root, $path, $options['vars']);
  1686. }
  1687. protected function createAssetCollection(array $assets = array(), array $options = array())
  1688. {
  1689. return new AssetCollection($assets, array(), null, isset($options['vars']) ? $options['vars'] : array());
  1690. }
  1691. protected function createAssetReference($name)
  1692. {
  1693. if (!$this->am) {
  1694. throw new \LogicException('There is no asset manager.');
  1695. }
  1696. return new AssetReference($this->am, $name);
  1697. }
  1698. protected function createHttpAsset($sourceUrl, $vars)
  1699. {
  1700. return new HttpAsset($sourceUrl, array(), false, $vars);
  1701. }
  1702. protected function createGlobAsset($glob, $root = null, $vars)
  1703. {
  1704. return new GlobAsset($glob, array(), $root, $vars);
  1705. }
  1706. protected function createFileAsset($source, $root = null, $path = null, $vars)
  1707. {
  1708. return new FileAsset($source, array(), $root, $path, $vars);
  1709. }
  1710. protected function getFilter($name)
  1711. {
  1712. if (!$this->fm) {
  1713. throw new \LogicException('There is no filter manager.');
  1714. }
  1715. return $this->fm->get($name);
  1716. }
  1717. private function applyWorkers(AssetCollectionInterface $asset)
  1718. {
  1719. foreach ($asset as $leaf) {
  1720. foreach ($this->workers as $worker) {
  1721. $retval = $worker->process($leaf);
  1722. if ($retval instanceof AssetInterface && $leaf !== $retval) {
  1723. $asset->replaceLeaf($leaf, $retval);
  1724. }
  1725. }
  1726. }
  1727. foreach ($this->workers as $worker) {
  1728. $retval = $worker->process($asset);
  1729. if ($retval instanceof AssetInterface) {
  1730. $asset = $retval;
  1731. }
  1732. }
  1733. return $asset instanceof AssetCollectionInterface ? $asset : $this->createAssetCollection(array($asset));
  1734. }
  1735. private static function isAbsolutePath($path)
  1736. {
  1737. return'/'== $path[0] ||'\\'== $path[0] || (3 < strlen($path) && ctype_alpha($path[0]) && $path[1] ==':'&& ('\\'== $path[2] ||'/'== $path[2]));
  1738. }
  1739. private static function findRootDir($path, array $roots)
  1740. {
  1741. foreach ($roots as $root) {
  1742. if (0 === strpos($path, $root)) {
  1743. return $root;
  1744. }
  1745. }
  1746. }
  1747. }
  1748. }
  1749. namespace Symfony\Bundle\AsseticBundle\Factory
  1750. {
  1751. use Assetic\Factory\AssetFactory as BaseAssetFactory;
  1752. use Symfony\Component\DependencyInjection\ContainerInterface;
  1753. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  1754. use Symfony\Component\HttpKernel\KernelInterface;
  1755. class AssetFactory extends BaseAssetFactory
  1756. {
  1757. private $kernel;
  1758. private $container;
  1759. private $parameterBag;
  1760. public function __construct(KernelInterface $kernel, ContainerInterface $container, ParameterBagInterface $parameterBag, $baseDir, $debug = false)
  1761. {
  1762. $this->kernel = $kernel;
  1763. $this->container = $container;
  1764. $this->parameterBag = $para

Large files files are truncated, but you can click here to view the full file