First of all, I'm not an expert on PHP. I recently read about generators and I understand their usefulness in avoiding loading a set of objects into an array to the point of saturating the memory.
The $pages->find() call is known to be greedy (and slow) when it comes to processing large amounts...