1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\php\symfony; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
8
|
|
|
use Ajax\service\JString; |
9
|
|
|
class JsUtils extends \Ajax\JsUtils{ |
10
|
|
|
|
11
|
|
|
public function getUrl($url){ |
12
|
|
|
//$request = Request::createFromGlobals(); |
|
|
|
|
13
|
|
|
$router=$this->getInjected(); |
14
|
|
|
if(isset($router)){ |
15
|
|
|
try { |
16
|
|
|
$url=$router->generate($url); |
17
|
|
|
}catch (\Exception $e){ |
18
|
|
|
return $router->getContext()->getBaseUrl(); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
return $url; |
22
|
|
|
} |
23
|
|
|
public function addViewElement($identifier,$content,&$view){ |
24
|
|
|
if(\is_array($view)){ |
25
|
|
|
if(\array_key_exists("q", $view)===false){ |
26
|
|
|
$view["q"]=array(); |
27
|
|
|
} |
28
|
|
|
$view["q"][$identifier]=$content; |
29
|
|
|
}elseif($view instanceof \Twig_Environment){ |
|
|
|
|
30
|
|
|
$vars=$view->getGlobals(); |
31
|
|
|
if(\array_key_exists("q", $vars)===false){ |
32
|
|
|
$vars["q"]=array(); |
33
|
|
|
} |
34
|
|
|
$vars["q"][$identifier]=$content; |
35
|
|
|
$view->addGlobal("q",$vars["q"]); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function createScriptVariable(&$view,$view_var, $output){ |
40
|
|
|
$this->addVariable($view_var, $output, $view); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function addVariable($key,$value,&$view){ |
44
|
|
|
if(\is_array($view)){ |
45
|
|
|
$view[$key]=$value; |
46
|
|
|
}elseif($view instanceof \Twig_Environment){ |
|
|
|
|
47
|
|
|
$view->addGlobal($key,$value); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Symfony\Component\DependencyInjection\ContainerInterface $initialControllerInstance |
53
|
|
|
* @param string $controllerName |
54
|
|
|
* @param string $actionName |
55
|
|
|
* @param array $params |
56
|
|
|
* @see \Ajax\JsUtils::forward() |
57
|
|
|
*/ |
58
|
|
|
public function forward($initialControllerInstance,$controllerName,$actionName,$params=array()){ |
59
|
|
|
$path=$params; |
60
|
|
|
$request = $initialControllerInstance->get('request_stack')->getCurrentRequest(); |
61
|
|
|
$path['_forwarded'] = $request->attributes; |
62
|
|
|
$path['_controller'] = $controllerName.":".$actionName; |
63
|
|
|
$subRequest = $request->duplicate([], null, $path); |
64
|
|
|
$response= $initialControllerInstance->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST); |
65
|
|
|
return $response->getContent(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function renderContent($initialControllerInstance,$viewName, $params=NULL) { |
69
|
|
|
if ($initialControllerInstance->has('templating')) { |
70
|
|
|
return $initialControllerInstance->get('templating')->render($viewName, $params); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!$initialControllerInstance->has('twig')) { |
74
|
|
|
throw new \LogicException('You can not use the "renderView" method if the Templating Component or the Twig Bundle are not available.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $initialControllerInstance->get('twig')->render($viewName, $params); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function fromDispatcher($dispatcher){ |
81
|
|
|
$request = $dispatcher->get('request_stack')->getCurrentRequest(); |
82
|
|
|
$uri=$request->getPathInfo(); |
83
|
|
|
if(JString::startswith($uri, "/")){ |
84
|
|
|
$uri=\substr($uri, 1); |
85
|
|
|
} |
86
|
|
|
return \explode("/", $uri); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.