/** * {@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)); } }
/** * {@inheritdoc} */ public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) { Assert::keyExists($options, 'rootDir', 'The "rootDir" option is missing.'); $options = array_replace_recursive(self::$defaultOptions, $options); $kvsGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::KEY_VALUE_STORE, $options['store']['type']); $kvsOptions = $options['store']; $kvsOptions['rootDir'] = $options['rootDir']; $kvsGenerator->generateNewInstance('store', $targetMethod, $generatorRegistry, $kvsOptions); $targetMethod->getClass()->addImport(new Import('Puli\\Discovery\\KeyValueStoreDiscovery')); $targetMethod->addBody('$' . $varName . ' = new KeyValueStoreDiscovery($repo, $store);'); }
/** * {@inheritdoc} */ public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) { Assert::keyExists($options, 'rootDir', 'The "rootDir" option is missing.'); $options = array_replace_recursive(self::$defaultOptions, $options); $kvsGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::KEY_VALUE_STORE, $options['store']['type']); $kvsOptions = $options['store']; $kvsOptions['rootDir'] = $options['rootDir']; $kvsGenerator->generateNewInstance('store', $targetMethod, $generatorRegistry, $kvsOptions); $className = ($options['optimize'] ? 'Optimized' : '') . 'PathMappingRepository'; $targetMethod->getClass()->addImport(new Import('Puli\\Repository\\' . $className)); $targetMethod->addBody(sprintf('$%s = new %s($store);', $varName, $className)); }
/** * Adds the createDiscovery() method. * * @param Clazz $class The factory class model. */ private function addCreateDiscoveryMethod(Clazz $class) { $method = new Method('createDiscovery'); $method->setDescription('Creates the resource discovery.'); $arg = new Argument(self::REPO_VAR_NAME); $arg->setTypeHint('ResourceRepository'); $arg->setType('ResourceRepository'); $arg->setDescription('The resource repository to read from.'); $method->addArgument($arg); $method->setReturnValue(new ReturnValue('$' . self::DISCOVERY_VAR_NAME, 'Discovery', 'The created discovery.')); $method->addBody(<<<EOF if (!interface_exists('Puli\\Discovery\\Api\\Discovery')) { throw new RuntimeException('Please install puli/discovery to create Discovery instances.'); } EOF ); $class->addImport(new Import('Puli\\Repository\\Api\\ResourceRepository')); $class->addImport(new Import('Puli\\Discovery\\Api\\Discovery')); $class->addImport(new Import('RuntimeException')); $class->addMethod($method); // Add method body $config = $this->config; $type = $config->get(Config::DISCOVERY_TYPE); $options = $this->camelizeKeys($config->get(Config::DISCOVERY)); $options['root-dir'] = $this->rootDir; $generator = $this->generatorRegistry->getServiceGenerator(GeneratorRegistry::DISCOVERY, $type); $generator->generateNewInstance(self::DISCOVERY_VAR_NAME, $method, $this->generatorRegistry, $options); }
/** * {@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); Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s'); Assert::isArray($options['store'], 'The "store" option should be an array. Got: %s'); if (!isset($options['store']['path'])) { $options['store']['path'] = $targetMethod->getClass()->getDirectory() . '/change-stream.json'; } $kvsGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::KEY_VALUE_STORE, $options['store']['type']); $kvsOptions = $options['store']; $kvsOptions['root-dir'] = $options['root-dir']; $kvsGenerator->generateNewInstance('store', $targetMethod, $generatorRegistry, $kvsOptions); $targetMethod->getClass()->addImport(new Import('Puli\\Repository\\ChangeStream\\KeyValueStoreChangeStream')); $targetMethod->addBody(sprintf('$%s = new KeyValueStoreChangeStream($store);', $varName)); }
/** * {@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); Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s'); Assert::isArray($options['store'], 'The "store" option should be an array. Got: %s'); if (!isset($options['store']['path'])) { $options['store']['path'] = $targetMethod->getClass()->getDirectory() . '/bindings.json'; } $kvsGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::KEY_VALUE_STORE, $options['store']['type']); $kvsOptions = $options['store']; $kvsOptions['root-dir'] = $options['root-dir']; $kvsGenerator->generateNewInstance('store', $targetMethod, $generatorRegistry, $kvsOptions); $targetMethod->getClass()->addImport(new Import('Puli\\Discovery\\KeyValueStoreDiscovery')); $targetMethod->getClass()->addImport(new Import('Puli\\Discovery\\Binding\\Initializer\\ResourceBindingInitializer')); $targetMethod->addBody(sprintf("\$%s = new KeyValueStoreDiscovery(\$store, array(\n new ResourceBindingInitializer(\$repo),\n));", $varName)); }