]> git.mxchange.org Git - friendica.git/blobdiff - src/App/Router.php
Merge pull request #10409 from annando/api-link-header
[friendica.git] / src / App / Router.php
index dfe890fb968809fc0fc429aadf48576fd073a088..82c493baa6b66db97f4fc40dd47bcda98a79596e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -44,12 +44,20 @@ use Friendica\Network\HTTPException;
  */
 class Router
 {
-       const POST = 'POST';
-       const GET  = 'GET';
+       const DELETE  = 'DELETE';
+       const GET     = 'GET';
+       const PATCH   = 'PATCH';
+       const POST    = 'POST';
+       const PUT     = 'PUT';
+       const OPTIONS = 'OPTIONS';
 
        const ALLOWED_METHODS = [
-               self::POST,
+               self::DELETE,
                self::GET,
+               self::PATCH,
+               self::POST,
+               self::PUT,
+               self::OPTIONS
        ];
 
        /** @var RouteCollector */
@@ -93,6 +101,10 @@ class Router
                $this->routeCollector = isset($routeCollector) ?
                        $routeCollector :
                        new RouteCollector(new Std(), new GroupCountBased());
+
+               if ($this->baseRoutesFilepath && !file_exists($this->baseRoutesFilepath)) {
+                       throw new HTTPException\InternalServerErrorException('Routes file path does\'n exist.');
+               }
        }
 
        /**
@@ -249,7 +261,7 @@ class Router
        {
                $dispatchData = [];
 
-               if ($this->baseRoutesFilepath && file_exists($this->baseRoutesFilepath)) {
+               if ($this->baseRoutesFilepath) {
                        $dispatchData = require $this->baseRoutesFilepath;
                        if (!is_array($dispatchData)) {
                                throw new HTTPException\InternalServerErrorException('Invalid base routes file');
@@ -268,20 +280,33 @@ class Router
         * The cached "routerDispatchData" lasts for a day, and must be cleared manually when there
         * is any changes in the enabled addons list.
         *
+        * Additionally, we check for the base routes file last modification time to automatically
+        * trigger re-computing the dispatch data.
+        *
         * @return array|mixed
         * @throws HTTPException\InternalServerErrorException
         */
        private function getCachedDispatchData()
        {
                $routerDispatchData = $this->cache->get('routerDispatchData');
+               $lastRoutesFileModifiedTime = $this->cache->get('lastRoutesFileModifiedTime');
+               $forceRecompute = false;
+
+               if ($this->baseRoutesFilepath) {
+                       $routesFileModifiedTime = filemtime($this->baseRoutesFilepath);
+                       $forceRecompute = $lastRoutesFileModifiedTime != $routesFileModifiedTime;
+               }
 
-               if ($routerDispatchData) {
+               if (!$forceRecompute && $routerDispatchData) {
                        return $routerDispatchData;
                }
 
                $routerDispatchData = $this->getDispatchData();
 
                $this->cache->set('routerDispatchData', $routerDispatchData, Duration::DAY);
+               if (!empty($routesFileModifiedTime)) {
+                       $this->cache->set('lastRoutesFileMtime', $routesFileModifiedTime, Duration::MONTH);
+               }
 
                return $routerDispatchData;
        }