/**
     * {@inheritdoc}
     */
    public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
    {
        Assert::keyExists($options, 'rootDir', 'The "rootDir" option is missing.');
        $options = array_replace(self::$defaultOptions, $options);
        if (!isset($options['path'])) {
            $options['path'] = $targetMethod->getClass()->getDirectory() . '/repository';
        }
        Assert::string($options['path'], 'The "path" option should be a string. Got: %s');
        Assert::string($options['rootDir'], 'The "rootDir" option should be a string. Got: %s');
        Assert::boolean($options['symlink'], 'The "symlink" option should be a boolean. Got: %s');
        $path = Path::makeAbsolute($options['path'], $options['rootDir']);
        $relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
        $escPath = $relPath ? '__DIR__.' . var_export('/' . $relPath, true) : '__DIR__';
        if ($relPath) {
            $targetMethod->addBody(<<<EOF
if (!file_exists({$escPath})) {
    mkdir({$escPath}, 0777, true);
}

EOF
);
        }
        $targetMethod->getClass()->addImport(new Import('Puli\\Repository\\FilesystemRepository'));
        $targetMethod->addBody(sprintf('$%s = new FilesystemRepository(%s, %s);', $varName, $escPath, var_export($options['symlink'], true)));
    }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
 {
     Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.');
     $options = array_replace_recursive(self::$defaultOptions, $options);
     if (!isset($options['path'])) {
         $options['path'] = $targetMethod->getClass()->getDirectory() . '/path-mappings.json';
     }
     Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s');
     Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s');
     Assert::boolean($options['optimize'], 'The "optimize" option should be a boolean. Got: %s');
     Assert::isArray($options['change-stream'], 'The "change-stream" option should be an array. Got: %s');
     $path = Path::makeAbsolute($options['path'], $options['root-dir']);
     $relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
     $relBaseDir = Path::makeRelative($options['root-dir'], $targetMethod->getClass()->getDirectory());
     $escPath = '__DIR__.' . var_export('/' . $relPath, true);
     $escBaseDir = $relBaseDir ? '__DIR__.' . var_export('/' . $relBaseDir, true) : '__DIR__';
     if ($options['optimize']) {
         $streamGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::CHANGE_STREAM, $options['change-stream']['type']);
         $streamOptions = $options['change-stream'];
         $streamOptions['root-dir'] = $options['root-dir'];
         $streamGenerator->generateNewInstance('stream', $targetMethod, $generatorRegistry, $streamOptions);
         $targetMethod->getClass()->addImport(new Import('Puli\\Repository\\OptimizedJsonRepository'));
         $targetMethod->addBody(sprintf('$%s = new OptimizedJsonRepository(%s, %s, false, $stream);', $varName, $escPath, $escBaseDir));
     } else {
         $targetMethod->getClass()->addImport(new Import('Puli\\Repository\\JsonRepository'));
         $targetMethod->addBody(sprintf('$%s = new JsonRepository(%s, %s, true);', $varName, $escPath, $escBaseDir));
     }
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
 {
     Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.');
     $options = array_replace(self::$defaultOptions, $options);
     Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s');
     Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s');
     Assert::boolean($options['serialize-strings'], 'The "serialize-strings" option should be a boolean. Got: %s');
     Assert::boolean($options['serialize-arrays'], 'The "serialize-arrays" option should be a boolean. Got: %s');
     Assert::boolean($options['escape-slash'], 'The "escape-slash" option should be a boolean. Got: %s');
     Assert::boolean($options['pretty-print'], 'The "pretty-print" option should be a boolean. Got: %s');
     $path = Path::makeAbsolute($options['path'], $options['root-dir']);
     $relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
     $flags = array();
     if (!$options['serialize-strings']) {
         $flags[] = 'JsonFileStore::NO_SERIALIZE_STRINGS';
     }
     if (!$options['serialize-arrays']) {
         $flags[] = 'JsonFileStore::NO_SERIALIZE_ARRAYS';
     }
     if (!$options['serialize-arrays']) {
         $flags[] = 'JsonFileStore::NO_ESCAPE_SLASH';
     }
     if ($options['pretty-print']) {
         $flags[] = 'JsonFileStore::PRETTY_PRINT';
     }
     $targetMethod->getClass()->addImport(new Import('Webmozart\\KeyValueStore\\JsonFileStore'));
     $targetMethod->addBody(sprintf('$%s = new JsonFileStore(%s%s%s);', $varName, $flags ? "\n    " : '', '__DIR__.' . var_export('/' . $relPath, true), $flags ? ",\n    " . implode("\n        | ", $flags) . "\n" : ''));
 }
 /**
  * Marks or unmarks the type as duplicate.
  *
  * The method {@link load()} needs to be called before calling this method,
  * otherwise an exception is thrown.
  *
  * @param bool $duplicate Whether or not the type is a duplicate.
  *
  * @throws NotLoadedException If the descriptor is not loaded.
  */
 public function markDuplicate($duplicate)
 {
     Assert::boolean($duplicate);
     if (null === $this->state) {
         throw new NotLoadedException('The type descriptor is not loaded.');
     }
     $this->state = $duplicate ? BindingTypeState::DUPLICATE : BindingTypeState::ENABLED;
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function findConfigKeys(Expression $expr, $includeFallback = false, $includeUnset = false, $raw = true)
 {
     Assert::boolean($includeFallback, 'The argument $includeFallback must be a boolean.');
     Assert::boolean($includeUnset, 'The argument $includeUnset must be a boolean.');
     $values = array();
     foreach ($this->getConfigKeys($includeFallback, $includeUnset, $raw) as $key => $value) {
         if ($expr->evaluate($key)) {
             $values[$key] = $value;
         }
     }
     return $values;
 }