1: <?php
2: namespace Azalea\Selenium\Toolkit;
3:
4: class ResourceLoader
5: {
6: protected static $basePath = "";
7:
8: public static function setBasePath($path)
9: {
10: self::$basePath = $path;
11: }
12:
13: public static function parse($filename)
14: {
15: $filepath = self::$basePath."/".$filename;
16: if (!file_exists($filepath)) {
17: throw new \InvalidArgumentException("File does not exist: ".$filepath);
18: }
19:
20: $contents = file_get_contents($filepath);
21:
22: $contents = preg_replace('!/\*.*?\*/!s', '', $contents);
23:
24: $contents = preg_replace('/\n\s*\n/', "\n", $contents);
25:
26: $config = json_decode($contents, true);
27: if (!$config) {
28: throw new \InvalidArgumentException("Could not parse json file: ".$filepath);
29: }
30:
31: return $config;
32: }
33:
34: public static function load($filename, $driver, $args = array())
35: {
36: $config = self::parse($filename);
37: return self::create($config, $driver, $args);
38: }
39:
40: public static function create($config, $driver, $args = array())
41: {
42: $type = "\\Azalea\\Selenium\\Toolkit\\View";
43: if (isset($config['type'])) {
44: $type = $config['type'];
45: $baseNamespace = "\\Azalea\\Selenium\\Toolkit\\Components\\";
46: if (substr($type, 0, 1) !== "\\") {
47: $type = $baseNamespace.$type;
48: }
49: }
50: return new $type($driver, $config, $args);
51: }
52: }
53:
54: ?>