Rework your factory closures to factory classes.
Config
'service_manager' => array(
'factories' => array(
'auth.service' => \Fully\Qualified\NS\AuthFactory::class,
),
),
Factory
namespace Fully\Qualified\NS;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class AuthFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator) {
// create your object and set dependencies
return $object
}
}
Besides this approach making caching possible, another advantage is that PHP will parse your config faster since it doesn't have to create a Closure class on each request for each anonymous function.