Skip to content

Commit 7423b3b

Browse files
authored
Merge pull request #17 from olexp/add-factory
Add addFactoryToContainer method
2 parents 3cfcc47 + 1fd4b05 commit 7423b3b

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Codeception/Lib/Connector/Laminas.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class Laminas extends AbstractBrowser
4747
*/
4848
private $persistentServices = [];
4949

50+
/**
51+
* @var array
52+
*/
53+
private $persistentFactories = [];
54+
5055
public function setApplicationConfig(array $applicationConfig): void
5156
{
5257
$this->applicationConfig = $applicationConfig;
@@ -148,6 +153,15 @@ public function addServiceToContainer(string $name, $service): void
148153
$this->persistentServices[$name] = $service;
149154
}
150155

156+
public function addFactoryToContainer(string $name, $factory): void
157+
{
158+
$this->application->getServiceManager()->setAllowOverride(true);
159+
$this->application->getServiceManager()->setFactory($name, $factory);
160+
$this->application->getServiceManager()->setAllowOverride(false);
161+
162+
$this->persistentFactories[$name] = $factory;
163+
}
164+
151165
private function extractHeaders(BrowserKitRequest $browserKitRequest): Headers
152166
{
153167
$headers = [];
@@ -200,5 +214,9 @@ private function createApplication(): void
200214
$eventManager = $this->application->getEventManager();
201215

202216
$eventManager->detach([$sendResponseListener, 'sendResponse']);
217+
218+
$serviceManager->setAllowOverride(true);
219+
$serviceManager->configure(['factories' => $this->persistentFactories]);
220+
$serviceManager->setAllowOverride(false);
203221
}
204222
}

src/Codeception/Module/Laminas.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Laminas\Router\Http\Hostname;
1919
use Laminas\Router\Http\Part;
2020
use Laminas\Router\Http\TreeRouteStack;
21+
use Laminas\ServiceManager\Factory\FactoryInterface;
2122
use Traversable;
2223
use function array_unique;
2324
use function class_exists;
@@ -181,6 +182,19 @@ public function addServiceToContainer(string $name, object $service): void
181182
$this->client->addServiceToContainer($name, $service);
182183
}
183184

185+
/**
186+
* Adds factory to a Laminas container
187+
*
188+
* @param string $name
189+
* @param string|callable|FactoryInterface $factory
190+
* @return void
191+
* @part services
192+
*/
193+
public function addFactoryToContainer(string $name, $factory): void
194+
{
195+
$this->client->addFactoryToContainer($name, $factory);
196+
}
197+
184198
/**
185199
* Opens web page using route name and parameters.
186200
*

0 commit comments

Comments
 (0)