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

Commit 93e1db2

Browse files
authored
Merge pull request #15 from bstoney/master
Added support for Windows
2 parents bb700cd + 99c24c7 commit 93e1db2

File tree

6 files changed

+100
-19
lines changed

6 files changed

+100
-19
lines changed

src/JupyterBroker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function send(
7777
);
7878

7979
if (null !== $this->logger) {
80-
$this->logger->debug('Sent message', ['processId' => posix_getpid(), 'message' => $finalMsg]);
80+
$this->logger->debug('Sent message', ['processId' => getmypid(), 'message' => $finalMsg]);
8181
}
8282

8383
$stream->send($finalMsg);

src/System/BsdSystem.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,4 @@ public function getOperativeSystem()
1111
{
1212
return self::OS_BSD;
1313
}
14-
15-
/** @return string */
16-
public function getAppDataDirectory()
17-
{
18-
return $this->getCurrentUserHome().'/.jupyter-php';
19-
}
2014
}

src/System/LinuxSystem.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,4 @@ public function getOperativeSystem()
1111
{
1212
return self::OS_LINUX;
1313
}
14-
15-
/** @return string */
16-
public function getAppDataDirectory()
17-
{
18-
return $this->getCurrentUserHome().'/.jupyter-php';
19-
}
2014
}

src/System/System.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ public abstract function getCurrentUser();
4040
/** @return string */
4141
public abstract function getCurrentUserHome();
4242

43-
/** @return string */
43+
/**
44+
* @param string $cmdName
45+
* @return boolean
46+
*/
47+
public abstract function checkIfCommandExists($cmdName);
48+
49+
/** @return string */
4450
public abstract function getAppDataDirectory();
4551

4652
/**

src/System/UnixSystem.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ public function getCurrentUserHome()
3232
}
3333
}
3434

35+
/**
36+
* @param string $cmdName
37+
* @return boolean
38+
*/
39+
public function checkIfCommandExists($cmdName)
40+
{
41+
if (!function_exists('exec')) {
42+
return false;
43+
}
44+
45+
$sysResponse = exec(
46+
'PATH='.getenv('PATH').'; '.
47+
"if command -v ".$cmdName." >/dev/null 2>&1; then echo \"true\"; else echo \"false\"; fi;"
48+
);
49+
50+
return filter_var($sysResponse, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
51+
}
52+
53+
/** @return string */
54+
public function getAppDataDirectory()
55+
{
56+
return $this->getCurrentUserHome().'/.jupyter-php';
57+
}
58+
3559
/**
3660
* Returns true if the path is a "valid" path and is writable (even if the complete path does not yet exist).
3761
* @param string $path
@@ -41,7 +65,7 @@ public function validatePath($path)
4165
{
4266
$absPath = $this->getAbsolutePath($path);
4367
$absPathParts = preg_split('/\//', preg_replace('/(^\/|\/$)/', '', $absPath));
44-
$nSteps = count($absPath);
68+
$nSteps = count($absPathParts);
4569

4670
$tmpPath = '';
4771
$prevReadable = false;

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)