class JmesRuntimeFactory in Feeds extensible parsers 8
Defines a factory for generating JMESPath runtime objects.
Hierarchy
- class \Drupal\feeds_ex\JmesRuntimeFactory implements JmesRuntimeFactoryInterface
Expanded class hierarchy of JmesRuntimeFactory
2 files declare their use of JmesRuntimeFactory
- JmesPathParser.php in src/
Feeds/ Parser/ JmesPathParser.php - JmesRuntimeFactoryTest.php in tests/
src/ Unit/ JmesRuntimeFactoryTest.php
File
- src/
JmesRuntimeFactory.php, line 14
Namespace
Drupal\feeds_exView source
class JmesRuntimeFactory implements JmesRuntimeFactoryInterface {
/**
* {@inheritdoc}
*/
public function createRuntime($type = NULL) {
switch ($type) {
case static::AST:
return $this
->createAstRuntime();
case static::COMPILER:
default:
try {
return $this
->createCompilerRuntime(\Drupal::service('file_system')
->realpath($this
->getCompileDirectory()));
} catch (RuntimeException $e) {
// Fallback to AstRuntime if creating a CompilerRuntime failed.
return $this
->createRuntime(static::AST);
}
}
}
/**
* Creates a runtime object of type \JmesPath\AstRuntime.
*/
public function createAstRuntime() {
return new AstRuntime();
}
/**
* Creates a runtime object of type \JmesPath\CompilerRuntime.
*
* @param string $directory
* The compile directory.
*/
public function createCompilerRuntime($directory) {
return new CompilerRuntime($directory);
}
/**
* Returns the compilation directory.
*
* @return string
* The directory JmesPath uses to store generated code.
*/
protected function getCompileDirectory() {
// Look for a previous directory.
$directory = \Drupal::state()
->get('feeds_ex_jmespath_compile_dir');
// The temp directory doesn't exist, or has moved.
if (!$this
->validateCompileDirectory($directory)) {
$directory = $this
->generateCompileDirectory();
\Drupal::state()
->set('feeds_ex_jmespath_compile_dir', $directory);
// Creates the directory with the correct perms. We don't check the
// return value since if it didn't work, there's nothing we can do. We
// just fallback to the AstRuntime anyway.
$this
->validateCompileDirectory($directory);
}
return $directory;
}
/**
* Generates a directory path to store auto-generated PHP files.
*
* @return string
* A temp directory path.
*/
protected function generateCompileDirectory() {
$prefix = Crypt::randomBytesBase64(40);
return 'temporary://' . $prefix . '_feeds_ex_jmespath_dir';
}
/**
* Validates that a compile directory exists and is valid.
*
* @param string $directory
* A directory path.
*
* @return bool
* True if the directory exists and is writable, false if not.
*/
protected function validateCompileDirectory($directory) {
if (!$directory) {
return FALSE;
}
return \Drupal::service('file_system')
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
JmesRuntimeFactory:: |
public | function | Creates a runtime object of type \JmesPath\AstRuntime. | |
JmesRuntimeFactory:: |
public | function | Creates a runtime object of type \JmesPath\CompilerRuntime. | |
JmesRuntimeFactory:: |
public | function |
Creates a runtime object. Overrides JmesRuntimeFactoryInterface:: |
|
JmesRuntimeFactory:: |
protected | function | Generates a directory path to store auto-generated PHP files. | |
JmesRuntimeFactory:: |
protected | function | Returns the compilation directory. | |
JmesRuntimeFactory:: |
protected | function | Validates that a compile directory exists and is valid. | |
JmesRuntimeFactoryInterface:: |
constant | Represents \JmesPath\AstRuntime. | ||
JmesRuntimeFactoryInterface:: |
constant | Represents \JmesPath\CompilerRuntime. |