1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains only the QuoteController class. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace AppBundle\Controller; |
7
|
|
|
|
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
10
|
|
|
use Symfony\Component\Routing\Exception\InvalidParameterException; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A quick note: This tool is referred to as "bash" in much of the legacy code base. As such, |
15
|
|
|
* the terms "quote" and "bash" are used interchangeably here, so as to not break many conventions. |
16
|
|
|
*/ |
17
|
|
|
class QuoteController extends Controller |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get the tool's shortname. |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function getToolShortname() |
25
|
|
|
{ |
26
|
|
|
return 'bash'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Method for rendering the Bash Main Form. |
31
|
|
|
* This method redirects if valid parameters are found, making it a |
32
|
|
|
* valid form endpoint as well. |
33
|
|
|
* |
34
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request Given by Symfony |
35
|
|
|
* |
36
|
|
|
* @Route("/bash", name="bash") |
37
|
|
|
* @Route("/quote", name="quote") |
38
|
|
|
* |
39
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
40
|
|
|
*/ |
41
|
|
|
public function indexAction(Request $request) |
42
|
|
|
{ |
43
|
|
|
// Check to see if the quote is a param. If so, |
44
|
|
|
// redirect to the proper route. |
45
|
|
|
if ($request->query->get('id') != '') { |
46
|
|
|
return $this->redirectToRoute( |
47
|
|
|
"quoteID", |
48
|
|
|
["id"=>$request->query->get('id')] |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Oterwise render the form. |
53
|
|
|
return $this->render( |
54
|
|
|
'quote/index.html.twig', |
55
|
|
|
[ |
56
|
|
|
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'), |
57
|
|
|
'xtPage' => 'bash', |
58
|
|
|
'xtPageTitle' => 'tool-bash', |
59
|
|
|
'xtSubtitle' => 'tool-bash-desc', |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Method for rendering a random quote. |
66
|
|
|
* This should redirect to the /quote/{id} path below |
67
|
|
|
* |
68
|
|
|
* @Route("/quote/random", name="quoteRandom") |
69
|
|
|
* @Route("/bash/random", name="bashRandom") |
70
|
|
|
* |
71
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
72
|
|
|
*/ |
73
|
|
|
public function randomQuoteAction() |
74
|
|
|
{ |
75
|
|
|
// Choose a random quote by ID. |
76
|
|
|
// if we can't find the quotes, return back to the main form with |
77
|
|
|
// a flash notice. |
78
|
|
|
try { |
79
|
|
|
$id = rand(1, sizeof($this->getParameter("quotes"))); |
80
|
|
|
} catch (InvalidParameterException $e) { |
81
|
|
|
$this->addFlash("notice", ["noquotes"]); |
|
|
|
|
82
|
|
|
return $this->redirectToRoute("quote"); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->redirectToRoute("quoteID", ["id"=>$id]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Method to show all quotes. |
90
|
|
|
* |
91
|
|
|
* @Route("/quote/all", name="quoteAll") |
92
|
|
|
* @Route("/bash/all", name="bashAll") |
93
|
|
|
* |
94
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
95
|
|
|
*/ |
96
|
|
|
public function quoteAllAction() |
97
|
|
|
{ |
98
|
|
|
// Load up an array of all the quotes. |
99
|
|
|
// if we can't find the quotes, return back to the main form with |
100
|
|
|
// a flash notice. |
101
|
|
|
try { |
102
|
|
|
$quotes = $this->getParameter("quotes"); |
103
|
|
|
} catch (InvalidParameterException $e) { |
104
|
|
|
$this->addFlash("notice", ["noquotes"]); |
|
|
|
|
105
|
|
|
return $this->redirectToRoute("quote"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Render the page. |
109
|
|
|
return $this->render( |
110
|
|
|
'quote/all.html.twig', |
111
|
|
|
[ |
112
|
|
|
'base_dir' => realpath( |
113
|
|
|
$this->getParameter('kernel.root_dir') . '/..' |
114
|
|
|
), |
115
|
|
|
'xtPage' => 'bash', |
116
|
|
|
'quotes' => $quotes, |
117
|
|
|
] |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Method to render a single quote. |
123
|
|
|
* |
124
|
|
|
* @param int $id ID of the quote |
125
|
|
|
* |
126
|
|
|
* @Route("/quote/{id}", name="quoteID") |
127
|
|
|
* @Route("/bash/{id}", name="bashID") |
128
|
|
|
* |
129
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
130
|
|
|
*/ |
131
|
|
|
public function quoteAction($id) |
132
|
|
|
{ |
133
|
|
|
// Get the singular quote. |
134
|
|
|
// if we can't find the quotes, return back to the main form with |
135
|
|
|
// a flash notice. |
136
|
|
|
try { |
137
|
|
|
if (isset($this->getParameter("quotes")[$id])) { |
138
|
|
|
$text = $this->getParameter("quotes")[$id]; |
139
|
|
|
} else { |
140
|
|
|
throw new InvalidParameterException("Quote doesn't exist"); |
141
|
|
|
} |
142
|
|
|
} catch (InvalidParameterException $e) { |
143
|
|
|
$this->addFlash("notice", ["noquotes"]); |
|
|
|
|
144
|
|
|
return $this->redirectToRoute("quote"); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// If the text is undefined, that quote doesn't exist. |
148
|
|
|
// Redirect back to the main form. |
149
|
|
|
if (!isset($text)) { |
150
|
|
|
$this->addFlash("notice", ["noquotes"]); |
|
|
|
|
151
|
|
|
return $this->redirectToRoute("quote"); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Show the quote. |
155
|
|
|
return $this->render( |
156
|
|
|
'quote/view.html.twig', |
157
|
|
|
[ |
158
|
|
|
'base_dir' => realpath( |
159
|
|
|
$this->getParameter('kernel.root_dir') . '/..' |
160
|
|
|
), |
161
|
|
|
"xtPage" => "bash", |
162
|
|
|
"text" => $text, |
163
|
|
|
"id" => $id, |
164
|
|
|
] |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: