@@ -33,14 +33,66 @@ public function getCurrentUserHome()
33
33
}
34
34
}
35
35
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
+
36
57
/**
37
58
* Returns true if the path is a "valid" path and is writable (event if the complete path does not yet exist).
38
59
* @param string $path
39
60
* @return boolean
40
61
*/
41
62
public function validatePath ($ path )
42
63
{
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 ;
44
96
}
45
97
46
98
/**
@@ -49,7 +101,13 @@ public function validatePath($path)
49
101
*/
50
102
public function ensurePath ($ path )
51
103
{
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 ;
53
111
}
54
112
55
113
/**
@@ -58,7 +116,7 @@ public function ensurePath($path)
58
116
*/
59
117
protected function isAbsolutePath ($ path )
60
118
{
61
- // TODO: Implement isAbsolutePath() method.
119
+ return preg_match ( ' /^[a-z]\:/i ' , $ path ) === 1 ;
62
120
}
63
121
64
122
/**
@@ -67,6 +125,11 @@ protected function isAbsolutePath($path)
67
125
*/
68
126
protected function getAbsolutePath ($ path )
69
127
{
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 ;
71
134
}
72
135
}
0 commit comments