php-vfxapply/src/Preset.php

119 lines
2.9 KiB
PHP

<?php
namespace VfxApply;
use Symfony\Component\Yaml\Yaml;
class Preset
{
/** @var string Preset name */
protected $name;
/** @var string The group this preset belong to, for organizing */
protected $group;
/** @var string The plugin this preset uses */
protected $plugin;
/** @var array The properties are used by the plugin */
protected $props = [];
/** @var array The params can be assigned from user data */
protected $params = [];
/** @var string The directory of the preset */
protected $directory = null;
public static function createFromFile($filename)
{
$body = file_get_contents($filename);
$conf = Yaml::parse($body);
if (!array_key_exists('preset',$conf)) {
throw new \Exception("File does not appear to be a valid preset");
}
return new Preset($conf['preset'], dirname($filename));
}
/**
* Constructor
*
* @param array $preset The preset data
* @param string $directory The directory containing the preset
*/
public function __construct(array $preset, $directory)
{
$this->name = $preset['name'];
$this->group = empty($preset['group'])?null:$preset['group'];
$this->plugin = $preset['plugin'];
$this->props = $preset['props'];
$this->params = empty($preset['params'])?null:$preset['params'];
$this->directory = $directory;
}
/**
*
* @return string Preset name
*/
public function getName()
{
return $this->name;
}
/**
*
* @return string Preset logical group
*/
public function getGroup()
{
return $this->group;
}
/**
* Get the plugin name, for resolving a plugin instance to use.
*
* @return string The plugin name/id
*/
public function getPlugin()
{
return $this->plugin;
}
/**
*
* @param string $prop The property to get
* @return mixed The property if it exists, null otherwise
*/
public function get($prop)
{
if (!array_key_exists($prop, $this->props)) {
return null;
}
return $this->props[$prop];
}
/**
*
* @param string $prop The parameter to get
* @return mixed The parameter if it exists, throws exception otherwise
* @throws Exception
*/
public function getParam($param)
{
if (!array_key_exists($param, $this->params)) {
throw new \Exception("No such param defined in preset: {$param}");
}
return $this->params[$param];
}
/**
* Return the directory containing this preset, for referencing resources
* bundled with presets.
*
* @return string The root directory
*/
public function getDirectory()
{
return $this->directory;
}
public function getResourcePath($file)
{
return $this->directory.DIRECTORY_SEPARATOR.$file;
}
}