11<?php
22
3- /**
3+ /*
44 * This file is part of the SgDatatablesBundle package.
55 *
66 * (c) stwe <https://github.com/stwe/DatatablesBundle>
1111
1212namespace Sg \DatatablesBundle \Command ;
1313
14+ use Doctrine \ORM \Mapping \ClassMetadataInfo ;
15+ use Exception ;
16+ use Sensio \Bundle \GeneratorBundle \Command \GenerateDoctrineCommand ;
17+ use Sensio \Bundle \GeneratorBundle \Command \Validators ;
1418use Sg \DatatablesBundle \Generator \DatatableGenerator ;
15-
16- use Symfony \Component \Console \Input \InputInterface ;
17- use Symfony \Component \Console \Output \OutputInterface ;
1819use Symfony \Component \Console \Input \InputArgument ;
20+ use Symfony \Component \Console \Input \InputInterface ;
1921use Symfony \Component \Console \Input \InputOption ;
22+ use Symfony \Component \Console \Output \OutputInterface ;
2023use Symfony \Component \HttpKernel \Bundle \BundleInterface ;
21- use Sensio \Bundle \GeneratorBundle \Command \GenerateDoctrineCommand ;
22- use Sensio \Bundle \GeneratorBundle \Command \Validators ;
23- use Doctrine \ORM \Mapping \ClassMetadataInfo ;
24- use Exception ;
2524
26- /**
27- * Class CreateDatatableCommand
28- *
29- * @package Sg\DatatablesBundle\Command
30- */
3125class CreateDatatableCommand extends GenerateDoctrineCommand
3226{
3327 //-------------------------------------------------
@@ -41,11 +35,12 @@ protected function configure()
4135 {
4236 $ this
4337 ->setName ('sg:datatable:generate ' )
44- ->setAliases (array ( 'sg:datatables:generate ' , 'sg:generate:datatable ' , 'sg:generate:datatables ' ) )
38+ ->setAliases ([ 'sg:datatables:generate ' , 'sg:generate:datatable ' , 'sg:generate:datatables ' ] )
4539 ->setDescription ('Generates a new Datatable based on a Doctrine entity. ' )
4640 ->addArgument ('entity ' , InputArgument::REQUIRED , 'The entity class name (shortcut notation). ' )
4741 ->addOption ('fields ' , 'f ' , InputOption::VALUE_OPTIONAL , 'The fields to create columns in the new Datatable. ' )
48- ->addOption ('overwrite ' , 'o ' , InputOption::VALUE_NONE , 'Allow to overwrite an existing Datatable. ' );
42+ ->addOption ('overwrite ' , 'o ' , InputOption::VALUE_NONE , 'Allow to overwrite an existing Datatable. ' )
43+ ;
4944 }
5045
5146 /**
@@ -58,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5853 list ($ bundle , $ entity ) = $ this ->parseShortcutNotation ($ entity );
5954
6055 // get entity's metadata
61- $ entityClass = $ this ->getContainer ()->get ('doctrine ' )->getAliasNamespace ($ bundle )."\\" .$ entity ;
56+ $ entityClass = $ this ->getContainer ()->get ('doctrine ' )->getAliasNamespace ($ bundle ).'\\' .$ entity ;
6257 $ metadata = $ this ->getEntityMetadata ($ entityClass );
6358
6459 // get fields option
@@ -100,7 +95,7 @@ protected function createGenerator()
10095 */
10196 protected function getSkeletonDirs (BundleInterface $ bundle = null )
10297 {
103- $ skeletonDirs = array () ;
98+ $ skeletonDirs = [] ;
10499 $ skeletonDirs [] = $ this ->getContainer ()->get ('kernel ' )->locateResource ('@SgDatatablesBundle/Resources/views/skeleton ' );
105100
106101 return $ skeletonDirs ;
@@ -119,12 +114,12 @@ protected function getSkeletonDirs(BundleInterface $bundle = null)
119114 */
120115 private static function parseFields ($ input )
121116 {
122- $ fields = array () ;
117+ $ fields = [] ;
123118
124119 foreach (explode (' ' , $ input ) as $ value ) {
125120 $ elements = explode (': ' , $ value );
126121
127- $ row = array () ;
122+ $ row = [] ;
128123 $ row ['property ' ] = $ elements [0 ];
129124 $ row ['column_type ' ] = 'Column::class ' ;
130125 $ row ['data ' ] = null ;
@@ -134,9 +129,11 @@ private static function parseFields($input)
134129 switch ($ elements [1 ]) {
135130 case 'datetime ' :
136131 $ row ['column_type ' ] = 'DateTimeColumn::class ' ;
132+
137133 break ;
138134 case 'boolean ' :
139135 $ row ['column_type ' ] = 'BooleanColumn::class ' ;
136+
140137 break ;
141138 }
142139 }
@@ -150,14 +147,11 @@ private static function parseFields($input)
150147 /**
151148 * Get Id from metadata.
152149 *
153- * @param array $metadata
154- *
155- * @return mixed
156150 * @throws Exception
157151 */
158152 private function getIdentifierFromMetadata (array $ metadata )
159153 {
160- if (count ($ metadata [0 ]->identifier ) > 1 ) {
154+ if (\ count ($ metadata [0 ]->identifier ) > 1 ) {
161155 throw new Exception ('CreateDatatableCommand::getIdentifierFromMetadata(): The Datatable generator does not support entities with multiple primary keys. ' );
162156 }
163157
@@ -168,24 +162,24 @@ private function getIdentifierFromMetadata(array $metadata)
168162 * Returns an array of fields. Fields can be both column fields and
169163 * association fields.
170164 *
171- * @param ClassMetadataInfo $metadata
172- *
173165 * @return array $fields
174166 */
175167 private function getFieldsFromMetadata (ClassMetadataInfo $ metadata )
176168 {
177- $ fields = array () ;
169+ $ fields = [] ;
178170
179171 foreach ($ metadata ->fieldMappings as $ field ) {
180- $ row = array () ;
172+ $ row = [] ;
181173 $ row ['property ' ] = $ field ['fieldName ' ];
182174
183175 switch ($ field ['type ' ]) {
184176 case 'datetime ' :
185177 $ row ['column_type ' ] = 'DateTimeColumn::class ' ;
178+
186179 break ;
187180 case 'boolean ' :
188181 $ row ['column_type ' ] = 'BooleanColumn::class ' ;
182+
189183 break ;
190184 default :
191185 $ row ['column_type ' ] = 'Column::class ' ;
@@ -201,11 +195,11 @@ private function getFieldsFromMetadata(ClassMetadataInfo $metadata)
201195 $ targetMetadata = $ this ->getEntityMetadata ($ targetClass );
202196
203197 foreach ($ targetMetadata [0 ]->fieldMappings as $ field ) {
204- $ row = array () ;
198+ $ row = [] ;
205199 $ row ['property ' ] = $ relation ['fieldName ' ].'. ' .$ field ['fieldName ' ];
206200 $ row ['column_type ' ] = 'Column::class ' ;
207201 $ row ['title ' ] = ucwords (str_replace ('. ' , ' ' , $ row ['property ' ]));
208- if ($ relation ['type ' ] === ClassMetadataInfo::ONE_TO_MANY || $ relation ['type ' ] === ClassMetadataInfo:: MANY_TO_MANY ) {
202+ if (ClassMetadataInfo:: ONE_TO_MANY === $ relation ['type ' ] || ClassMetadataInfo::MANY_TO_MANY === $ relation ['type ' ]) {
209203 $ row ['data ' ] = $ relation ['fieldName ' ].'[, ]. ' .$ field ['fieldName ' ];
210204 } else {
211205 $ row ['data ' ] = null ;
0 commit comments