Skip to content

Commit f33d5c4

Browse files
committed
Automatically set the domain or prefix on version groups if it's not provided.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 3ca48b1 commit f33d5c4

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

src/Provider/ApiServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ protected function registerRouter()
174174
$app['api.router.adapter'],
175175
new Http\Parser\Accept($config['vendor'], $config['version'], $config['defaultFormat']),
176176
$app['api.exception'],
177-
$app
177+
$app,
178+
$config['domain'],
179+
$config['prefix']
178180
);
179181
});
180182

src/Routing/Router.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,40 @@ class Router
9191
*/
9292
protected $routesDispatched = 0;
9393

94+
/**
95+
* The API domain.
96+
*
97+
* @var string
98+
*/
99+
protected $domain;
100+
101+
/**
102+
* The API prefix.
103+
*
104+
* @var string
105+
*/
106+
protected $prefix;
107+
94108
/**
95109
* Create a new router instance.
96110
*
97111
* @param \Dingo\Api\Routing\Adapter\Adapter $adapter
98112
* @param \Dingo\Api\Http\Parser\AcceptParser $accept
99113
* @param \Dingo\Api\Exception\Handler $exception
100114
* @param \Illuminate\Container\Container $container
115+
* @param string $domain
116+
* @param string $prefix
101117
*
102118
* @return void
103119
*/
104-
public function __construct(Adapter $adapter, AcceptParser $accept, Handler $exception, Container $container)
120+
public function __construct(Adapter $adapter, AcceptParser $accept, Handler $exception, Container $container, $domain, $prefix)
105121
{
106122
$this->adapter = $adapter;
107123
$this->accept = $accept;
108124
$this->exception = $exception;
109125
$this->container = $container;
126+
$this->domain = $domain;
127+
$this->prefix = $prefix;
110128
}
111129

112130
/**
@@ -158,6 +176,14 @@ public function group(array $attributes, $callback)
158176
$attributes['version'] = (array) $attributes['version'];
159177
}
160178

179+
if ((! isset($attributes['prefix']) || empty($attributes['prefix'])) && isset($this->prefix)) {
180+
$attributes['prefix'] = $this->prefix;
181+
}
182+
183+
if ((! isset($attributes['domain']) || empty($attributes['domain'])) && isset($this->domain)) {
184+
$attributes['domain'] = $this->domain;
185+
}
186+
161187
$this->groupStack[] = $attributes;
162188

163189
call_user_func($callback, $this);

tests/DispatcherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function setUp()
2828

2929
$this->adapter = new RoutingAdapterStub;
3030
$this->exception = m::mock('Dingo\Api\Exception\Handler');
31-
$this->router = new Router($this->adapter, new Http\Parser\Accept('api', 'v1', 'json'), $this->exception, $this->container);
31+
$this->router = new Router($this->adapter, new Http\Parser\Accept('api', 'v1', 'json'), $this->exception, $this->container, null, null);
3232

3333
$this->auth = new Auth($this->router, $this->container, []);
3434
$this->dispatcher = new Dispatcher($this->container, new Filesystem, $this->router, $this->auth);

tests/Routing/Adapter/BaseAdapterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function setUp()
2424

2525
$this->adapter = new $class(new IlluminateRouter(new Dispatcher($this->container), $this->container));
2626
$this->exception = m::mock('Dingo\Api\Exception\Handler');
27-
$this->router = new Router($this->adapter, new Http\Parser\Accept('api', 'v1', 'json'), $this->exception, $this->container);
27+
$this->router = new Router($this->adapter, new Http\Parser\Accept('api', 'v1', 'json'), $this->exception, $this->container, null, null);
2828

2929
Http\Response::setFormatters(['json' => new Http\Response\Format\Json]);
3030
}

0 commit comments

Comments
 (0)