@@ -18,11 +18,7 @@ abstract class Installer
18
18
/** @var string */
19
19
protected $ composerCmd ;
20
20
21
- /**
22
- * @param null|string $composerCmd
23
- * @return Installer
24
- */
25
- public static function getInstaller ($ composerCmd = null )
21
+ public static function getInstaller (string $ composerCmd = null ): Installer
26
22
{
27
23
$ system = System::getSystem ();
28
24
$ composerCmd = (null !== $ composerCmd ) ? $ composerCmd : $ system ->getComposerCommand ();
@@ -45,11 +41,7 @@ public static function getInstaller($composerCmd = null)
45
41
}
46
42
}
47
43
48
- /**
49
- * @param null|string $installPath
50
- * @param bool $beVerbose
51
- */
52
- public function install ($ installPath = null , $ beVerbose = false )
44
+ public function install (string $ installPath = null , bool $ beVerbose = false )
53
45
{
54
46
$ installPath = (null !== $ installPath ) ? $ installPath : $ this ->getInstallPath ();
55
47
if (null !== $ installPath && !$ this ->system ->validatePath ($ installPath )) {
@@ -62,70 +54,141 @@ public function install($installPath = null, $beVerbose = false)
62
54
$ this ->installKernel ();
63
55
}
64
56
65
- /**
66
- * @return string
67
- */
68
- protected abstract function getInstallPath ();
69
-
70
- /**
71
- *
72
- */
73
- protected abstract function installKernel ();
74
-
75
- /**
76
- * @param $installPath
77
- * @return mixed
78
- */
79
- protected abstract function executeSilentComposerCommand ($ installPath );
80
-
81
- /**
82
- * @param string $installPath
83
- * @param bool $beVerbose
84
- */
85
- protected function executeComposerCommand ($ installPath , $ beVerbose = false )
57
+ protected function getInstallPath (): string
58
+ {
59
+ return ($ this ->system ->isRunningAsAdmin ())
60
+ ? $ this ->getAdminInstallPath ()
61
+ : $ this ->getUserInstallPath ();
62
+ }
63
+
64
+ protected abstract function getAdminInstallPath (): string ;
65
+
66
+ protected abstract function getUserInstallPath (): string ;
67
+
68
+ protected function installKernel ()
69
+ {
70
+ $ kernelDef = json_encode ([
71
+ 'argv ' => [
72
+ 'php ' ,
73
+ $ this ->getKernelEntrypointPath (),
74
+ '{connection_file} '
75
+ ],
76
+ 'display_name ' => 'PHP ' ,
77
+ 'language ' => 'php ' ,
78
+ 'env ' => new \stdClass
79
+ ]);
80
+
81
+ $ kernelSpecPath = ($ this ->system ->isRunningAsAdmin ())
82
+ ? $ this ->getJupyterKernelsMetadataAdminPath ()
83
+ : $ this ->getJupyterKernelsMetadatUserPath ();
84
+
85
+ $ this ->system ->ensurePath ($ kernelSpecPath );
86
+ file_put_contents ($ kernelSpecPath .'/kernel.json ' , $ kernelDef );
87
+ }
88
+
89
+ protected abstract function getKernelEntryPointPath (): string ;
90
+
91
+ protected abstract function getJupyterKernelsMetadataAdminPath (): string ;
92
+
93
+ protected abstract function getJupyterKernelsMetadatUserPath (): string ;
94
+
95
+ protected function executeComposerCommand (string $ installPath , bool $ beVerbose = false )
86
96
{
87
97
$ composerStatus = 0 ;
88
98
89
99
$ pkgsDir = $ installPath .DIRECTORY_SEPARATOR .'pkgs ' ;
90
- if (file_exists ($ pkgsDir )) {
91
- foreach (
92
- new \RecursiveIteratorIterator (
93
- new \RecursiveDirectoryIterator ($ pkgsDir , \FilesystemIterator::SKIP_DOTS ),
94
- \RecursiveIteratorIterator::CHILD_FIRST
95
- ) as $ path
96
- ) {
97
- $ path ->isDir () && !$ path ->isLink () ? rmdir ($ path ->getPathname ()) : unlink ($ path ->getPathname ());
98
- }
99
- rmdir ($ pkgsDir );
100
- }
100
+ $ this ->preparePackagesDir ($ pkgsDir );
101
101
102
102
if ($ beVerbose ) {
103
103
echo "\n" ;
104
104
passthru (
105
- 'PATH= ' . getenv ('PATH ' ) . ' && ' .
106
- $ this ->composerCmd . ' --prefer-dist --no-interaction --working-dir=" ' .
107
- $ installPath .'" create-project litipk/jupyter-php=0.* pkgs ' ,
105
+ $ this ->system ->wrapCommandToAttachEnvironmentVariable (
106
+ 'PATH ' , getenv ('PATH ' ),
107
+ $ this ->getComposerInitCommand ($ pkgsDir ) . ' && ' .
108
+ $ this ->getComposerInstallCommand ($ pkgsDir )
109
+ ),
108
110
109
111
$ composerStatus
110
112
);
111
113
echo "\n" ;
112
114
} else {
113
- $ composerStatus = $ this ->executeSilentComposerCommand ($ installPath );
115
+ $ composerStatus = $ this ->executeSilentComposerCommand ($ pkgsDir );
114
116
}
115
117
116
- if ($ composerStatus !== 0 ) {
118
+ if (0 !== $ composerStatus ) {
117
119
throw new \RuntimeException ('Error while trying to download Jupyter-PHP dependencies with Composer. ' );
118
120
}
119
121
}
120
122
121
- /**
122
- * Installer constructor.
123
- * @param System $system
124
- * @param string $composerCmd
125
- */
126
- protected function __construct (System $ system , $ composerCmd )
123
+ protected function executeSilentComposerCommand (string $ pkgsDir )
124
+ {
125
+ $ composerOutputLines = [];
126
+
127
+ exec (
128
+ $ this ->system ->wrapCommandToAttachEnvironmentVariable (
129
+ 'PATH ' , getenv ('PATH ' ),
130
+ $ this ->getComposerInitCommand ($ pkgsDir , true ) . ' && ' .
131
+ $ this ->getComposerInstallCommand ($ pkgsDir , true )
132
+ ),
133
+
134
+ $ composerOutputLines ,
135
+ $ composerStatus
136
+ );
137
+
138
+ return $ composerStatus ;
139
+ }
140
+
141
+ private function getComposerInitCommand (string $ pkgsDir , bool $ silent = false ): string
142
+ {
143
+ $ cmd = (
144
+ $ this ->composerCmd . ' init ' .
145
+ ' --no-interaction ' .
146
+ ' --name=jupyter-php-instance ' .
147
+ ' --type=project ' .
148
+ ' --working-dir=" ' . $ pkgsDir . '" ' .
149
+ ' --require=litipk/jupyter-php=0.* '
150
+ );
151
+
152
+ return ($ silent )
153
+ ? $ this ->system ->wrapCommandToNullifyItsOutput ($ cmd )
154
+ : $ cmd ;
155
+ }
156
+
157
+ private function getComposerInstallCommand (string $ pkgsDir , bool $ silent = false ): string
158
+ {
159
+ $ cmd = (
160
+ $ this ->composerCmd . ' install ' .
161
+ ' --no-interaction ' .
162
+ ' --no-progress ' .
163
+ ' --prefer-dist ' .
164
+ ' --optimize-autoloader ' .
165
+ ' --working-dir=" ' . $ pkgsDir . '" '
166
+ );
167
+
168
+ return ($ silent )
169
+ ? $ this ->system ->wrapCommandToNullifyItsOutput ($ cmd . ' --no-progress ' )
170
+ : $ cmd ;
171
+ }
172
+
173
+ protected function __construct (System $ system , string $ composerCmd )
127
174
{
128
175
$ this ->system = $ system ;
129
176
$ this ->composerCmd = $ composerCmd ;
130
177
}
178
+
179
+ protected function preparePackagesDir (string $ pkgsDir )
180
+ {
181
+ if (file_exists ($ pkgsDir )) {
182
+ foreach (
183
+ new \RecursiveIteratorIterator (
184
+ new \RecursiveDirectoryIterator ($ pkgsDir , \FilesystemIterator::SKIP_DOTS ),
185
+ \RecursiveIteratorIterator::CHILD_FIRST
186
+ ) as $ path
187
+ ) {
188
+ $ path ->isDir () && !$ path ->isLink () ? rmdir ($ path ->getPathname ()) : unlink ($ path ->getPathname ());
189
+ }
190
+ rmdir ($ pkgsDir );
191
+ }
192
+ mkdir ($ pkgsDir );
193
+ }
131
194
}
0 commit comments