Skip to content

Commit 953d828

Browse files
lazyguruadamwathan
authored andcommitted
Add Magento2 driver
1 parent 4743c11 commit 953d828

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
$route = parse_url(substr($uri, 1))['path'];
39+
40+
$pub = '';
41+
if ('developer' === $this->mageMode) {
42+
$pub = 'pub/';
43+
}
44+
45+
if (!$this->isPubDirectory($sitePath, $route, $pub)) {
46+
return false;
47+
}
48+
49+
$magentoPackagePubDir = $sitePath;
50+
if ('developer' !== $this->mageMode) {
51+
$magentoPackagePubDir .= '/pub';
52+
}
53+
54+
$file = $magentoPackagePubDir . '/' . $route;
55+
56+
if (file_exists($file)) {
57+
return $magentoPackagePubDir . $uri;
58+
}
59+
60+
if (strpos($route, $pub . 'static/') === 0) {
61+
$route = preg_replace('#' . $pub . 'static/#', '', $route, 1);
62+
$_GET['resource'] = $route;
63+
include $magentoPackagePubDir . '/' . $pub . 'static.php';
64+
exit;
65+
}
66+
67+
if (strpos($route, $pub . 'media/') === 0) {
68+
include $magentoPackagePubDir . '/' . $pub . 'get.php';
69+
exit;
70+
}
71+
72+
return false;
73+
}
74+
75+
/**
76+
* Determine the current MAGE_MODE
77+
*
78+
* @param string $sitePath
79+
*/
80+
private function checkMageMode($sitePath)
81+
{
82+
if (null !== $this->mageMode) {
83+
// We have already figure out mode, no need to check it again
84+
return;
85+
}
86+
if (!file_exists($sitePath . '/index.php')) {
87+
$this->mageMode = 'production'; // Can't use developer mode without index.php in project root
88+
return;
89+
}
90+
$mageConfig = [];
91+
if (file_exists($sitePath . '/app/etc/env.php')) {
92+
$mageConfig = require $sitePath . '/app/etc/env.php';
93+
}
94+
if (array_key_exists('MAGE_MODE', $mageConfig)) {
95+
$this->mageMode = $mageConfig['MAGE_MODE'];
96+
}
97+
}
98+
99+
/**
100+
* Checks to see if route is referencing any directory inside pub. This is a dynamic check so that if any new
101+
* directories are added to pub this driver will not need to be updated.
102+
*
103+
* @param string $sitePath
104+
* @param string $route
105+
* @param string $pub
106+
* @return bool
107+
*/
108+
private function isPubDirectory($sitePath, $route, $pub = '')
109+
{
110+
$sitePath .= '/pub/';
111+
$dirs = glob($sitePath . '*', GLOB_ONLYDIR);
112+
113+
$dirs = str_replace($sitePath, '', $dirs);
114+
foreach ($dirs as $dir) {
115+
if (strpos($route, $pub . $dir . '/') === 0) {
116+
return true;
117+
}
118+
}
119+
return false;
120+
}
121+
122+
/**
123+
* Get the fully resolved path to the application's front controller.
124+
*
125+
* @param string $sitePath
126+
* @param string $siteName
127+
* @param string $uri
128+
* @return string
129+
*/
130+
public function frontControllerPath($sitePath, $siteName, $uri)
131+
{
132+
$this->checkMageMode($sitePath);
133+
134+
if ('developer' === $this->mageMode) {
135+
return $sitePath . '/index.php';
136+
}
137+
return $sitePath . '/pub/index.php';
138+
}
139+
}

cli/drivers/ValetDriver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static function assign($sitePath, $siteName, $uri)
6767
$drivers[] = 'JoomlaValetDriver';
6868
$drivers[] = 'DrupalValetDriver';
6969
$drivers[] = 'Concrete5ValetDriver';
70+
$drivers[] = 'Magento2ValetDriver';
7071

7172
$drivers[] = 'BasicValetDriver';
7273

cli/drivers/require.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
require_once __DIR__.'/JoomlaValetDriver.php';
2626
require_once __DIR__.'/DrupalValetDriver.php';
2727
require_once __DIR__.'/Concrete5ValetDriver.php';
28+
require_once __DIR__.'/Magento2ValetDriver.php';

0 commit comments

Comments
 (0)