Passed
Push — master ( 5f63ee...30b096 )
by Jean-Christophe
02:33
created

JsUtils::addVariable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
		$router=$this->getInjected();
13
		if(isset($router)){
14
			try {
15
				$url=$router->generate($url);
16
			}catch (\Exception $e){
17
				return $url;
18
			}
19
		}
20
		return $url;
21
	}
22
	public function addViewElement($identifier,$content,&$view){
23
		if(\is_array($view)){
24
			if(\array_key_exists("q", $view)===false){
25
				$view["q"]=array();
26
			}
27
			$view["q"][$identifier]=$content;
28
		}elseif($view instanceof \Twig_Environment){
29
			$vars=$view->getGlobals();
30
			if(\array_key_exists("q", $vars)===false){
31
				$vars["q"]=array();
32
			}
33
			$vars["q"][$identifier]=$content;
34
			$view->addGlobal("q",$vars["q"]);
35
		}
36
	}
37
38
	public function createScriptVariable(&$view,$view_var, $output){
39
		$this->addVariable($view_var, $output, $view);
40
	}
41
42
	protected function addVariable($key,$value,&$view){
43
		if(\is_array($view)){
44
			$view[$key]=$value;
45
		}elseif($view instanceof \Twig_Environment){
46
			$view->addGlobal($key,$value);
47
		}
48
	}
49
50
	/**
51
	 * @param Symfony\Component\DependencyInjection\ContainerInterface $initialControllerInstance
52
	 * @param string $controllerName
53
	 * @param string $actionName
54
	 * @param array $params
55
	 * @see \Ajax\JsUtils::forward()
56
	 */
57
	public function forward($initialControllerInstance,$controllerName,$actionName,$params=array()){
58
		$path=$params;
59
		$request = $initialControllerInstance->get('request_stack')->getCurrentRequest();
60
		$path['_forwarded'] = $request->attributes;
61
		$path['_controller'] = $controllerName.":".$actionName;
62
		$subRequest = $request->duplicate([], null, $path);
63
		$response= $initialControllerInstance->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
64
		return $response->getContent();
65
	}
66
67
	public function renderContent($initialControllerInstance,$viewName, $params=NULL) {
68
		if ($initialControllerInstance->has('templating')) {
69
			return $initialControllerInstance->get('templating')->render($viewName, $params);
70
		}
71
72
		if (!$initialControllerInstance->has('twig')) {
73
			throw new \LogicException('You can not use the "renderView" method if the Templating Component or the Twig Bundle are not available.');
74
		}
75
76
		return $initialControllerInstance->get('twig')->render($viewName, $params);
77
	}
78
79
	public function fromDispatcher($dispatcher){
80
		$request = $dispatcher->get('request_stack')->getCurrentRequest();
81
		$uri=$request->getPathInfo();
82
		if(JString::startswith($uri, "/")){
83
			$uri=\substr($uri, 1);
84
		}
85
		return \explode("/", $uri);
86
	}
87
}
88