22
33namespace Github ;
44
5+ use Github \Api \AbstractApi ;
56use Github \Api \ApiInterface ;
6- use Github \Api \Search ;
77use Github \HttpClient \Message \ResponseMediator ;
88
99/**
1414 */
1515class ResultPager implements ResultPagerInterface
1616{
17+ /**
18+ * The default number of entries to request per page.
19+ * @var int
20+ */
21+ private const PER_PAGE = 100 ;
22+
1723 /**
1824 * The GitHub Client to use for pagination.
1925 *
@@ -28,6 +34,9 @@ class ResultPager implements ResultPagerInterface
2834 */
2935 protected $ pagination ;
3036
37+ /** @var int */
38+ private $ perPage ;
39+
3140 /**
3241 * The Github client to use for pagination.
3342 *
@@ -41,9 +50,10 @@ class ResultPager implements ResultPagerInterface
4150 *
4251 * @param \Github\Client $client
4352 */
44- public function __construct (Client $ client )
53+ public function __construct (Client $ client, int $ perPage = null )
4554 {
4655 $ this ->client = $ client ;
56+ $ this ->perPage = $ perPage ?? self ::PER_PAGE ;
4757 }
4858
4959 /**
@@ -59,7 +69,23 @@ public function getPagination()
5969 */
6070 public function fetch (ApiInterface $ api , $ method , array $ parameters = [])
6171 {
72+ $ paginatorPerPage = $ this ->perPage ;
73+ $ closure = \Closure::bind (function (ApiInterface $ api ) use ($ paginatorPerPage ) {
74+ $ clone = clone $ api ;
75+
76+ if (null !== $ api ->getPerPage ()) {
77+ @trigger_error (sprintf ('Setting the perPage value on an api class is deprecated sinc 2.18 and will be removed in 3.0. Pass the desired items per page value in the constructor of "%s" ' , __CLASS__ ), E_USER_DEPRECATED );
78+ return $ clone ;
79+ }
80+
81+ $ clone ->perPage = $ paginatorPerPage ;
82+
83+ return $ clone ;
84+ }, null , AbstractApi::class);
85+
86+ $ api = $ closure ($ api );
6287 $ result = $ this ->callApi ($ api , $ method , $ parameters );
88+
6389 $ this ->postFetch ();
6490
6591 return $ result ;
@@ -70,37 +96,24 @@ public function fetch(ApiInterface $api, $method, array $parameters = [])
7096 */
7197 public function fetchAll (ApiInterface $ api , $ method , array $ parameters = [])
7298 {
73- $ isSearch = $ api instanceof Search;
74-
75- // get the perPage from the api
76- $ perPage = $ api ->getPerPage ();
77-
78- // set parameters per_page to GitHub max to minimize number of requests
79- $ api ->setPerPage (100 );
99+ return iterator_to_array ($ this ->fetchAllLazy ($ api , $ method , $ parameters ));
100+ }
80101
81- try {
82- $ result = $ this -> callApi ( $ api , $ method , $ parameters );
83- $ this ->postFetch ( );
102+ public function fetchAllLazy ( ApiInterface $ api , string $ method , array $ parameters = []): \ Generator
103+ {
104+ $ result = $ this ->fetch ( $ api , $ method , $ parameters );
84105
85- if ( $ isSearch ) {
86- $ result = isset ( $ result [ ' items ' ]) ? $ result [ ' items ' ] : $ result ;
87- }
106+ foreach ( $ result [ ' items ' ] ?? $ result as $ item ) {
107+ yield $ item ;
108+ }
88109
89- while ($ this ->hasNext ()) {
90- $ next = $ this ->fetchNext ();
110+ while ($ this ->hasNext ()) {
111+ $ result = $ this ->fetchNext ();
91112
92- if ($ isSearch ) {
93- $ result = array_merge ($ result , $ next ['items ' ]);
94- } else {
95- $ result = array_merge ($ result , $ next );
96- }
113+ foreach ($ result ['items ' ] ?? $ result as $ item ) {
114+ yield $ item ;
97115 }
98- } finally {
99- // restore the perPage
100- $ api ->setPerPage ($ perPage );
101116 }
102-
103- return $ result ;
104117 }
105118
106119 /**
@@ -187,6 +200,8 @@ protected function get($key)
187200 }
188201
189202 /**
203+ * @deprecated since 2.18 and will be removed in 3.0.
204+ *
190205 * @param ApiInterface $api
191206 * @param string $method
192207 * @param array $parameters
0 commit comments