@@ -18,11 +18,7 @@ abstract class Installer
1818 /** @var string */
1919 protected $ composerCmd ;
2020
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
2622 {
2723 $ system = System::getSystem ();
2824 $ composerCmd = (null !== $ composerCmd ) ? $ composerCmd : $ system ->getComposerCommand ();
@@ -45,11 +41,7 @@ public static function getInstaller($composerCmd = null)
4541 }
4642 }
4743
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 )
5345 {
5446 $ installPath = (null !== $ installPath ) ? $ installPath : $ this ->getInstallPath ();
5547 if (null !== $ installPath && !$ this ->system ->validatePath ($ installPath )) {
@@ -62,70 +54,141 @@ public function install($installPath = null, $beVerbose = false)
6254 $ this ->installKernel ();
6355 }
6456
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 )
8696 {
8797 $ composerStatus = 0 ;
8898
8999 $ 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 );
101101
102102 if ($ beVerbose ) {
103103 echo "\n" ;
104104 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+ ),
108110
109111 $ composerStatus
110112 );
111113 echo "\n" ;
112114 } else {
113- $ composerStatus = $ this ->executeSilentComposerCommand ($ installPath );
115+ $ composerStatus = $ this ->executeSilentComposerCommand ($ pkgsDir );
114116 }
115117
116- if ($ composerStatus !== 0 ) {
118+ if (0 !== $ composerStatus ) {
117119 throw new \RuntimeException ('Error while trying to download Jupyter-PHP dependencies with Composer. ' );
118120 }
119121 }
120122
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 )
127174 {
128175 $ this ->system = $ system ;
129176 $ this ->composerCmd = $ composerCmd ;
130177 }
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+ }
131194}
0 commit comments