/** * Executes this configuration handler. * * @param array $configFiles An array of absolute filesystem path to a configuration file * * @return string Data to be written to a cache file */ public function execute($configFiles) { $class = sfConfig::get('sf_app') . '_' . sfConfig::get('sf_environment') . 'ServiceContainer'; $serviceContainerBuilder = new sfServiceContainerBuilder(); $loader = new sfServiceContainerLoaderArray($serviceContainerBuilder); $loader->load(static::getConfiguration($configFiles)); $dumper = new sfServiceContainerDumperPhp($serviceContainerBuilder); $code = $dumper->dump(array('class' => $class, 'base_class' => $this->parameterHolder->get('base_class'))); // compile data $retval = sprintf("<?php\n" . "// auto-generated by sfServiceConfigHandler\n" . "// date: %s\n\n" . "\$class = '%s';\n" . "if (!class_exists(\$class, false)) {\n" . "%s\n" . "}\n" . "return \$class;\n\n", date('Y/m/d H:i:s'), $class, $code); return $retval; }
protected function initializeContainer() { $name = 'SonataProject_' . md5($this->appDir . $this->isDebug . $this->environment) . 'ServiceContainer'; $file = sys_get_temp_dir() . '/' . $name . '.php'; if (!$this->isDebug && is_readable($file)) { require_once $file; $container = new $name(); } else { $serviceConfig = $this->paths['config'] . '/services.yml'; if (!is_readable($serviceConfig)) { throw new RuntimeException(sprintf("The service config file in '%s' does not exist or is not readable", $serviceConfig)); } $container = new sfServiceContainerBuilder(); $loader = new sfServiceContainerLoaderFileYaml($container); $loader->load($serviceConfig); if (!$this->isDebug) { $dumper = new sfServiceContainerDumperPhp($container); file_put_contents($file, $dumper->dump(array('class' => $name))); } } return $container; }
protected function dumpServiceContainer($name, $file) { foreach (array(sfConfig::get('dm_cache_dir'), dirname($file)) as $dir) { if (!is_dir($dir)) { $oldUmask = umask(0); mkdir($dir, 0777); umask($oldUmask); } } $this->loadServiceContainerExtraStuff(); $sc = new sfServiceContainerBuilder(); $configPaths = $this->configuration->getConfigPaths('config/dm/services.yml'); $loader = new sfServiceContainerLoaderFileYaml($sc); $loader->load($configPaths); $loader = new dmServiceContainerLoaderConfiguration($sc, $this->dispatcher); $loader->load(dmConfig::getAll()); /* * Allow listeners of dm.service_container.pre_dump event * to modify the loader */ $sc = $this->dispatcher->filter(new sfEvent($this, 'dm.service_container.pre_dump'), $sc)->getReturnValue(); $dumper = new sfServiceContainerDumperPhp($sc); $baseClass = sfConfig::get('dm_service_container_base_class', 'dm' . ucfirst(sfConfig::get('dm_context_type')) . 'BaseServiceContainer'); file_put_contents($file, $dumper->dump(array('class' => $name, 'base_class' => $baseClass))); $oldUmask = umask(0); @chmod($file, 0777); umask($oldUmask); if (!file_exists($file)) { throw new dmException('Can not write the generated service container to ' . $file); } unset($dumper, $loader, $sc); }
*/ require_once dirname(__FILE__) . '/../lib/lime/lime.php'; require_once dirname(__FILE__) . '/../../lib/sfServiceContainerAutoloader.php'; sfServiceContainerAutoloader::register(); $t = new lime_test(5); $dir = dirname(__FILE__) . '/fixtures/php'; // ->dump() $t->diag('->dump()'); $dumper = new sfServiceContainerDumperPhp($container = new sfServiceContainerBuilder()); $t->is($dumper->dump(), file_get_contents($dir . '/services1.php'), '->dump() dumps an empty container as an empty PHP class'); $t->is($dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer')), file_get_contents($dir . '/services1-1.php'), '->dump() takes a class and a base_class options'); $container = new sfServiceContainerBuilder(); $dumper = new sfServiceContainerDumperPhp($container); // ->addParameters() $t->diag('->addParameters()'); $container = (include dirname(__FILE__) . '/fixtures/containers/container8.php'); $dumper = new sfServiceContainerDumperPhp($container); $t->is($dumper->dump(), file_get_contents($dir . '/services8.php'), '->dump() dumps parameters'); // ->addService() $t->diag('->addService()'); $container = (include dirname(__FILE__) . '/fixtures/containers/container9.php'); $dumper = new sfServiceContainerDumperPhp($container); $t->is($dumper->dump(), str_replace('%path%', dirname(__FILE__) . '/fixtures/includes', file_get_contents($dir . '/services9.php')), '->dump() dumps services'); $dumper = new sfServiceContainerDumperPhp($container = new sfServiceContainerBuilder()); $container->register('foo', 'FooClass')->addArgument(new stdClass()); try { $dumper->dump(); $t->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); } catch (RuntimeException $e) { $t->pass('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); }
/** * Loads Sympal's service container * * @link http://components.symfony-project.org/dependency-injection/trunk/book/06-Speed */ protected function loadServiceContainer() { $autoloaderPath = $this->getSymfonyContext()->getConfiguration()->getPluginConfiguration('sfSympalPlugin')->getRootDir() . '/lib/vendor/service_container/lib/sfServiceContainerAutoloader.php'; if (!file_exists($autoloaderPath)) { throw new sfException(sprintf('Cannot find the service container library at %s. If you are including sfSympalPlugin as a git submodule, be sure to run the following commands from inside the plugins/sfSympalPlugin directory: git submodule init git submodule update', $autoloaderPath)); } sfServiceContainerAutoloader::register(); $app = $this->getSymfonyContext()->getConfiguration()->getApplication(); $name = 'sfSympal' . $app . 'ServiceContainer'; $path = sfConfig::get('sf_app_cache_dir') . '/' . $name . '.php'; if (!sfConfig::get('sf_debug') && file_exists($path)) { require_once $path; $this->_serviceContainer = new $name(); } else { // build the service container dynamically $this->_serviceContainer = new sfServiceContainerBuilder(); $loader = new sfServiceContainerLoaderFileYaml($this->_serviceContainer); $configPaths = $this->getSymfonyContext()->getConfiguration()->getConfigPaths('config/sympal_services.yml'); $loader->load($configPaths); // if not in debug, write the service container to file if (!sfConfig::get('sf_debug')) { $dumper = new sfServiceContainerDumperPhp($this->_serviceContainer); file_put_contents($path, $dumper->dump(array('class' => $name, 'base_class' => sfSympalConfig::get('service_container', 'base_class', 'sfServiceContainer')))); } } }
/** * Create a container object. * The configuration may be specified in the 'config_file' * component of the options. If not specified, an empty container * will be generated. * If 'dump_file' is specified and we did not generate an empty * container, the specification is compiled into a PHP file that * can later be loaded instead of the configuration (performance). * * @return sfServiceContainerInterface The container */ public function makeContainer() { if (!isset($this->_options['config_file'])) { return new sfServiceContainer(); } /** * Attempt to load the generated PHP file, if defined * and exists. If succesful, the class we need should be * defined, so create and return it. */ $file = $this->_options['config_file']; $class = isset($this->_options['class']) ? $this->_options['class'] : 'Container'; if (isset($this->_options['dump_file'])) { $dumpFile = $this->_options['dump_file']; if (file_exists($dumpFile)) { require_once $dumpFile; return new $class(); } } /** * Create a builder to which we attach a loader so * we can load the configuration file. */ $parameters = $this->getParameters(); $sc = new sfServiceContainerBuilder($parameters); $ext = pathinfo($file, PATHINFO_EXTENSION); switch ($ext) { case 'xml': $loader = new sfServiceContainerLoaderFileXml($sc); break; case 'init': $loader = new sfServiceContainerLoaderFileIni($sc); break; case 'yaml': $loader = new sfServiceContainerLoaderFileYaml($sc); break; default: throw new Exception("No loader available for extension '{$ext}'"); break; } $loader->load($file); /** * If a dump file was specified, make the dump * so that it can be loaded in the future. */ if (isset($dumpFile)) { $dumper = new sfServiceContainerDumperPhp($sc); file_put_contents($dumpFile, $dumper->dump(array('class' => $class))); } return $sc; }
/** * Creates a PEAR package * * @dependsOn test, build:init * @before config:deploy-local */ task('build:package', function () { $spec = PackageSpec::in('build')->setName('pantr')->setChannel(property('pear.channel'))->setSummary('pantr is a simple php build tool.')->setDescription('pantr is a simple php build tool.')->setNotes('n/a')->setVersion(property('pantr.version'))->setStability('beta')->setLicense(PackageSpec::LICENSE_MIT)->addMaintainer('lead', 'Patrick Gotthardt', 'pago', '*****@*****.**')->setDependsOnPHPVersion('5.3.0')->addFiles(fileset()->ignore_version_control()->relative()->in('build'))->addFiles(array('bin/pantr', 'bin/pantr.bat'))->addExecutable('bin/pantr')->addExecutable('bin/pantr.bat', null, PackageSpec::PLATFORM_WIN)->createPackage('dist'); }); // compilation File::task('build:services', 'build/pantr/pantr/core/services.yml', ':dirname/:filename.php')->dependsOn('build:init')->run(function ($src, $target) { // compile dependency injection layer $sc = new \sfServiceContainerBuilder(); $loader = new \sfServiceContainerLoaderFileYaml($sc); $loader->load($src); $dumper = new \sfServiceContainerDumperPhp($sc); $code = $dumper->dump(array('class' => 'pantrContainer')); file_put_contents($target, $code); }); /** * Updates the version number in the pantr.php file * * @hidden * @dependsOn build:init */ task('build:version', function () { // update version number replace_in('build/pantr/pantr/pantr.php', function ($f, $c) { writeAction('rewrite', $f); return str_replace("const VERSION='DEV';", "const VERSION='" . property('pantr.version') . "';", $c); });