Db
5 years ago
Handler
5 years ago
TableLogAction
5 years ago
Visit
5 years ago
Action.php
5 years ago
ActionPageview.php
5 years ago
Cache.php
5 years ago
Db.php
5 years ago
Failures.php
6 years ago
FingerprintSalt.php
6 years ago
GoalManager.php
5 years ago
Handler.php
5 years ago
IgnoreCookie.php
5 years ago
LogTable.php
5 years ago
Model.php
5 years ago
PageUrl.php
5 years ago
Request.php
5 years ago
RequestProcessor.php
5 years ago
RequestSet.php
5 years ago
Response.php
5 years ago
ScheduledTasksRunner.php
5 years ago
Settings.php
5 years ago
TableLogAction.php
5 years ago
TrackerCodeGenerator.php
5 years ago
TrackerConfig.php
5 years ago
Visit.php
5 years ago
VisitExcluded.php
5 years ago
VisitInterface.php
5 years ago
Visitor.php
5 years ago
VisitorNotFoundInDb.php
5 years ago
VisitorRecognizer.php
5 years ago
Handler.php
123 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik\Tracker; |
| 11 | |
| 12 | use Piwik\Container\StaticContainer; |
| 13 | use Piwik\Exception\InvalidRequestParameterException; |
| 14 | use Piwik\Exception\UnexpectedWebsiteFoundException; |
| 15 | use Piwik\Tracker; |
| 16 | use Exception; |
| 17 | use Psr\Log\LoggerInterface; |
| 18 | |
| 19 | class Handler |
| 20 | { |
| 21 | /** |
| 22 | * @var Response |
| 23 | */ |
| 24 | private $response; |
| 25 | |
| 26 | /** |
| 27 | * @var ScheduledTasksRunner |
| 28 | */ |
| 29 | private $tasksRunner; |
| 30 | |
| 31 | /** |
| 32 | * @var LoggerInterface |
| 33 | */ |
| 34 | private $logger; |
| 35 | |
| 36 | public function __construct() |
| 37 | { |
| 38 | $this->setResponse(new Response()); |
| 39 | $this->logger = StaticContainer::get(LoggerInterface::class); |
| 40 | } |
| 41 | |
| 42 | public function setResponse($response) |
| 43 | { |
| 44 | $this->response = $response; |
| 45 | } |
| 46 | |
| 47 | public function init(Tracker $tracker, RequestSet $requestSet) |
| 48 | { |
| 49 | $this->response->init($tracker); |
| 50 | } |
| 51 | |
| 52 | public function process(Tracker $tracker, RequestSet $requestSet) |
| 53 | { |
| 54 | foreach ($requestSet->getRequests() as $request) { |
| 55 | $tracker->trackRequest($request); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public function onStartTrackRequests(Tracker $tracker, RequestSet $requestSet) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | public function onAllRequestsTracked(Tracker $tracker, RequestSet $requestSet) |
| 64 | { |
| 65 | $tasks = $this->getScheduledTasksRunner(); |
| 66 | if ($tasks->shouldRun($tracker)) { |
| 67 | $tasks->runScheduledTasks(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private function getScheduledTasksRunner() |
| 72 | { |
| 73 | if (is_null($this->tasksRunner)) { |
| 74 | $this->tasksRunner = new ScheduledTasksRunner(); |
| 75 | } |
| 76 | |
| 77 | return $this->tasksRunner; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @internal |
| 82 | */ |
| 83 | public function setScheduledTasksRunner(ScheduledTasksRunner $runner) |
| 84 | { |
| 85 | $this->tasksRunner = $runner; |
| 86 | } |
| 87 | |
| 88 | public function onException(Tracker $tracker, RequestSet $requestSet, Exception $e) |
| 89 | { |
| 90 | $statusCode = 500; |
| 91 | if ($e instanceof UnexpectedWebsiteFoundException) { |
| 92 | $statusCode = 400; |
| 93 | } elseif ($e instanceof InvalidRequestParameterException) { |
| 94 | $statusCode = 400; |
| 95 | } |
| 96 | |
| 97 | // if an internal server error, log as a real error, otherwise it's just malformed input |
| 98 | if ($statusCode == 500) { |
| 99 | $this->logger->error('Exception: {exception}', [ |
| 100 | 'exception' => $e, |
| 101 | ]); |
| 102 | } else { |
| 103 | $this->logger->debug('Exception: {exception}', [ |
| 104 | 'exception' => $e, |
| 105 | ]); |
| 106 | } |
| 107 | |
| 108 | $this->response->outputException($tracker, $e, $statusCode); |
| 109 | } |
| 110 | |
| 111 | public function finish(Tracker $tracker, RequestSet $requestSet) |
| 112 | { |
| 113 | $this->response->outputResponse($tracker); |
| 114 | return $this->response->getOutput(); |
| 115 | } |
| 116 | |
| 117 | public function getResponse() |
| 118 | { |
| 119 | return $this->response; |
| 120 | } |
| 121 | |
| 122 | } |
| 123 |