Completed
Push — master ( a78505...5e2c18 )
by Jean-Christophe
02:39
created

JsUtils::fromDispatcher()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
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){
0 ignored issues
show
Bug introduced by
The class Twig_Environment does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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){
0 ignored issues
show
Bug introduced by
The class Twig_Environment does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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