public function dump(array $options = array())
{
$this->targetDirRegex = null;
$options = array_merge(array(
'class' => 'ProjectServiceContainer',
'base_class' => 'Container',
'namespace' => '',
'debug' => true,
), $options);
$this->initializeMethodNamesMap($options['base_class']);
$this->docStar = $options['debug'] ? '*' : '';
if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) {
// Build a regexp where the first root dirs are mandatory,
// but every other sub-dir is optional up to the full path in $dir
// Mandate at least 2 root dirs and not more that 5 optional dirs.
$dir = explode(DIRECTORY_SEPARATOR, realpath($dir));
$i = count($dir);
if (3 <= $i) {
$regex = '';
$lastOptionalDir = $i > 8 ? $i - 5 : 3;
$this->targetDirMaxMatches = $i - $lastOptionalDir;
while (--$i >= $lastOptionalDir) {
$regex = sprintf('(%s%s)?', preg_quote(DIRECTORY_SEPARATOR.$dir[$i], '#'), $regex);
}
do {
$regex = preg_quote(DIRECTORY_SEPARATOR.$dir[$i], '#').$regex;
} while (0 < --$i);
$this->targetDirRegex = '#'.preg_quote($dir[0], '#').$regex.'#';
}
}
$code = $this->startClass($options['class'], $options['base_class'], $options['namespace']);
if ($this->container->isFrozen()) {
$code .= $this->addFrozenConstructor();
$code .= $this->addFrozenCompile();
$code .= $this->addIsFrozenMethod();
} else {
$code .= $this->addConstructor();
}
$code .=
$this->addServices().
$this->addDefaultParametersMethod().
$this->endClass().
$this->addProxyClasses()
;
$this->targetDirRegex = null;
$unusedEnvs = array();
foreach ($this->container->getEnvCounters() as $env => $use) {
if (!$use) {
$unusedEnvs[] = $env;
}
}
if ($unusedEnvs) {
throw new EnvParameterException($unusedEnvs);
}
return $code;
}
/** * Builds the container. * @param array $parameters */ public function build($parameters = array()) { // sort array by key to generate the container name ksort($parameters); // needed for new packages installed $composerClass = array_filter(get_declared_classes(), function ($item) { if (0 === strpos($item, 'ComposerAutoloaderInit')) { return true; } }); $composerClass = array_pop($composerClass); // generate hash $parametersHash = md5(serialize($parameters) . $composerClass); $containerClass = 'Container' . $parametersHash; $isDebug = true; $file = sprintf('%s/ladybug_cache/%s.php', sys_get_temp_dir(), $parametersHash); $containerConfigCache = new ConfigCache($file, $isDebug); if (!$containerConfigCache->isFresh()) { $this->initializeContainer(); $this->loadServices(); $this->loadThemes(); $this->loadPlugins(); $this->setParameters($parameters); $this->container->compile(); $dumper = new PhpDumper($this->container); $containerConfigCache->write($dumper->dump(array('class' => $containerClass)), $this->container->getResources()); } else { require_once $file; $this->container = new $containerClass(); } }