6
6
use Symfony \Component \Console \Input \InputArgument ;
7
7
use Symfony \Component \Console \Input \InputInterface ;
8
8
use Symfony \Component \Console \Output \OutputInterface ;
9
+ use Symfony \Component \Console \Helper \ProgressBar ;
9
10
10
11
use TextAnalysis \Downloaders \DownloadPackageFactory as DPF ;
11
12
use TextAnalysis \Downloaders \NltkCorporaIndexDownloader ;
12
13
13
14
15
+
14
16
/**
15
17
* Installs the selected nltk corpus package
16
18
*
17
19
* @author yooper
18
20
*/
19
21
class NltkPackageInstallCommand extends Command
20
22
{
23
+ /**
24
+ * @var ProgressBar
25
+ */
26
+ protected $ progressBar = null ;
27
+
28
+ /**
29
+ * @var \Symfony\Component\Console\Output\OutputInterface
30
+ */
31
+ private $ output ;
32
+
21
33
protected function configure ()
22
34
{
23
35
$ this ->setName ('pta:install:package ' )
@@ -31,25 +43,88 @@ protected function configure()
31
43
32
44
protected function execute (InputInterface $ input , OutputInterface $ output )
33
45
{
46
+ $ this ->output = $ output ;
34
47
$ packageId = $ input ->getArgument ('package ' );
35
48
36
49
$ listPackages = (new NltkCorporaIndexDownloader ())->getPackages ();
37
50
38
- $ packageFound = false ;
51
+ $ packageFound = null ;
39
52
40
53
foreach ($ listPackages as $ package )
41
54
{
42
55
if ($ packageId == $ package ->getId ()) {
43
- $ packageFound = true ;
44
- $ download = DPF ::download ($ package );
56
+ $ packageFound = $ package ;
45
57
break ;
46
58
}
47
59
}
48
60
49
61
if (!$ packageFound ) {
50
62
$ output ->writeln ("Package {$ packageId } was not found, try textconsole pta:list, to see the available packages " );
51
63
} 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 );
52
91
$ output ->writeln ("Package {$ package ->getId ()} - {$ package ->getName ()} was installed into {$ download ->getInstallDir ()}" );
53
92
}
54
93
}
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
+
55
130
}
0 commit comments