@@ -6,34 +6,34 @@ |
||
6 | 6 | |
7 | 7 | class IndexController extends BaseController { |
8 | 8 | |
9 | - /** |
|
10 | - * |
|
11 | - * @var \Models\User\UserRepository $UserRepository |
|
12 | - */ |
|
13 | - protected $UserRepository; |
|
14 | - |
|
15 | - protected $userListModule; |
|
16 | - |
|
17 | - /** |
|
18 | - * IndexController, constructed by container |
|
19 | - * |
|
20 | - * @param Twig_Environment $twig |
|
21 | - */ |
|
22 | - public function __construct( UserList $userListModule) { |
|
23 | - |
|
24 | - $this->userListModule = $userListModule; |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * Return index page (/) |
|
29 | - * |
|
30 | - * @param array $args |
|
31 | - * @return Response |
|
32 | - */ |
|
33 | - public function get($args) { |
|
34 | - return $this->render('pages/index.html.twig', [ |
|
35 | - "content" => $this->userListModule->process($args), |
|
36 | - ]); |
|
37 | - } |
|
9 | + /** |
|
10 | + * |
|
11 | + * @var \Models\User\UserRepository $UserRepository |
|
12 | + */ |
|
13 | + protected $UserRepository; |
|
14 | + |
|
15 | + protected $userListModule; |
|
16 | + |
|
17 | + /** |
|
18 | + * IndexController, constructed by container |
|
19 | + * |
|
20 | + * @param Twig_Environment $twig |
|
21 | + */ |
|
22 | + public function __construct( UserList $userListModule) { |
|
23 | + |
|
24 | + $this->userListModule = $userListModule; |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * Return index page (/) |
|
29 | + * |
|
30 | + * @param array $args |
|
31 | + * @return Response |
|
32 | + */ |
|
33 | + public function get($args) { |
|
34 | + return $this->render('pages/index.html.twig', [ |
|
35 | + "content" => $this->userListModule->process($args), |
|
36 | + ]); |
|
37 | + } |
|
38 | 38 | |
39 | 39 | } |
@@ -12,76 +12,76 @@ |
||
12 | 12 | class UserList extends Controllers\ModuleController implements Controllers\ModuleInterface |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * |
|
17 | - * @var \Core\Models\User\UserRepository |
|
18 | - */ |
|
19 | - protected $userRepository; |
|
20 | - |
|
21 | - function __construct(\Core\Models\User\UserRepository $userRepository) |
|
22 | - { |
|
23 | - $this->userRepository = $userRepository; |
|
24 | - } |
|
25 | - |
|
26 | - public function process($args = []) |
|
27 | - { |
|
28 | - |
|
29 | - $this->logger->debug('UserList start'); |
|
30 | - |
|
31 | - if ($args['fn'] == 'add') { |
|
32 | - $this->add(); |
|
33 | - } |
|
34 | - |
|
35 | - $Users = $this->userRepository->findMany(); |
|
36 | - |
|
37 | - $table = Table::create(); |
|
38 | - $table->addColNames([0, 1, 2]); |
|
39 | - $table->addClass('table table-striped'); |
|
40 | - $table->thead() |
|
41 | - ->addRowName('head row') |
|
42 | - ->th('head row', 0, 'Id') |
|
43 | - ->th('head row', 1, 'Имя') |
|
44 | - ->th('head row', 2, 'Email'); |
|
45 | - $i = 0; |
|
46 | - foreach ($Users as $User) { |
|
47 | - $table->addRow($i)->tdMultiple([ |
|
48 | - $User->getId(), |
|
49 | - $User->getName(), |
|
50 | - $User->getEmail()]); |
|
51 | - $i++; |
|
52 | - } |
|
53 | - |
|
54 | - return $this->render('default', [ |
|
55 | - 'table' => $table->render() |
|
56 | - ]); |
|
57 | - } |
|
58 | - |
|
59 | - protected function add() |
|
60 | - { |
|
61 | - |
|
62 | - try { |
|
63 | - |
|
64 | - $User = $this->userRepository->build(); |
|
65 | - $User->setEmail('[email protected]'); |
|
66 | - |
|
67 | - if (!$this->UserRepository->save($User)) { |
|
68 | - throw new Exception('User not save'); |
|
69 | - } else { |
|
70 | - $this->logger->info("Пользователь успешно сохранен!"); |
|
71 | - } |
|
72 | - } catch(\Frameworkless\Exceptions\ValidationException $ex) { |
|
73 | - |
|
74 | - foreach ($ex->getFailures() as $failure) { |
|
75 | - $this->logger->error("Property " . $failure->getPropertyPath() . ": " . $failure->getMessage() . "\n"); |
|
76 | - } |
|
77 | - |
|
78 | - $this->logger->info("Произошла ошибка при сохранении пользователя"); |
|
79 | - } catch(\Exception $ex) { |
|
80 | - |
|
81 | - $this->logger->error("system error:" . $ex->getMessage()); |
|
82 | - |
|
83 | - $this->logger->info("Произошла ошибка при сохранении пользователя"); |
|
84 | - } |
|
85 | - |
|
86 | - } |
|
15 | + /** |
|
16 | + * |
|
17 | + * @var \Core\Models\User\UserRepository |
|
18 | + */ |
|
19 | + protected $userRepository; |
|
20 | + |
|
21 | + function __construct(\Core\Models\User\UserRepository $userRepository) |
|
22 | + { |
|
23 | + $this->userRepository = $userRepository; |
|
24 | + } |
|
25 | + |
|
26 | + public function process($args = []) |
|
27 | + { |
|
28 | + |
|
29 | + $this->logger->debug('UserList start'); |
|
30 | + |
|
31 | + if ($args['fn'] == 'add') { |
|
32 | + $this->add(); |
|
33 | + } |
|
34 | + |
|
35 | + $Users = $this->userRepository->findMany(); |
|
36 | + |
|
37 | + $table = Table::create(); |
|
38 | + $table->addColNames([0, 1, 2]); |
|
39 | + $table->addClass('table table-striped'); |
|
40 | + $table->thead() |
|
41 | + ->addRowName('head row') |
|
42 | + ->th('head row', 0, 'Id') |
|
43 | + ->th('head row', 1, 'Имя') |
|
44 | + ->th('head row', 2, 'Email'); |
|
45 | + $i = 0; |
|
46 | + foreach ($Users as $User) { |
|
47 | + $table->addRow($i)->tdMultiple([ |
|
48 | + $User->getId(), |
|
49 | + $User->getName(), |
|
50 | + $User->getEmail()]); |
|
51 | + $i++; |
|
52 | + } |
|
53 | + |
|
54 | + return $this->render('default', [ |
|
55 | + 'table' => $table->render() |
|
56 | + ]); |
|
57 | + } |
|
58 | + |
|
59 | + protected function add() |
|
60 | + { |
|
61 | + |
|
62 | + try { |
|
63 | + |
|
64 | + $User = $this->userRepository->build(); |
|
65 | + $User->setEmail('[email protected]'); |
|
66 | + |
|
67 | + if (!$this->UserRepository->save($User)) { |
|
68 | + throw new Exception('User not save'); |
|
69 | + } else { |
|
70 | + $this->logger->info("Пользователь успешно сохранен!"); |
|
71 | + } |
|
72 | + } catch(\Frameworkless\Exceptions\ValidationException $ex) { |
|
73 | + |
|
74 | + foreach ($ex->getFailures() as $failure) { |
|
75 | + $this->logger->error("Property " . $failure->getPropertyPath() . ": " . $failure->getMessage() . "\n"); |
|
76 | + } |
|
77 | + |
|
78 | + $this->logger->info("Произошла ошибка при сохранении пользователя"); |
|
79 | + } catch(\Exception $ex) { |
|
80 | + |
|
81 | + $this->logger->error("system error:" . $ex->getMessage()); |
|
82 | + |
|
83 | + $this->logger->info("Произошла ошибка при сохранении пользователя"); |
|
84 | + } |
|
85 | + |
|
86 | + } |
|
87 | 87 | } |