66use Symfony \Component \Console \Input \InputArgument ;
77use Symfony \Component \Console \Input \InputInterface ;
88use Symfony \Component \Console \Output \OutputInterface ;
9+ use Symfony \Component \Console \Helper \ProgressBar ;
910
1011use TextAnalysis \Downloaders \DownloadPackageFactory as DPF ;
1112use TextAnalysis \Downloaders \NltkCorporaIndexDownloader ;
1213
1314
15+
1416/**
1517 * Installs the selected nltk corpus package
1618 *
1719 * @author yooper
1820 */
1921class NltkPackageInstallCommand extends Command
2022{
23+ /**
24+ * @var ProgressBar
25+ */
26+ protected $ progressBar = null ;
27+
28+ /**
29+ * @var \Symfony\Component\Console\Output\OutputInterface
30+ */
31+ private $ output ;
32+
2133 protected function configure ()
2234 {
2335 $ this ->setName ('pta:install:package ' )
@@ -31,25 +43,88 @@ protected function configure()
3143
3244 protected function execute (InputInterface $ input , OutputInterface $ output )
3345 {
46+ $ this ->output = $ output ;
3447 $ packageId = $ input ->getArgument ('package ' );
3548
3649 $ listPackages = (new NltkCorporaIndexDownloader ())->getPackages ();
3750
38- $ packageFound = false ;
51+ $ packageFound = null ;
3952
4053 foreach ($ listPackages as $ package )
4154 {
4255 if ($ packageId == $ package ->getId ()) {
43- $ packageFound = true ;
44- $ download = DPF ::download ($ package );
56+ $ packageFound = $ package ;
4557 break ;
4658 }
4759 }
4860
4961 if (!$ packageFound ) {
5062 $ output ->writeln ("Package {$ packageId } was not found, try textconsole pta:list, to see the available packages " );
5163 } else {
64+
65+ $ download = DPF ::download ($ package );
66+ // Create stream context.
67+ $ context = stream_context_create ([], ['notification ' => [$ this , 'progress ' ]]);
68+
69+ // Pipe file.
70+ $ resource = fopen ($ packageFound ->getUrl (), 'r ' , null , $ context );
71+ $ stream = fopen ($ download ->getDownloadFullPath (), 'w+ ' );
72+ if (!$ stream ) {
73+ $ output ->writeln ("Package {$ packageFound ->getId ()} - {$ packageFound ->getName ()} install failed, permission denied to create file into {$ download ->getDownloadFullPath ()}" );
74+ }
75+
76+ stream_copy_to_stream ($ resource , $ stream );
77+
78+ if (!fclose ($ stream )) {
79+ $ output ->writeln ("Could not save file {$ download ->getDownloadFullPath ()}" );
80+ }
81+
82+ // End output.
83+ $ this ->progressBar ->finish ();
84+
85+ if (!$ download ->verifyChecksum ()) {
86+ $ output ->writeln ("Bad checksum for the downloaded package {$ packageFound ->getId ()}" );
87+ exit ;
88+ }
89+ $ download ->unpackPackage ();
90+ $ output ->writeln (PHP_EOL );
5291 $ output ->writeln ("Package {$ package ->getId ()} - {$ package ->getName ()} was installed into {$ download ->getInstallDir ()}" );
5392 }
5493 }
94+
95+ /**
96+ * @param int $notificationCode
97+ * @param int $severity
98+ * @param string $message
99+ * @param int $messageCode
100+ * @param int $bytesTransferred
101+ * @param int $bytesMax
102+ */
103+ public function progress ($ notificationCode , $ severity , $ message , $ messageCode , $ bytesTransferred , $ bytesMax )
104+ {
105+ if (STREAM_NOTIFY_REDIRECTED === $ notificationCode ) {
106+ $ this ->progressBar ->clear ();
107+ $ this ->progressBar = null ;
108+ return ;
109+ }
110+
111+ if (STREAM_NOTIFY_FILE_SIZE_IS === $ notificationCode ) {
112+ if ($ this ->progressBar ) {
113+ $ this ->progressBar ->clear ();
114+ }
115+ $ this ->progressBar = new ProgressBar ($ this ->output , $ bytesMax );
116+ }
117+
118+ if (STREAM_NOTIFY_PROGRESS === $ notificationCode ) {
119+ if (is_null ($ this ->progressBar )) {
120+ $ this ->progressBar = new ProgressBar ($ this ->output );
121+ }
122+ $ this ->progressBar ->setProgress ($ bytesTransferred );
123+ }
124+
125+ if (STREAM_NOTIFY_COMPLETED === $ notificationCode ) {
126+ $ this ->finish ($ bytesTransferred );
127+ }
128+ }
129+
55130}
0 commit comments