Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit f77b6d7

Browse files
committed
Add support for Windows system
1 parent a0549c2 commit f77b6d7

File tree

1 file changed

+67
-4
lines changed

1 file changed

+67
-4
lines changed

src/System/WindowsSystem.php

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,66 @@ public function getCurrentUserHome()
3333
}
3434
}
3535

36+
/**
37+
* @param string $cmdName
38+
* @return boolean
39+
*/
40+
public function checkIfCommandExists($cmdName)
41+
{
42+
if (!function_exists('exec')) {
43+
return false;
44+
}
45+
46+
$sysResponse = exec("where $cmdName > nul 2>&1 && echo true");
47+
48+
return filter_var($sysResponse, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
49+
}
50+
51+
/** @return string */
52+
public function getAppDataDirectory()
53+
{
54+
return $this->getCurrentUserHome() . '/.jupyter-php';
55+
}
56+
3657
/**
3758
* Returns true if the path is a "valid" path and is writable (event if the complete path does not yet exist).
3859
* @param string $path
3960
* @return boolean
4061
*/
4162
public function validatePath($path)
4263
{
43-
// TODO: Implement validatePath() method.
64+
$absPath = $this->getAbsolutePath($path);
65+
$absPathParts = explode(DIRECTORY_SEPARATOR, $absPath);
66+
$nSteps = count($absPathParts);
67+
68+
$tmpPath = $absPathParts[0];
69+
$prevReadable = false;
70+
$prevWritable = false;
71+
72+
for ($i = 1; $i < $nSteps; $i++) {
73+
$tmpPath .= DIRECTORY_SEPARATOR . $absPathParts[$i];
74+
75+
if (file_exists($tmpPath)) {
76+
if (!is_dir($tmpPath)) {
77+
if (is_link($tmpPath)) {
78+
$linkPath = readlink($tmpPath);
79+
if (false === $linkPath || !is_dir($linkPath)) {
80+
return false;
81+
}
82+
$tmpPath = $linkPath;
83+
} else {
84+
return false;
85+
}
86+
}
87+
88+
$prevReadable = is_readable($tmpPath);
89+
$prevWritable = is_writable($tmpPath);
90+
} else {
91+
return ($prevReadable && $prevWritable);
92+
}
93+
}
94+
95+
return true;
4496
}
4597

4698
/**
@@ -49,7 +101,13 @@ public function validatePath($path)
49101
*/
50102
public function ensurePath($path)
51103
{
52-
// TODO: Implement ensurePath() method.
104+
$absPath = $this->getAbsolutePath($path);
105+
106+
if (!file_exists($absPath) && false === mkdir($absPath, 0755, true)) {
107+
throw new \RuntimeException('Unable to create the specified directory (' . $absPath . ').');
108+
}
109+
110+
return $absPath;
53111
}
54112

55113
/**
@@ -58,7 +116,7 @@ public function ensurePath($path)
58116
*/
59117
protected function isAbsolutePath($path)
60118
{
61-
// TODO: Implement isAbsolutePath() method.
119+
return preg_match('/^[a-z]\:/i', $path) === 1;
62120
}
63121

64122
/**
@@ -67,6 +125,11 @@ protected function isAbsolutePath($path)
67125
*/
68126
protected function getAbsolutePath($path)
69127
{
70-
// TODO: Implement getAbsolutePath() method.
128+
$path = $this->isAbsolutePath($path) ? $path : (getcwd() . DIRECTORY_SEPARATOR . $path);
129+
130+
// Normalise directory separators
131+
$path = preg_replace('/[\/\\\\]/u', DIRECTORY_SEPARATOR, $path);
132+
133+
return $path;
71134
}
72135
}

0 commit comments

Comments
 (0)