/** * @see \Fine47\MicroRouter\Responses\Http::serveImpl() */ protected function serveImpl() { // Create a temporary file to hold the output buffer. $tempPath = System::tempFile(); try { // Save output buffer as JSON. Assert::that(['condition' => false !== file_put_contents($tempPath, Json::encode($this->getBody())), 'message' => 'Unable to serve the response body', 'type' => Error::TYPE_RUNTIME, 'magic' => 0xeb73]); // Close the file handle and outputs its contents. Assert::that(['condition' => false !== readfile($tempPath, false), 'message' => 'Unable to serve the response body', 'type' => Error::TYPE_RUNTIME, 'magic' => 0xeb74]); } finally { // Deletes temporary file. @unlink($tempPath); } // Once the body is served, it cannot be re-served again. $this->shutdown(); }
/** * Returns a list of filters for the action at the specified script path. * Search for filters starts at $routePath and ends at $scriptPath, so filters * are first found on the top level directory and ends at the deepest path. * * @param string $routePath top level directory to start searching from * @param string $matchUri farthest path to traverse * @param \Fine47\MicroRouter\Interfaces\Query $query for the action * @return array list of filters found, an empty array otherwise */ protected function getActionFilters($routePath, $matchUri, Interfaces\Query $query) { $filters = []; // Get the name of PHP script that should include the filters. $filtersScriptName = $this->getFilterScriptName($query); // Start searching from top directory. $filtersScriptPath = $routePath; // Sub-directories to dig into. $subDirectories = explode('/', $matchUri); // Add also the top directory. array_unshift($subDirectories, ''); // Cycle through the route path and look for filters starting at top level. while (!empty($subDirectories)) { // Go to the next sub-directory. $filtersScriptPath .= '/' . array_shift($subDirectories); // Construct path to possible PHP script. $filtersScript = $filtersScriptPath . '/' . $filtersScriptName; // Does it exist? if (System::isFile($filtersScript)) { // Read it. $filtersList = (require_once $filtersScript); // Result should be an array. if (is_array($filtersList) && !empty($filtersList)) { foreach ($filtersList as $filterName => $filter) { if (!isset($filters[$filterName]) && $filter instanceof Interfaces\Filter) { $filters[$filterName] = $filter; } } } // GC. unset($filtersList); } } return $filters; }
/** * @see \Fine47\MicroRouter\Loggers\ErrorLog::shutdown() */ public function shutdown() { parent::shutdown(); System::closeFile($this->fileHandle); $this->fileHandle = null; }