-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,48 @@ | ||
<?php | ||
|
||
$version = explode('?', explode('/', $_SERVER['REQUEST_URI'])[1])[0]; | ||
$route = __DIR__ . '/../src/' . $version . '/route.php'; | ||
$requestedVersion = explode('?', explode('/', $_SERVER['REQUEST_URI'])[1])[0]; | ||
$src = __DIR__ . '/../src/'; | ||
|
||
if (is_readable($route)) { | ||
require_once $route; | ||
} else { | ||
echo 'Invalid version. Try `/latest`'; | ||
// Try direct route with the requested version | ||
if (!empty($requestedVersion)) { | ||
$route = $src . $requestedVersion . '/route.php'; | ||
if (is_readable($route)) { | ||
require_once $route; | ||
die; | ||
} | ||
} | ||
|
||
// Loop through these `src` folder and look for folder names which would be an indicative | ||
// of which versions available currently | ||
$availableVersions = []; | ||
foreach (scandir($src) as $result) { | ||
if ($result === '.' or $result === '..') continue; | ||
if (is_dir($src . '/' . $result)) { | ||
$availableVersions[] = $result; | ||
} | ||
} | ||
|
||
// Try to look if we have `rc` tags for the requested version (naive implementation) | ||
// This should only be used in staging. ie. look for `v0.1.0-rc.3` for `v0.1.0` | ||
if (!empty($requestedVersion)) { | ||
// Reverse sort versions array so the latest version would stay top of the array | ||
// Then, do a simple string matching to grab be first one | ||
rsort($availableVersions, SORT_STRING); | ||
foreach ($availableVersions as $version) { | ||
$containsRc = strpos($version, '-rc.') !== false; | ||
$firstPartEqualsToRequestedVersion = explode('-', $version)[0] === $requestedVersion; | ||
if ($containsRc && $firstPartEqualsToRequestedVersion) { | ||
$route = $src . $version . '/route.php'; | ||
if (is_readable($route)) { | ||
require_once $route; | ||
die; | ||
} | ||
} | ||
} | ||
} | ||
|
||
echo json_encode([ | ||
'message' => 'Invalid version. Try `latest` or other available versions', | ||
'versions' => $availableVersions | ||
]); | ||
die; |