This repository was archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouteGenerator.php
92 lines (78 loc) · 2.74 KB
/
RouteGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php namespace Impleri\Resource;
use BadFunctionCallException;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\View;
/**
* Controller Generator
*
* Small class to generate resource routes.
*/
class RouteGenerator extends Generator
{
/**
* Default view to use for generating routes.
* @var string
*/
protected $view = 'resource::routes';
/**
* Fill Options
*
* Boilerplate method to inflate generator options using defaults.
* @param array $options Variables to pass to the view
* @return array Options
* @throws BadFunctionCallException If missing element/collection from $options
*/
protected function fillOptions($options)
{
// Need at least a name to use
if (!isset($options['collection']) && !isset($options['element'])) {
throw new BadFunctionCallException('A collection or element name is required.');
}
// Set the default base path if none is
if (!isset($options['basePath'])) {
$options['basePath'] = $this->path;
}
$options['basePath'] = $this->trailingSeparator($options['basePath']);
if (!isset($options['collection'])) {
// Pluralize the element
$options['collection'] = Str::plural($options['element']);
} elseif (!isset($options['element'])) {
// Singularize the collection
$options['element'] = Str::singular($options['collection']);
}
// Set the model from the element
if (!isset($options['model'])) {
$options['model'] = Str::studly($options['element']);
}
// Set the controller from the model
if (!isset($options['controller'])) {
$options['controller'] = $options['model'] . 'Controller';
}
// Assume an element if neither is specified
if (!isset($options['isCollection']) && !isset($options['isElement'])) {
$options['isElement'] = true;
}
// Assume PUT collection is false
if (!isset($options['allowPutAll'])) {
$options['allowPutAll'] = false;
}
// Assume DELETE collection is false
if (!isset($options['allowDeleteAll'])) {
$options['allowDeleteAll'] = false;
}
return $options;
}
/**
* Execute
*
* Generic method to generate resource class files.
* @param array $options Variables to pass to the view
* @return string Routes to add to routes.php
*/
public function execute($options)
{
$options = $this->fillOptions($options);
// Pass options to the view and return the rendered string
return View::make($this->view, $options)->render();
}
}