/** * {@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, 'root-dir', 'The "root-dir" option is missing.'); $options = array_replace(self::$defaultOptions, $options); if (!isset($options['path'])) { $options['path'] = $targetMethod->getClass()->getDirectory() . '/repository'; } 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['symlink'], 'The "symlink" option should be a boolean. Got: %s'); $path = Path::makeAbsolute($options['path'], $options['root-dir']); $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))); }
/** * {@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" : '')); }
/** * Creates the mapping. * * @param string $glob A glob for resources in the repository. * @param string $targetName The name of the install target. * @param string $webPath The web path of the resource in the install * target. * @param Uuid $uuid The UUID of the mapping. */ public function __construct($glob, $targetName, $webPath, Uuid $uuid = null) { Assert::stringNotEmpty($glob, 'The glob must be a non-empty string. Got: %s'); Assert::stringNotEmpty($targetName, 'The target name must be a non-empty string. Got: %s'); Assert::string($webPath, 'The web path must be a string. Got: %s'); $this->uuid = $uuid ?: Uuid::uuid4(); $this->glob = $glob; $this->targetName = $targetName; $this->webPath = '/' . trim($webPath, '/'); }
/** * Creates the mapping. * * @param string $glob A glob for resources in the repository. * @param string $serverName The name of the asset server. * @param string $serverPath The path of the resource in the document root * of the server. * @param Uuid $uuid The UUID of the mapping. */ public function __construct($glob, $serverName, $serverPath, Uuid $uuid = null) { Assert::stringNotEmpty($glob, 'The glob must be a non-empty string. Got: %s'); Assert::stringNotEmpty($serverName, 'The server name must be a non-empty string. Got: %s'); Assert::string($serverPath, 'The public path must be a string. Got: %s'); $this->uuid = $uuid ?: Uuid::uuid4(); $this->glob = $glob; $this->serverName = $serverName; $this->serverPath = '/' . trim($serverPath, '/'); }
/** * Creates a new server. * * @param string $name The name of the server. * @param string $installerName The name of the used installer. * @param string $documentRoot The document root of the server. * @param string $urlFormat The format of the generated resource URLs. * Include the placeholder "%s" for the * resource path relative to the document * root. * @param array $parameterValues Values for the parameters defined by the * installer descriptor. */ public function __construct($name, $installerName, $documentRoot, $urlFormat = self::DEFAULT_URL_FORMAT, array $parameterValues = array()) { Assert::stringNotEmpty($name, 'The server name must be a non-empty string. Got: %s'); Assert::stringNotEmpty($installerName, 'The installer name must be a non-empty string. Got: %s'); Assert::stringNotEmpty($documentRoot, 'The server location must be a non-empty string. Got: %s'); Assert::stringNotEmpty($urlFormat, 'The URL format must be a non-empty string. Got: %s'); $this->name = $name; $this->installerName = $installerName; $this->documentRoot = $documentRoot; $this->urlFormat = $urlFormat; $this->parameterValues = $parameterValues; }
/** * Creates a new installer descriptor. * * @param string $name The installer name. * @param string $className The fully-qualified class name * of the installer. * @param string|null $description The description of the installer. * @param InstallerParameter[] $parameters The installer parameters. */ public function __construct($name, $className, $description = null, array $parameters = array()) { Assert::stringNotEmpty($name, 'The installer name must be a non-empty string. Got: %s'); Assert::stringNotEmpty($className, 'The installer class must be a non-empty string. Got: %s'); Assert::nullOrStringNotEmpty($description, 'The installer description must be a non-empty string or null. Got: %s'); Assert::allIsInstanceOf($parameters, __NAMESPACE__ . '\\InstallerParameter'); $this->name = $name; $this->className = $className; $this->description = $description; foreach ($parameters as $parameter) { $this->parameters[$parameter->getName()] = $parameter; } }
/** * Creates the import statement. * * @param string $className The fully-qualified imported class name. * @param string|null $alias If not `null`, the class will be imported * with the given alias. */ public function __construct($className, $alias = null) { Assert::stringNotEmpty($className, 'The imported class name must be a non-empty string. Got: %s'); Assert::nullOrStringNotEmpty($className, 'The import alias must be a non-empty string or null. Got: %s'); $pos = strrpos($className, '\\'); if (false === $pos) { $this->namespaceName = ''; $this->shortClassName = $className; } else { $this->namespaceName = substr($className, 0, $pos); $this->shortClassName = substr($className, $pos + 1); } $this->alias = $alias; }
/** * {@inheritdoc} */ public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) { Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.'); if (!isset($options['path'])) { $options['path'] = $targetMethod->getClass()->getDirectory() . '/change-stream.json'; } Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s'); Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s'); $path = Path::makeAbsolute($options['path'], $options['root-dir']); $relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory()); $escPath = '__DIR__.' . var_export('/' . $relPath, true); $targetMethod->getClass()->addImport(new Import('Puli\\Repository\\ChangeStream\\JsonChangeStream')); $targetMethod->addBody(sprintf('$%s = new JsonChangeStream(%s);', $varName, $escPath)); }
/** * {@inheritdoc} */ public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) { Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.'); if (!isset($options['path'])) { $options['path'] = $targetMethod->getClass()->getDirectory() . '/bindings.json'; } Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s'); Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s'); $path = Path::makeAbsolute($options['path'], $options['root-dir']); $relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory()); $escPath = '__DIR__.' . var_export('/' . $relPath, true); $targetMethod->getClass()->addImport(new Import('Puli\\Discovery\\JsonDiscovery')); $targetMethod->getClass()->addImport(new Import('Puli\\Discovery\\Binding\\Initializer\\ResourceBindingInitializer')); $targetMethod->addBody(sprintf("\$%s = new JsonDiscovery(%s, array(\n new ResourceBindingInitializer(\$repo),\n));", $varName, $escPath)); }
/** * {@inheritdoc} */ public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) { $options = array_replace(self::$defaultOptions, $options); Assert::stringNotEmpty($options['host'], 'The "host" option must be a non-empty string. Got: %s'); Assert::integer($options['port'], 'The "port" option must be an integer. Got: %s'); $escHost = var_export($options['host'], true); $escPort = var_export($options['port'], true); $targetMethod->getClass()->addImports(array(new Import('Redis'), new Import('Webmozart\\KeyValueStore\\PhpRedisStore'))); $targetMethod->addBody(<<<EOF \$client = new Redis(); \$client->connect({$escHost}, {$escPort}); \${$varName} = new PhpRedisStore(\$client); EOF ); }
/** * {@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)); }
/** * {@inheritdoc} */ public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) { Assert::keyExists($options, 'bucket', 'The "bucket" option is missing.'); $options = array_replace(self::$defaultOptions, $options); Assert::stringNotEmpty($options['bucket'], 'The "bucket" option must be a non-empty string. Got: %s'); Assert::stringNotEmpty($options['host'], 'The "host" option must be a non-empty string. Got: %s'); Assert::integer($options['port'], 'The "port" option must be an integer. Got: %s'); $escBucket = var_export($options['bucket'], true); $escHost = var_export($options['host'], true); $escPort = var_export($options['port'], true); $targetMethod->getClass()->addImports(array(new Import('Basho\\Riak\\Riak'), new Import('Webmozart\\KeyValueStore\\RiakStore'))); $targetMethod->addBody(<<<EOF \$client = new Riak({$escHost}, {$escPort}); \${$varName} = new RiakStore({$escBucket}, \$client); EOF ); }
/** * Creates a new install target. * * @param string $name The name of the target. * @param string $installerName The name of the used installer. * @param string $location The location where resources are installed. * @param string $urlFormat The format of the generated resource URLs. Include the placeholder "%s" for the resource path relative to the target location. * @param array $parameterValues Values for the parameters defined by the installer descriptor. */ public function __construct($name, $installerName, $location, $urlFormat = self::DEFAULT_URL_FORMAT, array $parameterValues = array()) { Assert::stringNotEmpty($name, 'The target name must be a non-empty string. Got: %s'); Assert::stringNotEmpty($installerName, 'The installer name must be a non-empty string. Got: %s'); Assert::notEq($name, self::DEFAULT_TARGET, 'The target name must not be "' . self::DEFAULT_TARGET . '".'); Assert::stringNotEmpty($location, 'The target location must be a non-empty string. Got: %s'); Assert::stringNotEmpty($urlFormat, 'The target URL format must be a non-empty string. Got: %s'); $this->name = $name; $this->installerName = $installerName; $this->location = $location; $this->urlFormat = $urlFormat; $this->parameterValues = $parameterValues; }