44
55use Illuminate \Contracts \Database \Eloquent \Builder as EloquentBuilder ;
66use Illuminate \Contracts \Database \Query \Builder as QueryBuilder ;
7+ use Illuminate \Support \Facades \Config ;
78use Illuminate \Support \Traits \Macroable ;
89use Yajra \DataTables \Exceptions \Exception ;
9- use Yajra \DataTables \Utilities \Config ;
10+ use Yajra \DataTables \Utilities \Config as DataTablesConfig ;
1011use Yajra \DataTables \Utilities \Request ;
1112
1213class DataTables
@@ -42,123 +43,40 @@ public static function of($source)
4243 */
4344 public static function make ($ source )
4445 {
45- $ args = func_get_args ();
46- $ engines = (array ) config ('datatables.engines ' );
47- $ builders = (array ) config ('datatables.builders ' );
48-
49- $ instance = self ::tryCreateFromBuilders ($ source , $ builders , $ engines , $ args );
50- if ($ instance !== null ) {
51- return $ instance ;
52- }
53-
54- $ instance = self ::tryCreateFromEngines ($ source , $ engines , $ args );
55- if ($ instance !== null ) {
56- return $ instance ;
57- }
58-
59- throw new Exception ('No available engine for ' .$ source ::class);
60- }
46+ $ engines = Config::array ('datatables.engines ' , []);
47+ $ builders = Config::array ('datatables.builders ' , []);
6148
62- /**
63- * Try to create a DataTable instance from builders configuration.
64- *
65- * @param object $source
66- */
67- private static function tryCreateFromBuilders ($ source , array $ builders , array $ engines , array $ args ): ?DataTableAbstract
68- {
49+ $ args = func_get_args ();
6950 foreach ($ builders as $ class => $ engine ) {
70- if (! self ::isValidBuilderClass ($ source , $ class )) {
71- continue ;
72- }
51+ if (is_string ($ class ) && $ source instanceof $ class ) {
52+ /** @var int|string $engineKey */
53+ $ engineKey = is_int ($ engine ) || is_string ($ engine ) ? $ engine : (string ) $ engine ;
54+ $ callback = [$ engines [$ engineKey ], 'create ' ];
7355
74- $ engineClass = self ::getEngineClass ($ engine , $ engines );
75- if ($ engineClass === null ) {
76- continue ;
77- }
56+ if (is_callable ($ callback )) {
57+ /** @var \Yajra\DataTables\DataTableAbstract $instance */
58+ $ instance = call_user_func_array ($ callback , $ args );
7859
79- $ instance = self ::createInstance ($ engineClass , $ args );
80- if ($ instance !== null ) {
81- return $ instance ;
60+ return $ instance ;
61+ }
8262 }
8363 }
8464
85- return null ;
86- }
87-
88- /**
89- * Try to create a DataTable instance from engines configuration.
90- *
91- * @param object $source
92- */
93- private static function tryCreateFromEngines ($ source , array $ engines , array $ args ): ?DataTableAbstract
94- {
9565 foreach ($ engines as $ engine ) {
96- if (! self :: canCreateInstance ( $ engine , $ args )) {
97- continue ;
98- }
66+ $ canCreate = [ $ engine , ' canCreate ' ];
67+ if ( is_callable ( $ canCreate ) && call_user_func_array ( $ canCreate , $ args )) {
68+ $ create = [ $ engine , ' create ' ];
9969
100- $ instance = self ::createInstance ($ engine , $ args );
101- if ($ instance !== null ) {
102- return $ instance ;
103- }
104- }
70+ if (is_callable ($ create )) {
71+ /** @var \Yajra\DataTables\DataTableAbstract $instance */
72+ $ instance = call_user_func_array ($ create , $ args );
10573
106- return null ;
107- }
108-
109- /**
110- * Check if the source is a valid instance of the builder class.
111- *
112- * @param object $source
113- * @param string $class
114- */
115- private static function isValidBuilderClass ($ source , $ class ): bool
116- {
117- return is_string ($ class ) && class_exists ($ class ) && $ source instanceof $ class ;
118- }
119-
120- /**
121- * Get the engine class from the engine name.
122- *
123- * @param mixed $engine
124- */
125- private static function getEngineClass ($ engine , array $ engines ): ?string
126- {
127- if (! is_string ($ engine ) || ! isset ($ engines [$ engine ])) {
128- return null ;
129- }
130-
131- return $ engines [$ engine ];
132- }
133-
134- /**
135- * Check if an engine can create an instance with the given arguments.
136- *
137- * @param string $engine
138- */
139- private static function canCreateInstance ($ engine , array $ args ): bool
140- {
141- $ canCreate = [$ engine , 'canCreate ' ];
142-
143- return is_callable ($ canCreate ) && call_user_func_array ($ canCreate , $ args );
144- }
145-
146- /**
147- * Create a DataTable instance from the engine class.
148- *
149- * @param string $engineClass
150- */
151- private static function createInstance ($ engineClass , array $ args ): ?DataTableAbstract
152- {
153- $ callback = [$ engineClass , 'create ' ];
154- if (! is_callable ($ callback )) {
155- return null ;
74+ return $ instance ;
75+ }
76+ }
15677 }
15778
158- /** @var \Yajra\DataTables\DataTableAbstract $instance */
159- $ instance = call_user_func_array ($ callback , $ args );
160-
161- return $ instance ;
79+ throw new Exception ('No available engine for ' .$ source ::class);
16280 }
16381
16482 /**
@@ -172,7 +90,7 @@ public function getRequest(): Request
17290 /**
17391 * Get config instance.
17492 */
175- public function getConfig (): Config
93+ public function getConfig (): DataTablesConfig
17694 {
17795 return app ('datatables.config ' );
17896 }
@@ -184,8 +102,7 @@ public function getConfig(): Config
184102 */
185103 public function query (QueryBuilder $ builder ): QueryDataTable
186104 {
187- /** @var string $dataTable */
188- $ dataTable = config ('datatables.engines.query ' );
105+ $ dataTable = Config::string ('datatables.engines.query ' );
189106
190107 $ this ->validateDataTable ($ dataTable , QueryDataTable::class);
191108
@@ -199,8 +116,7 @@ public function query(QueryBuilder $builder): QueryDataTable
199116 */
200117 public function eloquent (EloquentBuilder $ builder ): EloquentDataTable
201118 {
202- /** @var string $dataTable */
203- $ dataTable = config ('datatables.engines.eloquent ' );
119+ $ dataTable = Config::string ('datatables.engines.eloquent ' );
204120
205121 $ this ->validateDataTable ($ dataTable , EloquentDataTable::class);
206122
@@ -216,8 +132,7 @@ public function eloquent(EloquentBuilder $builder): EloquentDataTable
216132 */
217133 public function collection ($ collection ): CollectionDataTable
218134 {
219- /** @var string $dataTable */
220- $ dataTable = config ('datatables.engines.collection ' );
135+ $ dataTable = Config::string ('datatables.engines.collection ' );
221136
222137 $ this ->validateDataTable ($ dataTable , CollectionDataTable::class);
223138
0 commit comments