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,8 @@ class ResultPager implements ResultPagerInterface
2834 */
2935 protected $ pagination ;
3036
37+ private $ perPage ;
38+
3139 /**
3240 * The Github client to use for pagination.
3341 *
@@ -41,9 +49,10 @@ class ResultPager implements ResultPagerInterface
4149 *
4250 * @param \Github\Client $client
4351 */
44- public function __construct (Client $ client )
52+ public function __construct (Client $ client, int $ perPage = null )
4553 {
4654 $ this ->client = $ client ;
55+ $ this ->perPage = $ perPage ?? self ::PER_PAGE ;
4756 }
4857
4958 /**
@@ -59,7 +68,23 @@ public function getPagination()
5968 */
6069 public function fetch (ApiInterface $ api , $ method , array $ parameters = [])
6170 {
71+ $ paginatorPerPage = $ this ->perPage ;
72+ $ closure = \Closure::bind (function (AbstractApi $ api ) use ($ paginatorPerPage ) {
73+ $ clone = clone $ api ;
74+
75+ if (null !== $ api ->getPerPage ()) {
76+ @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 );
77+ return $ clone ;
78+ }
79+
80+ $ clone ->perPage = $ paginatorPerPage ;
81+
82+ return $ clone ;
83+ }, null , AbstractApi::class);
84+
85+ $ api = $ closure ($ api );
6286 $ result = $ this ->callApi ($ api , $ method , $ parameters );
87+
6388 $ this ->postFetch ();
6489
6590 return $ result ;
@@ -70,34 +95,23 @@ public function fetch(ApiInterface $api, $method, array $parameters = [])
7095 */
7196 public function fetchAll (ApiInterface $ api , $ method , array $ parameters = [])
7297 {
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 );
98+ return iterator_to_array ($ this ->fetchAllLazy ($ api , $ method , $ parameters ));
99+ }
80100
81- try {
82- $ result = $ this -> callApi ( $ api , $ method , $ parameters );
83- $ this ->postFetch ( );
101+ public function fetchAllLazy ( ApiInterface $ api , string $ method , array $ parameters = [])
102+ {
103+ $ result = $ this ->fetch ( $ api , $ method , $ parameters );
84104
85- if ( $ isSearch ) {
86- $ result = isset ( $ result [ ' items ' ]) ? $ result [ ' items ' ] : $ result ;
87- }
105+ foreach ( $ result [ ' items ' ] ?? $ result as $ item ) {
106+ yield $ item ;
107+ }
88108
89- while ($ this ->hasNext ()) {
90- $ next = $ this ->fetchNext ();
109+ while ($ this ->hasNext ()) {
110+ $ result = $ this ->fetchNext ();
91111
92- if ($ isSearch ) {
93- $ result = array_merge ($ result , $ next ['items ' ]);
94- } else {
95- $ result = array_merge ($ result , $ next );
96- }
112+ foreach ($ result ['items ' ] ?? $ result as $ item ) {
113+ yield $ item ;
97114 }
98- } finally {
99- // restore the perPage
100- $ api ->setPerPage ($ perPage );
101115 }
102116
103117 return $ result ;
@@ -187,6 +201,8 @@ protected function get($key)
187201 }
188202
189203 /**
204+ * @deprecated since 2.18 and will be removed in 3.0.
205+ *
190206 * @param ApiInterface $api
191207 * @param string $method
192208 * @param array $parameters
0 commit comments