|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiAPI Yii2 base project for building API |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hiapi |
|
6
|
|
|
* @package hiapi |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiapi\commands; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
|
|
15
|
|
|
abstract class BaseCommand extends \yii\base\Model |
|
16
|
|
|
{ |
|
17
|
|
|
protected $entityClass; |
|
18
|
|
|
|
|
19
|
|
|
protected static $handler; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct($entityClass, $config = []) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->entityClass = $entityClass; |
|
24
|
|
|
parent::__construct($config); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public static function getHandler() |
|
28
|
|
|
{ |
|
29
|
|
|
if (!is_object(static::$handler)) { |
|
30
|
|
|
static::$handler = Yii::createObject(static::$handler); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return static::$handler; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function setHandler($value) |
|
37
|
|
|
{ |
|
38
|
|
|
static::$handler = $value; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function loadFromRequest($request) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->load($this->getRequestData($request), ''); |
|
44
|
|
|
$this->validate(); |
|
45
|
|
|
|
|
46
|
|
|
return $this->hasErrors() ? $this->getErrors() : null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getRequestData($request) |
|
50
|
|
|
{ |
|
51
|
|
|
$get = $request->get(); |
|
52
|
|
|
$post = $request->post(); |
|
53
|
|
|
|
|
54
|
|
|
return array_merge($get, $post); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getEntityClass() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->entityClass; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getRecordClass() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->getRepository()->getRecordClass(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected $_repository; |
|
68
|
|
|
|
|
69
|
|
|
public function getRepository() |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->_repository === null) { |
|
72
|
|
|
$this->_repository = $this->findRepository(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->_repository; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function findRepository() |
|
79
|
|
|
{ |
|
80
|
|
|
return Yii::$app->entityManager->getRepository($this->entityClass); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|