Skip to content

Commit

Permalink
Merge pull request #1 from PHP-DI/improve-645
Browse files Browse the repository at this point in the history
Simplify #645
  • Loading branch information
bbaga committed Feb 25, 2019
2 parents b15f8e7 + e459f0a commit ee0a72d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 30 deletions.
8 changes: 5 additions & 3 deletions doc/lazy-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,16 @@ $containerBuilder->writeProxiesToFile(true, __DIR__ . '/tmp/proxies');

You will need to clear the directory every time you deploy to avoid keeping outdated proxies.

### Generating the proxy classes on container build time
### Generating proxy classes when compiling the container

By default the proxies are written to disk the first time they are required. Enabling pre-generation will write the proxy classes to disk when the container is built.
By default proxies are written to disk the first time they are used.

Proxy classes can be pre-generated (for example before deploying) by enabling [container compilation](performances.md):

```php
// Enable writing proxies to file in the var/cache directory at container compile time
$containerBuilder->enableCompilation(__DIR__ . '/var/cache');
$containerBuilder->writeProxiesToFile(true, __DIR__ . '/var/cache');
```

For this functionality to work, both configuration options have to be set.
For this functionality to work, both configuration options have to be set.
2 changes: 1 addition & 1 deletion src/Compiler/ObjectCreationCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private function compileLazyDefinition(ObjectDefinition $definition) : string
$subDefinition->setLazy(false);
$subDefinition = $this->compiler->compileValue($subDefinition);

$this->compiler->getProxyFactory()->generateProxy($definition->getClassName());
$this->compiler->getProxyFactory()->generateProxyClass($definition->getClassName());

return <<<PHP
\$object = \$this->proxyFactory->createProxy(
Expand Down
17 changes: 0 additions & 17 deletions src/Proxy/LazyLoadingValueHolderFactory.php

This file was deleted.

20 changes: 11 additions & 9 deletions src/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DI\Proxy;

use ProxyManager\Configuration;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\FileLocator\FileLocator;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
Expand Down Expand Up @@ -39,10 +40,8 @@ class ProxyFactory
*/
private $proxyManager;

public function __construct(
bool $writeProxiesToFile = false,
string $proxyDirectory = null
) {
public function __construct(bool $writeProxiesToFile = false, string $proxyDirectory = null)
{
$this->writeProxiesToFile = $writeProxiesToFile;
$this->proxyDirectory = $proxyDirectory;
}
Expand All @@ -62,15 +61,18 @@ public function createProxy(string $className, \Closure $initializer) : LazyLoad
}

/**
* Generates and writes proxy class to file.
* Generates and writes the proxy class to file.
*
* @param string $className name of the class to be proxied
*/
public function generateProxy(string $className) : string
public function generateProxyClass(string $className)
{
$this->createProxyManager();

return $this->proxyManager->generateProxyClassToFile($className);
// If proxy classes a written to file then we pre-generate the class
// If they are not written to file then there is no point to do this
if ($this->writeProxiesToFile) {
$this->createProxyManager();
$this->createProxy($className, function () {});
}
}

private function createProxyManager()
Expand Down

0 comments on commit ee0a72d

Please sign in to comment.