Skip to content

Commit b5ecb62

Browse files
committed
added dynamic model creation for user and content
added dynamic model creation for user and content for full extensibility on this 2 built-in components
1 parent f3f4af4 commit b5ecb62

File tree

4 files changed

+107
-12
lines changed

4 files changed

+107
-12
lines changed

src/Darryldecode/Backend/Components/ContentBuilder/Commands/QueryContentCommand.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Darryldecode\Backend\Components\ContentBuilder\Models\Content;
88
use Darryldecode\Backend\Components\ContentBuilder\Models\ContentType;
99
use Illuminate\Contracts\Bus\SelfHandling;
10+
use Illuminate\Contracts\Config\Repository;
1011
use Illuminate\Contracts\Events\Dispatcher;
1112

1213
class QueryContentCommand extends Command implements SelfHandling {
@@ -26,15 +27,20 @@ class QueryContentCommand extends Command implements SelfHandling {
2627
* @var null
2728
*/
2829
private $queryHook;
30+
/**
31+
* @var array
32+
*/
33+
private $with;
2934

3035
/**
3136
* @param null $id
3237
* @param null $slug
3338
* @param null $title
39+
* @param array $with
3440
* @param bool $disablePermissionChecking
3541
* @param null|callable $queryHook
3642
*/
37-
public function __construct($id = null, $slug = null, $title = null, $disablePermissionChecking = false, $queryHook = null)
43+
public function __construct($id = null, $slug = null, $title = null, $with = array(), $disablePermissionChecking = false, $queryHook = null)
3844
{
3945
parent::__construct();
4046
$this->id = $id;
@@ -43,21 +49,23 @@ public function __construct($id = null, $slug = null, $title = null, $disablePer
4349
$this->args = func_get_args();
4450
$this->disablePermissionChecking = $disablePermissionChecking;
4551
$this->queryHook = $queryHook;
52+
$this->with = $with;
4653
}
4754

4855
/**
4956
* @param Dispatcher $dispatcher
5057
* @param ContentType $contentType
5158
* @param Content $content
59+
* @param Repository $config
5260
* @return CommandResult
5361
*/
54-
public function handle(Dispatcher $dispatcher, ContentType $contentType, Content $content)
62+
public function handle(Dispatcher $dispatcher, ContentType $contentType, Content $content, Repository $config)
5563
{
5664
// fire before query
5765
$dispatcher->fire('content.beforeQuery', array($this->args));
5866

5967
// query
60-
$results = $this->query($contentType, $content);
68+
$results = $this->query($contentType, $content, $config);
6169

6270
// fire after query
6371
$dispatcher->fire('content.afterQuery', array($results));
@@ -69,18 +77,22 @@ public function handle(Dispatcher $dispatcher, ContentType $contentType, Content
6977
/**
7078
* @param $contentType \Darryldecode\Backend\Components\ContentBuilder\Models\ContentType
7179
* @param $content \Darryldecode\Backend\Components\ContentBuilder\Models\Content
80+
* @param $config \Illuminate\Contracts\Config\Repository
7281
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
7382
*/
74-
private function query($contentType, $content)
83+
private function query($contentType, $content, $config)
7584
{
76-
$q = $content->with(array(
85+
// prepare content model used
86+
$content = $this->createContentModel($content, $config);
87+
88+
$q = $content->with(array_merge(array(
7789
'terms',
7890
'author',
7991
'metaData',
8092
'type.formGroups',
8193
'revisions',
8294
'type'
83-
));
95+
),$this->with));
8496

8597
if( !is_null($this->queryHook) && (is_callable($this->queryHook)) )
8698
{
@@ -99,4 +111,22 @@ private function query($contentType, $content)
99111
return $q->ofSlug($this->slug)->ofTitle($this->title)->first();
100112
}
101113
}
114+
115+
/**
116+
* @param $content \Darryldecode\Backend\Components\ContentBuilder\Models\Content
117+
* @param $config \Illuminate\Contracts\Config\Repository
118+
* @return mixed
119+
*/
120+
protected function createContentModel($content, $config)
121+
{
122+
$contentModelUsed = $config->get('backend.backend.content_model');
123+
$contentModelUsed = new $contentModelUsed();
124+
125+
if( $contentModelUsed instanceof Content )
126+
{
127+
return $contentModelUsed;
128+
}
129+
130+
return $content;
131+
}
102132
}

src/Darryldecode/Backend/Components/ContentBuilder/Commands/QueryContentsCommand.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Darryldecode\Backend\Components\ContentBuilder\Models\ContentType;
1515
use Illuminate\Contracts\Bus\SelfHandling;
1616
use Illuminate\Contracts\Events\Dispatcher;
17+
use Illuminate\Contracts\Config\Repository;
1718

1819
class QueryContentsCommand extends Command implements SelfHandling {
1920
/**
@@ -64,6 +65,10 @@ class QueryContentsCommand extends Command implements SelfHandling {
6465
* @var null
6566
*/
6667
private $queryHook;
68+
/**
69+
* @var array
70+
*/
71+
private $with;
6772

6873
/**
6974
* @param null $type
@@ -75,6 +80,7 @@ class QueryContentsCommand extends Command implements SelfHandling {
7580
* @param int $perPage
7681
* @param string $sortBy
7782
* @param string $sortOrder
83+
* @param array $with
7884
* @param bool $disablePermissionChecking
7985
* @param null $startDate
8086
* @param null $endDate
@@ -89,6 +95,7 @@ public function __construct($type = null,
8995
$perPage = 8,
9096
$sortBy = 'created_at',
9197
$sortOrder = 'DESC',
98+
$with = array(),
9299
$disablePermissionChecking = false,
93100
$startDate = null,
94101
$endDate = null,
@@ -109,21 +116,23 @@ public function __construct($type = null,
109116
$this->startDate = $startDate;
110117
$this->endDate = $endDate;
111118
$this->queryHook = $queryHook;
119+
$this->with = $with;
112120
}
113121

114122
/**
115123
* @param Dispatcher $dispatcher
116124
* @param ContentType $contentType
117125
* @param Content $content
126+
* @param Repository $config
118127
* @return CommandResult
119128
*/
120-
public function handle(Dispatcher $dispatcher, ContentType $contentType, Content $content)
129+
public function handle(Dispatcher $dispatcher, ContentType $contentType, Content $content, Repository $config)
121130
{
122131
// fire before query
123132
$dispatcher->fire('contents.beforeQuery', array($this->args));
124133

125134
// query
126-
$results = $this->query($contentType, $content);
135+
$results = $this->query($contentType, $content, $config);
127136

128137
// fire after query
129138
$dispatcher->fire('contents.afterQuery', array($results));
@@ -137,18 +146,22 @@ public function handle(Dispatcher $dispatcher, ContentType $contentType, Content
137146
*
138147
* @param ContentType $contentType
139148
* @param Content $content
149+
* @param $config
140150
* @return mixed
141151
*/
142-
protected function query($contentType, $content)
152+
protected function query($contentType, $content, $config)
143153
{
144-
$q = $content->with(array(
154+
// prepare content model used
155+
$content = $this->createContentModel($content, $config);
156+
157+
$q = $content->with(array_merge(array(
145158
'terms',
146159
'author',
147160
'metaData',
148161
'type.formGroups',
149162
'revisions',
150163
'type'
151-
));
164+
),$this->with));
152165

153166
// check if there is status provided
154167
if( ($this->status) && ($this->status != 'any') )
@@ -285,4 +298,22 @@ private function extractTerms($terms)
285298

286299
return $t;
287300
}
301+
302+
/**
303+
* @param $content \Darryldecode\Backend\Components\ContentBuilder\Models\Content
304+
* @param $config \Illuminate\Contracts\Config\Repository
305+
* @return mixed
306+
*/
307+
protected function createContentModel($content, $config)
308+
{
309+
$contentModelUsed = $config->get('backend.backend.content_model');
310+
$contentModelUsed = new $contentModelUsed();
311+
312+
if( $contentModelUsed instanceof Content )
313+
{
314+
return $contentModelUsed;
315+
}
316+
317+
return $content;
318+
}
288319
}

src/Darryldecode/Backend/Components/User/Commands/QueryUsersCommand.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Darryldecode\Backend\Base\Commands\CommandResult;
1313
use Darryldecode\Backend\Components\User\Models\User;
1414
use Darryldecode\Backend\Components\User\Models\Group;
15+
use Illuminate\Config\Repository;
1516
use Illuminate\Contracts\Bus\SelfHandling;
1617
use Illuminate\Contracts\Events\Dispatcher;
1718

@@ -110,9 +111,10 @@ public function __construct($id = null,
110111
* @param User $user
111112
* @param Group $group
112113
* @param Dispatcher $dispatcher
114+
* @param Repository $config
113115
* @return CommandResult
114116
*/
115-
public function handle(User $user, Group $group, Dispatcher $dispatcher)
117+
public function handle(User $user, Group $group, Dispatcher $dispatcher, Repository $config)
116118
{
117119
// check user permission
118120
if( ! $this->disablePermissionChecking )
@@ -123,6 +125,9 @@ public function handle(User $user, Group $group, Dispatcher $dispatcher)
123125
}
124126
}
125127

128+
// prepare the user model
129+
$user = $this->createUserModel($user, $config);
130+
126131
// fire before query event
127132
$dispatcher->fire('user.beforeQuery', array($this->args));
128133

@@ -170,4 +175,22 @@ public function handle(User $user, Group $group, Dispatcher $dispatcher)
170175
// return result
171176
return new CommandResult(true, "Query user(s) successful.", $results, 200);
172177
}
178+
179+
/**
180+
* @param $user \Darryldecode\Backend\Components\User\Models\User
181+
* @param $config \Illuminate\Config\Repository
182+
* @return mixed
183+
*/
184+
protected function createUserModel($user, $config)
185+
{
186+
$userModelUsed = $config->get('backend.backend.user_model');
187+
$userModelUsed = new $userModelUsed();
188+
189+
if( $userModelUsed instanceof User )
190+
{
191+
return $userModelUsed;
192+
}
193+
194+
return $user;
195+
}
173196
}

src/Darryldecode/Backend/Config/backend.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,15 @@
3232
'medium' => array(300,200),
3333
'large' => array(600,450),
3434
),
35+
36+
/*
37+
* built-in component models being used
38+
*
39+
* NOTE:
40+
*
41+
* The purpose of this is for extensibility, if you want to extend relationships for user/content model
42+
* you can change this to your own and make sure to extend this models
43+
*/
44+
'user_model' => 'Darryldecode\Backend\Components\User\Models\User',
45+
'content_model' => 'Darryldecode\Backend\Components\ContentBuilder\Models\Content',
3546
];

0 commit comments

Comments
 (0)