|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Moo\FlashCardBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Moo\FlashCardBundle\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* AbstractCommand contains abstracted/helper methods that are needed for a command line object. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractCommand extends ContainerAwareCommand |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* An instance of Validator |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Symfony\Component\Validator\Validator |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $validator; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Shortcut to return the Doctrine Registry service. |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Doctrine\Bundle\DoctrineBundle\Registry |
|
35
|
|
|
*/ |
|
36
|
5 |
|
protected function getDoctrine() |
|
37
|
|
|
{ |
|
38
|
5 |
|
return $this->getContainer()->get('doctrine'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Shortcut to return an entity repository class. |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Doctrine\ORM\EntityRepository |
|
45
|
|
|
*/ |
|
46
|
9 |
|
protected function getRepository($name) |
|
47
|
|
|
{ |
|
48
|
9 |
|
return $this->getContainer()->get('moo_flashcard.' . $name . '.repository'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Shortcut to the translator method |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $message |
|
55
|
|
|
* @param array $params |
|
56
|
|
|
*/ |
|
57
|
7 |
|
protected function trans($message, $params = []) |
|
58
|
|
|
{ |
|
59
|
7 |
|
return $this->getContainer()->get('translator')->trans($message, $params); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Writes an error message to the output and adds a newline at the end. |
|
64
|
|
|
* |
|
65
|
|
|
* @param OutputInterface $output |
|
66
|
|
|
* @param string $message |
|
67
|
|
|
* @param array $params |
|
68
|
|
|
* |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
2 |
|
protected function error(OutputInterface $output, $message, $params = []) |
|
72
|
|
|
{ |
|
73
|
2 |
|
return $output->writeln('<error>' . $this->trans($message, $params) . '</error>'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Writes an success message to the output and adds a newline at the end. |
|
78
|
|
|
* |
|
79
|
|
|
* @param OutputInterface $output |
|
80
|
|
|
* @param string $message |
|
81
|
|
|
* @param array $params |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
5 |
|
protected function success(OutputInterface $output, $message, $params = []) |
|
86
|
|
|
{ |
|
87
|
5 |
|
return $output->writeln('<info>' . $this->trans($message, $params) . '</info>'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get instance of Validator |
|
92
|
|
|
* |
|
93
|
|
|
* @return \Symfony\Component\Validator\Validator |
|
94
|
|
|
*/ |
|
95
|
12 |
|
protected function getValidator() |
|
96
|
|
|
{ |
|
97
|
12 |
|
if (null === $this->validator) { |
|
98
|
12 |
|
$this->validator = $this->getContainer()->get('validator'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
12 |
|
return $this->validator; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Validate a property of an entity |
|
106
|
|
|
* |
|
107
|
|
|
* @param object $entity |
|
108
|
|
|
* @param string $propertyName |
|
109
|
|
|
* @param null $groups |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
4 |
|
protected function validate($entity, $propertyName, $groups = null) |
|
114
|
|
|
{ |
|
115
|
4 |
|
$violations = $this->getValidator()->validateProperty($entity, $propertyName, $groups); |
|
116
|
4 |
|
if (count($violations) > 0) { |
|
117
|
2 |
|
return $violations[0]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|