|
| 1 | +<?php |
| 2 | + |
| 3 | +class Magento2ValetDriver extends ValetDriver |
| 4 | +{ |
| 5 | + |
| 6 | + /** |
| 7 | + * Holds the MAGE_MODE from app/etc/config.php or $ENV |
| 8 | + * |
| 9 | + * @var string |
| 10 | + */ |
| 11 | + private $mageMode; |
| 12 | + |
| 13 | + /** |
| 14 | + * Determine if the driver serves the request. |
| 15 | + * |
| 16 | + * @param string $sitePath |
| 17 | + * @param string $siteName |
| 18 | + * @param string $uri |
| 19 | + * @return boolean |
| 20 | + */ |
| 21 | + public function serves($sitePath, $siteName, $uri) |
| 22 | + { |
| 23 | + return file_exists($sitePath . '/bin/magento') && file_exists($sitePath . '/pub/index.php'); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Determine if the incoming request is for a static file. |
| 28 | + * |
| 29 | + * @param string $sitePath |
| 30 | + * @param string $siteName |
| 31 | + * @param string $uri |
| 32 | + * @return string|false |
| 33 | + */ |
| 34 | + public function isStaticFile($sitePath, $siteName, $uri) |
| 35 | + { |
| 36 | + $this->checkMageMode($sitePath); |
| 37 | + |
| 38 | + $uri = $this->handleForVersions($uri); |
| 39 | + $route = parse_url(substr($uri, 1))['path']; |
| 40 | + |
| 41 | + $pub = ''; |
| 42 | + if ('developer' === $this->mageMode) { |
| 43 | + $pub = 'pub/'; |
| 44 | + } |
| 45 | + |
| 46 | + if (!$this->isPubDirectory($sitePath, $route, $pub)) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + $magentoPackagePubDir = $sitePath; |
| 51 | + if ('developer' !== $this->mageMode) { |
| 52 | + $magentoPackagePubDir .= '/pub'; |
| 53 | + } |
| 54 | + |
| 55 | + $file = $magentoPackagePubDir . '/' . $route; |
| 56 | + |
| 57 | + if (file_exists($file)) { |
| 58 | + return $magentoPackagePubDir . $uri; |
| 59 | + } |
| 60 | + |
| 61 | + if (strpos($route, $pub . 'static/') === 0) { |
| 62 | + $route = preg_replace('#' . $pub . 'static/#', '', $route, 1); |
| 63 | + $_GET['resource'] = $route; |
| 64 | + include $magentoPackagePubDir . '/' . $pub . 'static.php'; |
| 65 | + exit; |
| 66 | + } |
| 67 | + |
| 68 | + if (strpos($route, $pub . 'media/') === 0) { |
| 69 | + include $magentoPackagePubDir . '/' . $pub . 'get.php'; |
| 70 | + exit; |
| 71 | + } |
| 72 | + |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Rewrite URLs that look like "versions12345/" to remove |
| 78 | + * the versions12345/ part |
| 79 | + * |
| 80 | + * @param string $route |
| 81 | + */ |
| 82 | + private function handleForVersions($route) |
| 83 | + { |
| 84 | + return preg_replace('/version\d*\//', '', $route); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Determine the current MAGE_MODE |
| 89 | + * |
| 90 | + * @param string $sitePath |
| 91 | + */ |
| 92 | + private function checkMageMode($sitePath) |
| 93 | + { |
| 94 | + if (null !== $this->mageMode) { |
| 95 | + // We have already figure out mode, no need to check it again |
| 96 | + return; |
| 97 | + } |
| 98 | + if (!file_exists($sitePath . '/index.php')) { |
| 99 | + $this->mageMode = 'production'; // Can't use developer mode without index.php in project root |
| 100 | + return; |
| 101 | + } |
| 102 | + $mageConfig = []; |
| 103 | + if (file_exists($sitePath . '/app/etc/env.php')) { |
| 104 | + $mageConfig = require $sitePath . '/app/etc/env.php'; |
| 105 | + } |
| 106 | + if (array_key_exists('MAGE_MODE', $mageConfig)) { |
| 107 | + $this->mageMode = $mageConfig['MAGE_MODE']; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Checks to see if route is referencing any directory inside pub. This is a dynamic check so that if any new |
| 113 | + * directories are added to pub this driver will not need to be updated. |
| 114 | + * |
| 115 | + * @param string $sitePath |
| 116 | + * @param string $route |
| 117 | + * @param string $pub |
| 118 | + * @return bool |
| 119 | + */ |
| 120 | + private function isPubDirectory($sitePath, $route, $pub = '') |
| 121 | + { |
| 122 | + $sitePath .= '/pub/'; |
| 123 | + $dirs = glob($sitePath . '*', GLOB_ONLYDIR); |
| 124 | + |
| 125 | + $dirs = str_replace($sitePath, '', $dirs); |
| 126 | + foreach ($dirs as $dir) { |
| 127 | + if (strpos($route, $pub . $dir . '/') === 0) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + } |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Get the fully resolved path to the application's front controller. |
| 136 | + * |
| 137 | + * @param string $sitePath |
| 138 | + * @param string $siteName |
| 139 | + * @param string $uri |
| 140 | + * @return string |
| 141 | + */ |
| 142 | + public function frontControllerPath($sitePath, $siteName, $uri) |
| 143 | + { |
| 144 | + $this->checkMageMode($sitePath); |
| 145 | + |
| 146 | + if ('developer' === $this->mageMode) { |
| 147 | + return $sitePath . '/index.php'; |
| 148 | + } |
| 149 | + return $sitePath . '/pub/index.php'; |
| 150 | + } |
| 151 | +} |
0 commit comments