GeneratorController::generateModelAction()   C
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 7
Bugs 0 Features 4
Metric Value
c 7
b 0
f 4
dl 0
loc 50
ccs 0
cts 36
cp 0
rs 6.7272
cc 7
eloc 34
nc 3
nop 0
crap 56
1
<?php
2
namespace Tpg\ExtjsBundle\Controller;
3
4
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
5
use Symfony\Bundle\FrameworkBundle\Tests\Functional\AppKernel;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Finder\SplFileInfo;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\StreamedResponse;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Bundle\Bundle;
12
use Tpg\ExtjsBundle\Service\GeneratorService;
13
14
class GeneratorController extends Controller {
15
    public function generateModelAction() {
16
        $models = $this->getRequest()->get("model");
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
17
        /** @var $generator GeneratorService */
18
        $generator = $this->get("tpg_extjs.generator");
19
        /** @var $kernel AppKernel */
20
        $kernel = $this->get('kernel');
21
        if ($models === null) {
22
            $list = $this->container->getParameter("tpg_extjs.entities");
23
            return new StreamedResponse(function () use($list, $generator, $kernel) {
24
                foreach ($list as $configEntityLine) {
25
                    list($bundleName, $path) = explode("/", substr($configEntityLine, 1), 2);
26
                    /** @var Bundle $bundle */
27
                    $bundle = $kernel->getBundle($bundleName, true);
28
29
                    /** Entity end with backslash, it is a directory */
30
                    $loadAllEntitiesFromDir = ($configEntityLine[strlen($configEntityLine)-1] == "/");
31
32
                    if ( $loadAllEntitiesFromDir ) {
33
                        $bundleRef = new \ReflectionClass($bundle);
34
                        $dir = new Finder();
35
                        $dir->files()->depth('== 0')->in(dirname($bundleRef->getFileName()).'/'.$path)->name('/.*\.php$/');
36
                        foreach($dir as $file) {
37
                            /** @var SplFileInfo $file*/
38
                            $entityClassname = $bundleRef->getNamespaceName() . "\\" . str_replace("/", "\\", $path) . substr($file->getFilename(), 0, -4);
39
                            echo $generator->generateMarkupForEntity($entityClassname);
40
                        }
41
                    } else  {
42
                        $entityClassname = $bundle->getNamespace() . "\\" . str_replace("/", "\\", $path);
43
                        echo $generator->generateMarkupForEntity($entityClassname);
44
                    }
45
                }
46
                flush();
47
            }, 200, array(
48
                'Content-Type'=>'application/javascript'
49
            ));
50
        } else {
51
            if (!is_array($models)) {
52
                $models = array($models);
53
            }
54
            return new StreamedResponse(function () use($models, $generator) {
55
                foreach ($models as $model) {
56
                    $model = str_replace(".", "\\", $model);
57
                    echo $generator->generateMarkupForEntity($model);
58
                }
59
                flush();
60
            }, 200, array(
61
                'Content-Type'=>'application/javascript'
62
            ));
63
        }
64
    }
65
66
    public function generateRemoteApiAction() {
67
        /** @var $generator GeneratorService */
68
        $generator = $this->get("tpg_extjs.generator");
69
        $apis = $generator->generateRemotingApi();
70
        $response = new Response();
71
        $response->headers->set('Content-Type', 'application/javascript');
72
        return $this->render(
73
            "TpgExtjsBundle:ExtjsMarkup:remoteapi.js.twig",
74
            array(
75
                "apis"=>$apis,
76
                "route"=>'extjs_remoting',
77
            ),
78
            $response
79
        );
80
    }
81
82
    public function remotingAction($bundle) {
83
        $bundleName = str_replace('.', "", $bundle);
84
        $request = json_decode($this->getRequest()->getContent(), true);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
85
        if (!isset($request['data'])) {
86
            $data = array();
87
        } else {
88
            $data = $request['data'];
89
        }
90
        $controller = str_replace('.', "\\", $bundle)."\\Controller\\".$request['action'].'Controller';
91
        $ref = (new \ReflectionClass($controller));
92
        $actionMethod = $ref->getMethod($request['method'].'Action');
93
        $actionparams = $actionMethod->getParameters();
94
        if (count($actionparams) == 1
95
            && is_object($actionparams[0]->getClass())
96
            && (
97
                $actionparams[0]->getClass()->name == 'FOS\RestBundle\Request\ParamFetcherInterface'
98
                || $actionparams[0]->getClass()->name == 'Symfony\Component\HttpFoundation\Request'
99
            )) {
100
            // if the action expects only a ParamFetcher or Request param just give it the first member of $data
101
            $requestData = $data[0];
102
        } else {
103
            $requestData = array();
104
            $i = 0;
105
            foreach($actionparams as $parameter) {
106
                $requestData[$parameter->getName()] = $data[$i++];
107
            }
108
        }
109
110
        /* TODO:
111
            figure out which request params need to be "path params" and which are "query params".
112
            We would need to check the route for the action to figure this out.
113
            Meanwhile we just give the complete request param array as both pathParams and queryParams
114
            so the action can fetch the params from where it expects them to be.
115
        */
116
        $pathParams = $requestData;
117
        $queryParams = $requestData;
118
119
        /** @var JsonResponse $response */
120
        $response = $this->forward($bundleName.':'.$request['action'].':'.$request['method'], $pathParams, $queryParams);
121
        return new JsonResponse(array(
122
            'type'=>$request['type'],
123
            'tid'=>$request['tid'],
124
            'action'=>$request['action'],
125
            'method'=>$request['method'],
126
            'result'=>json_decode($response->getContent())
127
        ));
128
    }
129
}