|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webperf plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Monitor the performance of your webpages through real-world user timing data |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2019 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\webperf\base; |
|
12
|
|
|
|
|
13
|
|
|
use Craft; |
|
|
|
|
|
|
14
|
|
|
use craft\base\Model; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
use yii\base\InvalidArgumentException; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
|
|
|
|
|
19
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
20
|
|
|
* @package Webperf |
|
|
|
|
|
|
21
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
|
|
23
|
|
|
abstract class FluentModel extends Model |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
// Static Protected Methods |
|
27
|
|
|
// ========================================================================= |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Remove any properties that don't exist in the model |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $class |
|
|
|
|
|
|
33
|
|
|
* @param array $config |
|
|
|
|
|
|
34
|
|
|
*/ |
|
|
|
|
|
|
35
|
|
|
protected static function cleanProperties(string $class, array &$config) |
|
36
|
|
|
{ |
|
37
|
|
|
foreach ($config as $propName => $propValue) { |
|
38
|
|
|
if (!property_exists($class, $propName)) { |
|
39
|
|
|
unset($config[$propName]); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Public Methods |
|
45
|
|
|
// ========================================================================= |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add fluent getters/setters for this class |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $method The method name (property name) |
|
51
|
|
|
* @param array $args The arguments list |
|
52
|
|
|
* |
|
53
|
|
|
* @return mixed The value of the property |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __call($method, $args) |
|
56
|
|
|
{ |
|
57
|
|
|
try { |
|
58
|
|
|
$reflector = new \ReflectionClass(static::class); |
|
59
|
|
|
} catch (\ReflectionException $e) { |
|
60
|
|
|
Craft::error( |
|
61
|
|
|
$e->getMessage(), |
|
62
|
|
|
__METHOD__ |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
if (!$reflector->hasProperty($method)) { |
|
68
|
|
|
throw new InvalidArgumentException("Property {$method} doesn't exist"); |
|
69
|
|
|
} |
|
70
|
|
|
$property = $reflector->getProperty($method); |
|
71
|
|
|
if (empty($args)) { |
|
72
|
|
|
// Return the property |
|
73
|
|
|
return $property->getValue(); |
|
74
|
|
|
} |
|
75
|
|
|
// Set the property |
|
76
|
|
|
$property->setValue($this, $args[0]); |
|
77
|
|
|
|
|
78
|
|
|
// Make it chainable |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|