public function testProcess()
 {
     $wise = new Wise();
     $wise->setGlobalParameters(array('global' => array('value' => 999)));
     $this->setPropertyValue($this->loader, 'wise', $wise);
     file_put_contents("{$this->dir}/one.php", '<?php return ' . var_export(array('imports' => array(array('resource' => 'two.php')), 'global' => '%global.value%', 'placeholder' => '%imported.list%', 'sub' => array('inline_placeholder' => 'rand: %imported.list.null%%imported.value%'), '%imported.key%' => 'a value'), true) . ';');
     file_put_contents("{$this->dir}/two.php", '<?php return ' . var_export(array('imported' => array('key' => 'replaced_key', 'list' => array('null' => null, 'value' => 123), 'value' => $rand = rand())), true) . ';');
     $this->assertEquals(array('imports' => array(array('resource' => 'two.php')), 'global' => 999, 'placeholder' => array('null' => null, 'value' => 123), 'sub' => array('inline_placeholder' => 'rand: ' . $rand), 'replaced_key' => 'a value', 'imported' => array('key' => 'replaced_key', 'list' => array('null' => null, 'value' => 123), 'value' => $rand)), $this->loader->load('one.php'));
 }
 /**
  * Imports other configuration files and resolves references.
  *
  * @param array  $data The data.
  * @param string $file The file source.
  *
  * @return array The processed data.
  *
  * @throws ImportException           If "imports" is invalid.
  * @throws InvalidReferenceException If an invalid reference is used.
  */
 public function process($data, $file)
 {
     if (empty($data)) {
         return array();
     }
     if (isset($data['imports'])) {
         if (false === is_array($data['imports'])) {
             throw ImportException::format('The "imports" value is not valid in "%s".', $file);
         }
         $dir = dirname($file);
         foreach ($data['imports'] as $i => $import) {
             if (false === is_array($import)) {
                 throw ImportException::format('One of the "imports" values (#%d) is not valid in "%s".', $i, $file);
             }
             if (false === isset($import['resource'])) {
                 throw ImportException::format('A resource was not defined for an import in "%s".', $file);
             }
             $this->setCurrentDir($dir);
             $data = array_replace_recursive($this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false), $data);
         }
     }
     $global = $this->wise ? $this->wise->getGlobalParameters() : array();
     $_this = $this;
     ArrayUtil::walkRecursive($data, function (&$value, $key, &$array) use(&$data, $global, $_this) {
         $value = $_this->doReplace($value, $data, $global);
         if (false !== strpos($key, '%')) {
             unset($array[$key]);
             $key = $_this->doReplace($key, $data, $global);
             $array[$key] = $value;
         }
     });
     return $data;
 }
示例#3
0
 /**
  * @depends testGetProcessor
  */
 public function testSetProcessor()
 {
     $this->wise->setProcessor($this->processor);
     $this->assertSame($this->processor, $this->wise->getProcessor());
     $processor = new BasicProcessor();
     $this->wise->setProcessor($processor);
     $this->assertSame($processor, $this->wise->getProcessor());
 }
 /**
  * {@inheritDoc}
  */
 public function register(Application $app)
 {
     $app['wise'] = $app->share(function () use($app) {
         $wise = new Wise($app['debug']);
         $wise->setLoader($app['wise.loader']);
         $wise->setCollector($app['wise.collector']);
         $wise->setGlobalParameters($app['wise.options']['parameters']);
         $wise->setProcessor($app['wise.processor']);
         if (isset($app['wise.cache_dir'])) {
             $wise->setCacheDir($app['wise.cache_dir']);
         }
         return $wise;
     });
     $app['wise.collector'] = $app->share(function () use($app) {
         return new Resource\ResourceCollector();
     });
     $app['wise.loader'] = $app->share(function () use($app) {
         return new DelegatingLoader($app['wise.loader_resolver']);
     });
     $app['wise.loader_resolver'] = $app->share(function () use($app) {
         return new LoaderResolver($app['wise.loaders']);
     });
     $app['wise.loaders'] = $app->share(function () use($app) {
         $loaders = array(new Loader\IniFileLoader($app['wise.locator']), new Loader\PhpFileLoader($app['wise.locator']));
         if (class_exists('Herrera\\Json\\Json')) {
             $loaders[] = new Loader\JsonFileLoader($app['wise.locator']);
         }
         if (class_exists('DOMDocument')) {
             $loaders[] = new Loader\XmlFileLoader($app['wise.locator']);
         }
         if (class_exists('Symfony\\Component\\Yaml\\Yaml')) {
             $loaders[] = new Loader\YamlFileLoader($app['wise.locator']);
         }
         foreach ($loaders as $loader) {
             if ($loader instanceof SilexAwareInterface) {
                 $loader->setSilex($app);
             }
         }
         return $loaders;
     });
     $app['wise.locator'] = $app->share(function () use($app) {
         return new FileLocator($app['wise.path']);
     });
     $app['wise.options'] = new \ArrayObject(array('config' => array('routes' => 'routes', 'services' => 'services'), 'mode' => $app['debug'] ? 'dev' : 'prod', 'type' => 'json', 'parameters' => array()));
     $app['wise.processor'] = $app->share(function () use($app) {
         return new Processor\DelegatingProcessor($app['wise.processor_resolver']);
     });
     $app['wise.processor_resolver'] = $app->share(function () use($app) {
         return new Processor\ProcessorResolver($app['wise.processors']);
     });
     $app['wise.processors'] = array();
 }