16
16
use App \Entity \User ;
17
17
use App \SecurityAdvisory \FriendsOfPhpSecurityAdvisoriesSource ;
18
18
use Composer \Pcre \Preg ;
19
+ use Psr \Cache \CacheItemPoolInterface ;
19
20
use Psr \Log \LoggerInterface ;
20
21
use Composer \Package \Loader \ArrayLoader ;
21
22
use Composer \Package \Loader \ValidatingArrayLoader ;
24
25
use Composer \Repository \InvalidRepositoryException ;
25
26
use Composer \Repository \VcsRepository ;
26
27
use Composer \IO \BufferIO ;
28
+ use Psr \SimpleCache \CacheInterface ;
29
+ use Symfony \Component \Cache \Psr16Cache ;
27
30
use Symfony \Component \Console \Output \OutputInterface ;
28
31
use App \Entity \Package ;
29
32
use App \Entity \Version ;
@@ -62,6 +65,7 @@ class UpdaterWorker
62
65
private Scheduler $ scheduler ;
63
66
private PackageManager $ packageManager ;
64
67
private DownloadManager $ downloadManager ;
68
+ private CacheInterface $ cache ;
65
69
/** For use in fixtures loader only */
66
70
private bool $ loadMinimalVersions = false ;
67
71
@@ -75,7 +79,9 @@ public function __construct(
75
79
DownloadManager $ downloadManager ,
76
80
private StatsDClient $ statsd ,
77
81
private readonly FallbackGitHubAuthProvider $ fallbackGitHubAuthProvider ,
82
+ CacheItemPoolInterface $ cache ,
78
83
) {
84
+ $ this ->cache = new Psr16Cache ($ cache );
79
85
$ this ->logger = $ logger ;
80
86
$ this ->doctrine = $ doctrine ;
81
87
$ this ->updater = $ updater ;
@@ -127,7 +133,6 @@ public function process(Job $job, SignalHandler $signal): array
127
133
$ usesPackagistToken = false ;
128
134
if (Preg::isMatch ('{^https://github\.com/(?P<repo>[^/]+/[^/]+?)(?:\.git)?$}i ' , $ package ->getRepository (), $ matches )) {
129
135
$ usesPackagistToken = true ;
130
- $ apc = extension_loaded ('apcu ' );
131
136
132
137
foreach ($ package ->getMaintainers () as $ maintainer ) {
133
138
if ($ maintainer ->getId () === 1 ) {
@@ -137,26 +142,26 @@ public function process(Job $job, SignalHandler $signal): array
137
142
continue ;
138
143
}
139
144
140
- $ valid = null ;
141
- if ($ apc ) {
142
- $ valid = apcu_fetch ('is_token_valid_ ' .$ maintainer ->getUsernameCanonical ());
143
- }
144
-
145
- if (true !== $ valid ) {
146
- $ context = stream_context_create (['http ' => ['header ' => ['User-agent: packagist-token-check ' , 'Authorization: token ' .$ newGithubToken ]]]);
147
- $ rate = json_decode ((string ) @file_get_contents ('https://api.github.com/repos/ ' .$ matches ['repo ' ].'/git/refs/heads?per_page=1 ' , false , $ context ), true );
145
+ $ valid = $ this ->cache ->get ('is_token_valid_ ' .$ maintainer ->getUsernameCanonical ());
146
+ if ('1 ' !== $ valid ) {
147
+ $ context = stream_context_create (['http ' => [
148
+ 'header ' => ['User-agent: packagist-token-check ' , 'Authorization: token ' .$ newGithubToken ],
149
+ 'ignore_errors ' => true ,
150
+ ]]);
151
+ $ rateResponse = json_decode ((string ) @file_get_contents ('https://api.github.com/repos/ ' .$ matches ['repo ' ].'/git/refs/heads?per_page=1 ' , false , $ context ), true );
148
152
// invalid/outdated token, wipe it so we don't try it again
149
- if (!$ rate && isset ($ http_response_header [0 ]) && (strpos ($ http_response_header [0 ], '403 ' ) || strpos ($ http_response_header [0 ], '401 ' ))) {
150
- $ maintainer ->setGithubToken (null );
151
- $ em ->persist ($ maintainer );
152
- $ em ->flush ();
153
- continue ;
153
+ if (!$ rateResponse || (isset ($ http_response_header [0 ]) && Preg::isMatch ('{HTTP/\s+ 4[0-9][0-9] } ' , $ http_response_header [0 ]))) {
154
+ if (str_contains ($ http_response_header [0 ], '403 ' ) || str_contains ($ http_response_header [0 ], '401 ' )) {
155
+ $ this ->logger ->error ('Invalid token check response for ' .$ maintainer ->getUsernameCanonical ().' on ' .$ matches ['repo ' ], ['headers ' => $ http_response_header , 'response ' => $ rateResponse ]);
156
+ $ maintainer ->setGithubToken (null );
157
+ $ em ->persist ($ maintainer );
158
+ $ em ->flush ();
159
+ continue ;
160
+ }
154
161
}
155
162
}
156
163
157
- if ($ apc ) {
158
- apcu_store ('is_token_valid_ ' .$ maintainer ->getUsernameCanonical (), true , 86400 );
159
- }
164
+ $ this ->cache ->set ('is_token_valid_ ' .$ maintainer ->getUsernameCanonical (), '1 ' , 86400 );
160
165
161
166
$ usesPackagistToken = false ;
162
167
$ io ->setAuthentication ('github.com ' , $ newGithubToken , 'x-oauth-basic ' );
0 commit comments