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+ *
20+ * @var int
21+ */
22+ private const PER_PAGE = 100 ;
23+
1724 /**
1825 * The GitHub Client to use for pagination.
1926 *
@@ -28,6 +35,9 @@ class ResultPager implements ResultPagerInterface
2835 */
2936 protected $ pagination ;
3037
38+ /** @var int */
39+ private $ perPage ;
40+
3141 /**
3242 * The Github client to use for pagination.
3343 *
@@ -41,9 +51,10 @@ class ResultPager implements ResultPagerInterface
4151 *
4252 * @param \Github\Client $client
4353 */
44- public function __construct (Client $ client )
54+ public function __construct (Client $ client, int $ perPage = null )
4555 {
4656 $ this ->client = $ client ;
57+ $ this ->perPage = $ perPage ?? self ::PER_PAGE ;
4758 }
4859
4960 /**
@@ -59,7 +70,25 @@ public function getPagination()
5970 */
6071 public function fetch (ApiInterface $ api , $ method , array $ parameters = [])
6172 {
73+ $ paginatorPerPage = $ this ->perPage ;
74+ $ closure = \Closure::bind (function (ApiInterface $ api ) use ($ paginatorPerPage ) {
75+ $ clone = clone $ api ;
76+
77+ if (null !== $ api ->getPerPage ()) {
78+ @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 );
79+
80+ return $ clone ;
81+ }
82+
83+ /* @phpstan-ignore-next-line */
84+ $ clone ->perPage = $ paginatorPerPage ;
85+
86+ return $ clone ;
87+ }, null , AbstractApi::class);
88+
89+ $ api = $ closure ($ api );
6290 $ result = $ this ->callApi ($ api , $ method , $ parameters );
91+
6392 $ this ->postFetch ();
6493
6594 return $ result ;
@@ -70,37 +99,24 @@ public function fetch(ApiInterface $api, $method, array $parameters = [])
7099 */
71100 public function fetchAll (ApiInterface $ api , $ method , array $ parameters = [])
72101 {
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 );
102+ return iterator_to_array ($ this ->fetchAllLazy ($ api , $ method , $ parameters ));
103+ }
80104
81- try {
82- $ result = $ this -> callApi ( $ api , $ method , $ parameters );
83- $ this ->postFetch ( );
105+ public function fetchAllLazy ( ApiInterface $ api , string $ method , array $ parameters = []): \ Generator
106+ {
107+ $ result = $ this ->fetch ( $ api , $ method , $ parameters );
84108
85- if ( $ isSearch ) {
86- $ result = isset ( $ result [ ' items ' ]) ? $ result [ ' items ' ] : $ result ;
87- }
109+ foreach ( $ result [ ' items ' ] ?? $ result as $ item ) {
110+ yield $ item ;
111+ }
88112
89- while ($ this ->hasNext ()) {
90- $ next = $ this ->fetchNext ();
113+ while ($ this ->hasNext ()) {
114+ $ result = $ this ->fetchNext ();
91115
92- if ($ isSearch ) {
93- $ result = array_merge ($ result , $ next ['items ' ]);
94- } else {
95- $ result = array_merge ($ result , $ next );
96- }
116+ foreach ($ result ['items ' ] ?? $ result as $ item ) {
117+ yield $ item ;
97118 }
98- } finally {
99- // restore the perPage
100- $ api ->setPerPage ($ perPage );
101119 }
102-
103- return $ result ;
104120 }
105121
106122 /**
@@ -187,6 +203,8 @@ protected function get($key)
187203 }
188204
189205 /**
206+ * @deprecated since 2.18 and will be removed in 3.0.
207+ *
190208 * @param ApiInterface $api
191209 * @param string $method
192210 * @param array $parameters
0 commit comments