/**
  * Adds support for interceptors to an atomic operation.
  *
  * @param AtomicOperation                             $operation    The operation.
  * @param OperationInterceptor|OperationInterceptor[] $interceptors The interceptor(s).
  */
 public function __construct(AtomicOperation $operation, $interceptors)
 {
     $interceptors = is_array($interceptors) ? $interceptors : array($interceptors);
     Assert::allIsInstanceOf($interceptors, __NAMESPACE__ . '\\OperationInterceptor');
     $this->operation = $operation;
     $this->interceptors = $interceptors;
 }
 /**
  * {@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" : ''));
 }
    /**
     * {@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)));
    }
 /**
  * {@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 fromJson($jsonData, array $options = array())
 {
     Assert::isInstanceOf($jsonData, 'stdClass');
     $moduleFile = new ModuleFile(null, isset($options['path']) ? $options['path'] : null);
     $this->addJsonToModuleFile($jsonData, $moduleFile);
     return $moduleFile;
 }
 /**
  * Creates the environment.
  *
  * @param string|null              $homeDir         The path to the home
  *                                                  directory or `null` if
  *                                                  none exists.
  * @param string                   $rootDir         The path to the project's
  *                                                  root directory.
  * @param Config                   $config          The configuration.
  * @param RootPackageFile          $rootPackageFile The root package file.
  * @param ConfigFile               $configFile      The configuration file or
  *                                                  `null` if none exists.
  * @param EventDispatcherInterface $dispatcher      The event dispatcher.
  */
 public function __construct($homeDir, $rootDir, Config $config, RootPackageFile $rootPackageFile, ConfigFile $configFile = null, EventDispatcherInterface $dispatcher = null)
 {
     Assert::directory($rootDir, 'The root directory %s is not a directory.');
     parent::__construct($homeDir, $config, $configFile, $dispatcher);
     $this->rootDir = Path::canonicalize($rootDir);
     $this->rootPackageFile = $rootPackageFile;
 }
Example #7
0
 /**
  * Creates a new package.
  *
  * @param PackageFile|null $packageFile The package file or `null` if the
  *                                      package file could not be loaded.
  * @param string           $installPath The absolute install path.
  * @param InstallInfo|null $installInfo The install info of this package.
  * @param Exception[]      $loadErrors  The errors that happened during
  *                                      loading of the package, if any.
  */
 public function __construct(PackageFile $packageFile = null, $installPath, InstallInfo $installInfo = null, array $loadErrors = array())
 {
     Assert::absoluteSystemPath($installPath);
     Assert::true($packageFile || $loadErrors, 'The load errors must be passed if the package file is null.');
     Assert::allIsInstanceOf($loadErrors, 'Exception');
     // If a package name was set during installation, that name wins over
     // the predefined name in the puli.json file (if any)
     $this->name = $installInfo && null !== $installInfo->getPackageName() ? $installInfo->getPackageName() : ($packageFile ? $packageFile->getPackageName() : null);
     if (null === $this->name) {
         $this->name = $this->getDefaultName();
     }
     // The path is stored both here and in the install info. While the
     // install info contains the path as it is stored in the install file
     // (i.e. relative or absolute), the install path of the package is
     // always an absolute path.
     $this->installPath = $installPath;
     $this->installInfo = $installInfo;
     $this->packageFile = $packageFile;
     $this->loadErrors = $loadErrors;
     if (!file_exists($installPath)) {
         $this->state = PackageState::NOT_FOUND;
     } elseif (count($loadErrors) > 0) {
         $this->state = PackageState::NOT_LOADABLE;
     } else {
         $this->state = PackageState::ENABLED;
     }
 }
Example #8
0
 /**
  * Creates the context.
  *
  * @param string|null              $homeDir    The path to the home directory
  *                                             or `null` if none exists.
  * @param Config                   $config     The configuration.
  * @param ConfigFile               $configFile The configuration file or
  *                                             `null` if none exists.
  * @param EventDispatcherInterface $dispatcher The event dispatcher.
  */
 public function __construct($homeDir, Config $config, ConfigFile $configFile = null, EventDispatcherInterface $dispatcher = null)
 {
     Assert::nullOrDirectory($homeDir, 'The home directory %s is not a directory.');
     $this->homeDir = $homeDir ? Path::canonicalize($homeDir) : null;
     $this->config = $config;
     $this->dispatcher = $dispatcher ?: new EventDispatcher();
     $this->configFile = $configFile;
 }
Example #9
0
 /**
  * Creates a new configuration file.
  *
  * @param string|null $path       The path where the configuration file is stored
  *                                or `null` if this configuration is not stored
  *                                on the file system.
  * @param Config      $baseConfig The configuration that the configuration will
  *                                inherit its values from.
  *
  * @throws InvalidArgumentException If the path is not a string or empty.
  */
 public function __construct($path = null, Config $baseConfig = null)
 {
     Assert::nullOrString($path, 'The path to the configuration file should be a string or null. Got: %s');
     Assert::nullOrNotEmpty($path, 'The path to the configuration file should not be empty.');
     // Inherit from default configuration
     $this->config = new Config($baseConfig);
     $this->path = $path;
 }
 /**
  * {@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);
     $path = Path::makeAbsolute($options['path'], $options['rootDir']);
     $relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
     $targetMethod->getClass()->addImport(new Import('Webmozart\\KeyValueStore\\JsonFileStore'));
     $targetMethod->addBody(sprintf('$%s = new JsonFileStore(%s, %s);', $varName, '__DIR__.' . var_export('/' . $relPath, true), $options['cache'] ? 'true' : 'false'));
 }
Example #11
0
 /**
  * Creates the context.
  *
  * @param string|null                   $homeDir        The path to the
  *                                                      home directory or
  *                                                      `null` if none
  *                                                      exists.
  * @param string                        $rootDir        The path to the
  *                                                      project's root
  *                                                      directory.
  * @param Config                        $config         The configuration.
  * @param RootModuleFile                $rootModuleFile The root module
  *                                                      file.
  * @param ConfigFile|null               $configFile     The configuration
  *                                                      file or `null` if
  *                                                      none exists.
  * @param EventDispatcherInterface|null $dispatcher     The event
  *                                                      dispatcher.
  * @param string                        $env            The environment
  *                                                      that Puli is
  *                                                      running in.
  */
 public function __construct($homeDir, $rootDir, Config $config, RootModuleFile $rootModuleFile, ConfigFile $configFile = null, EventDispatcherInterface $dispatcher = null, $env = Environment::DEV)
 {
     Assert::directory($rootDir, 'The root directory %s is not a directory.');
     Assert::oneOf($env, Environment::all(), 'The environment must be one of: %2$s. Got: %s');
     parent::__construct($homeDir, $config, $configFile, $dispatcher);
     $this->rootDir = Path::canonicalize($rootDir);
     $this->rootModuleFile = $rootModuleFile;
     $this->env = $env;
 }
 /**
  * Adds an attribute to the name.
  *
  * @param string $name  The attribute name. Must start with a letter and
  *                      contain letters, digits and hyphens only.
  * @param string $value The attribute value. Any non-empty string is
  *                      allowed.
  *
  * @see merge()
  */
 public function add($name, $value)
 {
     Assert::string($name, 'The attribute name must be a string. Got: %s');
     Assert::notEmpty($name, 'The attribute name must not be empty.');
     Assert::startsWithLetter($name, 'The attribute name %s must start with a letter.');
     Assert::true((bool) preg_match('~^[a-zA-Z][a-zA-Z0-9\\-]*$~', $name), sprintf('The attribute name must contain letters, numbers and hyphens only. Got: "%s"', $name));
     Assert::string($value, 'The attribute value must be a string. Got: %s');
     $this->attributes[$name] = $value;
 }
Example #13
0
 /**
  * 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, '/');
 }
Example #14
0
 /**
  * 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 migration manager.
  *
  * @param JsonMigration[] $migrations
  */
 public function __construct(array $migrations)
 {
     Assert::allIsInstanceOf($migrations, __NAMESPACE__ . '\\JsonMigration');
     foreach ($migrations as $migration) {
         $this->migrationsByOriginVersion[$migration->getOriginVersion()] = $migration;
         $this->migrationsByTargetVersion[$migration->getTargetVersion()] = $migration;
     }
     uksort($this->migrationsByOriginVersion, 'version_compare');
     uksort($this->migrationsByTargetVersion, 'version_compare');
 }
 /**
  * {@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);');
 }
Example #17
0
 /**
  * Writes a {@link Clazz} instance to a file.
  *
  * The directory of the class must have been set.
  *
  * @param Clazz $class The class to write.
  */
 public function writeClass(Clazz $class)
 {
     Assert::notEmpty($class->getDirectory(), 'The directory of the written class must not be empty.');
     ob_start();
     require __DIR__ . '/../../res/template/Class.tpl.php';
     $source = "<?php\n" . ob_get_clean();
     if (!file_exists($class->getDirectory())) {
         mkdir($class->getDirectory(), 0777, true);
     }
     file_put_contents($class->getFilePath(), $source);
 }
Example #18
0
 /**
  * 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;
 }
 /**
  * {@inheritdoc}
  */
 public function fromJson($jsonData, array $options = array())
 {
     $path = isset($options['path']) ? $options['path'] : null;
     $baseConfig = isset($options['baseConfig']) ? $options['baseConfig'] : null;
     Assert::isInstanceOf($jsonData, 'stdClass');
     Assert::nullOrString($path, 'The "path" option should be null or a string. Got: %s');
     Assert::nullOrIsInstanceOf($baseConfig, 'Puli\\Manager\\Api\\Config\\Config', 'The "baseConfig" option should be null or an instance of %2$s. Got: %s');
     $moduleFile = new RootModuleFile(null, $path, $baseConfig);
     $this->addJsonToModuleFile($jsonData, $moduleFile);
     $this->addJsonToRootModuleFile($jsonData, $moduleFile);
     return $moduleFile;
 }
 /**
  * {@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));
 }
Example #21
0
 /**
  * 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;
     }
 }
 /**
  * {@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));
 }
Example #23
0
 /**
  * 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;
 }
Example #24
0
 /**
  * {@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 fromJson($jsonData, array $options = array())
 {
     $path = isset($options['path']) ? $options['path'] : null;
     $baseConfig = isset($options['baseConfig']) ? $options['baseConfig'] : null;
     Assert::isInstanceOf($jsonData, 'stdClass');
     Assert::nullOrString($path, 'The "path" option should be null or a string. Got: %s');
     Assert::nullOrIsInstanceOf($baseConfig, 'Puli\\Manager\\Api\\Config\\Config', 'The "baseConfig" option should be null or an instance of %2$s. Got: %s');
     $configFile = new ConfigFile($path, $baseConfig);
     $config = $configFile->getConfig();
     $jsonData = $this->objectsToArrays($jsonData);
     foreach ($jsonData as $key => $value) {
         $config->set($key, $value);
     }
     return $configFile;
 }
Example #26
0
 /**
  * Creates the parameter.
  *
  * @param string      $name         The parameter name.
  * @param int         $flags        A bitwise combination of the flag
  *                                  constants in this class.
  * @param null        $defaultValue The default value of the parameter. Must
  *                                  only be set for optional parameters.
  * @param string|null $description  A human-readable description.
  */
 public function __construct($name, $flags = self::OPTIONAL, $defaultValue = null, $description = null)
 {
     Assert::parameterName($name);
     Assert::nullOrInteger($flags, 'The parameter "$flags" must be an integer or null. Got: %s');
     Assert::nullOrParameterValue($defaultValue);
     Assert::nullOrString($description, 'The parameter description must be a string or null. Got: %s');
     Assert::nullOrNotEmpty($description, 'The parameter description must not be empty.');
     if ($flags & self::REQUIRED && null !== $defaultValue) {
         throw new RuntimeException('Required parameters cannot have default values.');
     }
     $this->name = $name;
     $this->flags = (int) $flags;
     $this->defaultValue = $defaultValue;
     $this->description = $description;
 }
    /**
     * {@inheritdoc}
     */
    public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
    {
        $options = array_replace(self::$defaultOptions, $options);
        Assert::string($options['host'], 'The host must be a string. Got: %s');
        Assert::integer($options['port'], 'The port 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 write($path, $contents)
 {
     Assert::notEmpty($path, 'Cannot write to an empty path.');
     if (is_dir($path)) {
         throw new StorageException(sprintf('Cannot write %s: Is a directory.', $path));
     }
     if (!is_dir($dir = Path::getDirectory($path))) {
         $filesystem = new Filesystem();
         $filesystem->mkdir($dir);
     }
     if (false === ($numBytes = @file_put_contents($path, $contents))) {
         $error = error_get_last();
         throw new StorageException(sprintf('Could not write %s: %s.', $path, $error['message']));
     }
     return $numBytes;
 }
Example #30
0
    /**
     * {@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('Predis\\Client'), new Import('Webmozart\\KeyValueStore\\PredisStore')));
        $targetMethod->addBody(<<<EOF
\$client = new Client(array(
    'host' => {$escHost},
    'port' => {$escPort},
));
\${$varName} = new PredisStore(\$client);
EOF
);
    }