<commit_before><?php

declare(strict_types=1);

/**
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 */
namespace OC\AppFramework\Http;

use OC\AppFramework\Http;
use OC\AppFramework\Middleware\MiddlewareDispatcher;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\DB\ConnectionAdapter;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\IConfig;
use OCP\IRequest;
use Psr\Log\LoggerInterface;

/**
 * Class to dispatch the request to the middleware dispatcher
 */
class Dispatcher {

	/** @var MiddlewareDispatcher */
	private $middlewareDispatcher;

	/** @var Http */
	private $protocol;

	/** @var ControllerMethodReflector */
	private $reflector;

	/** @var IRequest */
	private $request;

	/** @var IConfig */
	private $config;

	/** @var ConnectionAdapter */
	private $connection;

	/** @var LoggerInterface */
	private $logger;

	/**
	 * @param Http $protocol the http protocol with contains all status headers
	 * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which
	 * runs the middleware
	 * @param ControllerMethodReflector $reflector the reflector that is used to inject
	 * the arguments for the controller
	 * @param IRequest $request the incoming request
	 * @param IConfig $config
	 * @param ConnectionAdapter $connection
	 * @param LoggerInterface $logger
	 */
	public function __construct(Http $protocol,
								MiddlewareDispatcher $middlewareDispatcher,
								ControllerMethodReflector $reflector,
								IRequest $request,
								IConfig $config,
								ConnectionAdapter $connection,
								LoggerInterface $logger) {
		$this->protocol = $protocol;
		$this->middlewareDispatcher = $middlewareDispatcher;
		$this->reflector = $reflector;
		$this->request = $request;
		$this->config = $config;
		$this->connection = $connection;
		$this->logger = $logger;
	}


	/**
	 * Handles a request and calls the dispatcher on the controller
	 * @param Controller $controller the controller which will be called
	 * @param string $methodName the method name which will be called on
	 * the controller
	 * @return array $array[0] contains a string with the http main header,
	 * $array[1] contains headers in the form: $key => value, $array[2] contains
	 * the response output
	 * @throws \Exception
	 */
	public function dispatch(Controller $controller, string $methodName): array {
		$out = [null, [], null];

		try {
			// prefill reflector with everything thats needed for the
			// middlewares
			$this->reflector->reflect($controller, $methodName);

			$this->middlewareDispatcher->beforeController($controller,
				$methodName);

			$databaseStatsBefore = [];
			if ($this->config->getSystemValueBool('debug', false)) {
				$databaseStatsBefore = $this->connection->getInner()->getStats();
			}

			$response = $this->executeController($controller, $methodName);

			if (!empty($databaseStatsBefore)) {
				$databaseStatsAfter = $this->connection->getInner()->getStats();
				$numBuilt = $databaseStatsAfter['built'] - $databaseStatsBefore['built'];
				$numExecuted = $databaseStatsAfter['executed'] - $databaseStatsBefore['executed'];

				if ($numBuilt > 50) {
					$this->logger->debug('Controller {class}::{method} created {count} QueryBuilder objects, please check if they are created inside a loop by accident.' , [
						'class' => get_class($controller),
						'method' => $methodName,
						'count' => $numBuilt,
					]);
				}

				if ($numExecuted > 100) {
					$this->logger->warning('Controller {class}::{method} executed {count} queries.' , [
						'class' => get_class($controller),
						'method' => $methodName,
						'count' => $numExecuted,
					]);
				}
			}

			// if an exception appears, the middleware checks if it can handle the
			// exception and creates a response. If no response is created, it is
			// assumed that theres no middleware who can handle it and the error is
			// thrown again
		} catch (\Exception $exception) {
			$response = $this->middlewareDispatcher->afterException(
				$controller, $methodName, $exception);
		} catch (\Throwable $throwable) {
			$exception = new \Exception($throwable->getMessage(), $throwable->getCode(), $throwable);
			$response = $this->middlewareDispatcher->afterException(
			$controller, $methodName, $exception);
		}

		$response = $this->middlewareDispatcher->afterController(
			$controller, $methodName, $response);

		// depending on the cache object the headers need to be changed
		$out[0] = $this->protocol->getStatusHeader($response->getStatus());
		$out[1] = array_merge($response->getHeaders());
		$out[2] = $response->getCookies();
		$out[3] = $this->middlewareDispatcher->beforeOutput(
			$controller, $methodName, $response->render()
		);
		$out[4] = $response;

		return $out;
	}


	/**
	 * Uses the reflected parameters, types and request parameters to execute
	 * the controller
	 * @param Controller $controller the controller to be executed
	 * @param string $methodName the method on the controller that should be executed
	 * @return Response
	 */
	private function executeController(Controller $controller, string $methodName): Response {
		$arguments = [];<commit_msg>Improve syntax error reporting<commit_after><?php

declare(strict_types=1);

/**
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 */
namespace OC\AppFramework\Http;

use OC\AppFramework\Http;
use OC\AppFramework\Middleware\MiddlewareDispatcher;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OC\DB\ConnectionAdapter;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\IConfig;
use OCP\IRequest;
use Psr\Log\LoggerInterface;

/**
 * Class to dispatch the request to the middleware dispatcher
 */
class Dispatcher {

	/** @var MiddlewareDispatcher */
	private $middlewareDispatcher;

	/** @var Http */
	private $protocol;

	/** @var ControllerMethodReflector */
	private $reflector;

	/** @var IRequest */
	private $request;

	/** @var IConfig */
	private $config;

	/** @var ConnectionAdapter */
	private $connection;

	/** @var LoggerInterface */
	private $logger;

	/**
	 * @param Http $protocol the http protocol with contains all status headers
	 * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which
	 * runs the middleware
	 * @param ControllerMethodReflector $reflector the reflector that is used to inject
	 * the arguments for the controller
	 * @param IRequest $request the incoming request
	 * @param IConfig $config
	 * @param ConnectionAdapter $connection
	 * @param LoggerInterface $logger
	 */
	public function __construct(Http $protocol,
								MiddlewareDispatcher $middlewareDispatcher,
								ControllerMethodReflector $reflector,
								IRequest $request,
								IConfig $config,
								ConnectionAdapter $connection,
								LoggerInterface $logger) {
		$this->protocol = $protocol;
		$this->middlewareDispatcher = $middlewareDispatcher;
		$this->reflector = $reflector;
		$this->request = $request;
		$this->config = $config;
		$this->connection = $connection;
		$this->logger = $logger;
	}


	/**
	 * Handles a request and calls the dispatcher on the controller
	 * @param Controller $controller the controller which will be called
	 * @param string $methodName the method name which will be called on
	 * the controller
	 * @return array $array[0] contains a string with the http main header,
	 * $array[1] contains headers in the form: $key => value, $array[2] contains
	 * the response output
	 * @throws \Exception
	 */
	public function dispatch(Controller $controller, string $methodName): array {
		$out = [null, [], null];

		try {
			// prefill reflector with everything thats needed for the
			// middlewares
			$this->reflector->reflect($controller, $methodName);

			$this->middlewareDispatcher->beforeController($controller,
				$methodName);

			$databaseStatsBefore = [];
			if ($this->config->getSystemValueBool('debug', false)) {
				$databaseStatsBefore = $this->connection->getInner()->getStats();
			}

			$response = $this->executeController($controller, $methodName);

			if (!empty($databaseStatsBefore)) {
				$databaseStatsAfter = $this->connection->getInner()->getStats();
				$numBuilt = $databaseStatsAfter['built'] - $databaseStatsBefore['built'];
				$numExecuted = $databaseStatsAfter['executed'] - $databaseStatsBefore['executed'];

				if ($numBuilt > 50) {
					$this->logger->debug('Controller {class}::{method} created {count} QueryBuilder objects, please check if they are created inside a loop by accident.' , [
						'class' => get_class($controller),
						'method' => $methodName,
						'count' => $numBuilt,
					]);
				}

				if ($numExecuted > 100) {
					$this->logger->warning('Controller {class}::{method} executed {count} queries.' , [
						'class' => get_class($controller),
						'method' => $methodName,
						'count' => $numExecuted,
					]);
				}
			}

			// if an exception appears, the middleware checks if it can handle the
			// exception and creates a response. If no response is created, it is
			// assumed that theres no middleware who can handle it and the error is
			// thrown again
		} catch (\Exception $exception) {
			$response = $this->middlewareDispatcher->afterException(
				$controller, $methodName, $exception);
		} catch (\Throwable $throwable) {
			$exception = new \Exception($throwable->getMessage() . ' in file \'' . $throwable->getFile() . '\' line ' . $throwable->getLine(), $throwable->getCode(), $throwable);
			$response = $this->middlewareDispatcher->afterException(
			$controller, $methodName, $exception);
		}

		$response = $this->middlewareDispatcher->afterController(
			$controller, $methodName, $response);

		// depending on the cache object the headers need to be changed
		$out[0] = $this->protocol->getStatusHeader($response->getStatus());
		$out[1] = array_merge($response->getHeaders());
		$out[2] = $response->getCookies();
		$out[3] = $this->middlewareDispatcher->beforeOutput(
			$controller, $methodName, $response->render()
		);
		$out[4] = $response;

		return $out;
	}


	/**
	 * Uses the reflected parameters, types and request parameters to execute
	 * the controller
	 * @param Controller $controller the controller to be executed
	 * @param string $methodName the method on the controller that should be executed
	 * @return Response
	 */
	private function executeController(Controller $controller, string $methodName): Response {
		$arguments = [];
{'fraction_non_alphanumeric': 0.12813824584284317, 'fraction_numerical': 0.0035865666775350504, 'mean_word_length': 4.101455301455301, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 159, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5553718', 'n_tokens_mistral': 3883, 'n_tokens_neox': 3385, 'n_words': 1098}