当前位置: 首页>>代码示例>>PHP>>正文


PHP ContainerBuilder::generateCode方法代码示例

本文整理汇总了PHP中Nette\DI\ContainerBuilder::generateCode方法的典型用法代码示例。如果您正苦于以下问题:PHP ContainerBuilder::generateCode方法的具体用法?PHP ContainerBuilder::generateCode怎么用?PHP ContainerBuilder::generateCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Nette\DI\ContainerBuilder的用法示例。


在下文中一共展示了ContainerBuilder::generateCode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: loadConfig

 /**
  * Loads configuration from file and process it.
  * @return DI\Container
  */
 public function loadConfig($file, $section = NULL)
 {
     if ($file === NULL) {
         $file = $this->defaultConfigFile;
     }
     $container = $this->container;
     $file = $container->expand($file);
     if (!is_file($file)) {
         $file = preg_replace('#\\.neon$#', '.ini', $file);
         // back compatibility
     }
     if ($section === NULL) {
         if (PHP_SAPI === 'cli') {
             $section = Environment::CONSOLE;
         } else {
             $section = $container->params['productionMode'] ? Environment::PRODUCTION : Environment::DEVELOPMENT;
         }
     }
     $cache = new Cache($container->templateCacheStorage, 'Nette.Configurator');
     $cacheKey = array((array) $container->params, $file, $section);
     $cached = $cache->load($cacheKey);
     if ($cached) {
         require $cached['file'];
         fclose($cached['handle']);
         return $this->container;
     }
     $config = Nette\Config\Config::fromFile($file, $section);
     $code = "<?php\n// source file {$file}\n\n";
     // back compatibility with singular names
     foreach (array('service', 'variable') as $item) {
         if (isset($config[$item])) {
             trigger_error(basename($file) . ": Section '{$item}' is deprecated; use plural form '{$item}s' instead.", E_USER_WARNING);
             $config[$item . 's'] = $config[$item];
             unset($config[$item]);
         }
     }
     // process services
     if (isset($config['services'])) {
         foreach ($config['services'] as $key => &$def) {
             if (preg_match('#^Nette\\\\.*\\\\I?([a-zA-Z]+)$#', strtr($key, '-', '\\'), $m)) {
                 // back compatibility
                 $m[1][0] = strtolower($m[1][0]);
                 trigger_error(basename($file) . ": service name '{$key}' has been renamed to '{$m['1']}'", E_USER_WARNING);
                 $key = $m[1];
             }
             if (is_array($def)) {
                 if (method_exists(get_called_class(), "createService{$key}") && !isset($def['factory']) && !isset($def['class'])) {
                     $def['factory'] = array(get_called_class(), "createService{$key}");
                 }
                 if (isset($def['option'])) {
                     $def['arguments'][] = $def['option'];
                 }
                 if (!empty($def['run'])) {
                     $def['tags'] = array('run');
                 }
             }
         }
         $builder = new DI\ContainerBuilder();
         $code .= $builder->generateCode($config['services']);
         unset($config['services']);
     }
     // consolidate variables
     if (!isset($config['variables'])) {
         $config['variables'] = array();
     }
     foreach ($config as $key => $value) {
         if (!in_array($key, array('variables', 'services', 'php', 'const', 'mode'))) {
             $config['variables'][$key] = $value;
         }
     }
     // pre-expand variables at compile-time
     $variables = $config['variables'];
     array_walk_recursive($config, function (&$val) use($variables) {
         $val = Configurator::preExpand($val, $variables);
     });
     // add variables
     foreach ($config['variables'] as $key => $value) {
         $code .= $this->generateCode('$container->params[?] = ?', $key, $value);
     }
     // PHP settings
     if (isset($config['php'])) {
         foreach ($config['php'] as $key => $value) {
             if (is_array($value)) {
                 // back compatibility - flatten INI dots
                 foreach ($value as $k => $v) {
                     $code .= $this->configurePhp("{$key}.{$k}", $v);
                 }
             } else {
                 $code .= $this->configurePhp($key, $value);
             }
         }
     }
     // define constants
     if (isset($config['const'])) {
         foreach ($config['const'] as $key => $value) {
             $code .= $this->generateCode('define', $key, $value);
//.........这里部分代码省略.........
开发者ID:kovkus,项目名称:r-cms,代码行数:101,代码来源:Configurator.php

示例2: loadConfig

	/**
	 * Loads configuration from file and process it.
	 * @return DI\Container
	 */
	public function loadConfig($file, $section = NULL)
	{
		if ($file === NULL) {
			$file = $this->defaultConfigFile;
		}
		$container = $this->container;
		$file = $container->expand($file);
		if (!is_file($file)) {
			$file = preg_replace('#\.neon$#', '.ini', $file); // back compatibility
		}
		if ($section === NULL) {
			if (PHP_SAPI === 'cli') {
				$section = Environment::CONSOLE;
			} else {
				$section = $container->params['productionMode'] ? Environment::PRODUCTION : Environment::DEVELOPMENT;
			}
		}

		$cache = new Cache($container->templateCacheStorage, 'Nette.Configurator');
		$cacheKey = array((array) $container->params, $file, $section);
		$cached = $cache->load($cacheKey);
		if ($cached) {
			require $cached['file'];
			fclose($cached['handle']);
			return $this->container;
		}

		$config = Nette\Config\Config::fromFile($file, $section);
		$code = "<?php\n// source file $file\n\n";

		// back compatibility with singular names
		foreach (array('service', 'variable') as $item) {
			if (isset($config[$item])) {
				trigger_error(basename($file) . ": Section '$item' is deprecated; use plural form '{$item}s' instead.", E_USER_WARNING);
				$config[$item . 's'] = $config[$item];
				unset($config[$item]);
			}
		}

		// add expanded variables
		while (!empty($config['variables'])) {
			$old = $config['variables'];
			foreach ($config['variables'] as $key => $value) {
				try {
					$code .= $this->generateCode('$container->params[?] = ?', $key, $container->params[$key] = $container->expand($value));
					unset($config['variables'][$key]);
				} catch (Nette\InvalidArgumentException $e) {}
			}
			if ($old === $config['variables']) {
				throw new InvalidStateException("Unable to expand variables: " . implode(', ', array_keys($old)) . ".");
			}
		}
		unset($config['variables']);

		// process services
		if (isset($config['services'])) {
			foreach ($config['services'] as $key => & $def) {
				if (preg_match('#^Nette\\\\.*\\\\I?([a-zA-Z]+)$#', strtr($key, '-', '\\'), $m)) { // back compatibility
					$m[1][0] = strtolower($m[1][0]);
					trigger_error(basename($file) . ": service name '$key' has been renamed to '$m[1]'", E_USER_WARNING);
					$key = $m[1];
				}

				if (is_array($def)) {
					if (method_exists(get_called_class(), "createService$key") && !isset($def['factory']) && !isset($def['class'])) {
						$def['factory'] = array(get_called_class(), "createService$key");
					}

					if (isset($def['option'])) {
						$def['arguments'][] = $def['option'];
					}

					if (!empty($def['run'])) {
						$def['tags'] = array('run');
					}
				}
			}
			$builder = new DI\ContainerBuilder;
			/**/$code .= $builder->generateCode($config['services']);/**/
			/*5.2* $code .= $this->generateCode('$builder = new '.get_class($builder).'; $builder->addDefinitions($container, ?)', $config['services']);*/
			unset($config['services']);
		}

		// expand variables
		array_walk_recursive($config, function(&$val) use ($container) {
			$val = $container->expand($val);
		});

		// PHP settings
		if (isset($config['php'])) {
			foreach ($config['php'] as $key => $value) {
				if (is_array($value)) { // back compatibility - flatten INI dots
					foreach ($value as $k => $v) {
						$code .= $this->configurePhp("$key.$k", $v);
					}
				} else {
//.........这里部分代码省略.........
开发者ID:norbe,项目名称:nette,代码行数:101,代码来源:Configurator.php


注:本文中的Nette\DI\ContainerBuilder::generateCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。