Subversion Repository Public Repository

Nextrek

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php namespace Illuminate\Routing\Generators;

use Illuminate\Filesystem\Filesystem;

class ControllerGenerator {

	/**
	 * The filesystem instance.
	 *
	 * @var \Illuminate\Filesystem\Filesystem
	 */
	protected $files;

	/**
	 * The default resource controller methods.
	 *
	 * @var array
	 */
	protected $defaults = array(
		'index',
		'create',
		'store',
		'show',
		'edit',
		'update',
		'destroy'
	);

	/**
	 * Create a new controller generator instance.
	 *
	 * @param  \Illuminate\Filesystem\Filesystem  $files
	 * @return void
	 */
	public function __construct(Filesystem $files)
	{
		$this->files = $files;
	}

	/**
	 * Create a new resourceful controller file.
	 *
	 * @param  string  $controller
	 * @param  string  $path
	 * @param  array   $options
	 * @return void
	 */
	public function make($controller, $path, array $options = array())
	{
		$stub = $this->addMethods($this->getController($controller), $options);

		$this->writeFile($stub, $controller, $path);

		return false;
	}

	/**
	 * Write the completed stub to disk.
	 *
	 * @param  string  $stub
	 * @param  string  $controller
	 * @param  string  $path
	 * @return void
	 */
	protected function writeFile($stub, $controller, $path)
	{
		if (str_contains($controller, '\\'))
		{
			$this->makeDirectory($controller, $path);
		}

		$controller = str_replace('\\', DIRECTORY_SEPARATOR, $controller);

		if ( ! $this->files->exists($fullPath = $path."/{$controller}.php"))
		{
			return $this->files->put($fullPath, $stub);
		}
	}

	/**
	 * Create the directory for the controller.
	 *
	 * @param  string  $controller
	 * @param  string  $path
	 * @return void
	 */
	protected function makeDirectory($controller, $path)
	{
		$directory = $this->getDirectory($controller);

		if ( ! $this->files->isDirectory($full = $path.'/'.$directory))
		{
			$this->files->makeDirectory($full, 0777, true);
		}
	}

	/**
	 * Get the directory the controller should live in.
	 *
	 * @param  string  $controller
	 * @return string
	 */
	protected function getDirectory($controller)
	{
		return implode('/', array_slice(explode('\\', $controller), 0, -1));
	}

	/**
	 * Get the controller class stub.
	 *
	 * @param  string  $controller
	 * @return string
	 */
	protected function getController($controller)
	{
		$stub = $this->files->get(__DIR__.'/stubs/controller.stub');

		// We will explode out the controller name on the namespace delimiter so we
		// are able to replace a namespace in this stub file. If no namespace is
		// provided we'll just clear out the namespace place-holder locations.
		$segments = explode('\\', $controller);

		$stub = $this->replaceNamespace($segments, $stub);

		return str_replace('{{class}}', last($segments), $stub);
	}

	/**
	 * Replace the namespace on the controller.
	 *
	 * @param  array   $segments
	 * @param  string  $stub
	 * @return string
	 */
	protected function replaceNamespace(array $segments, $stub)
	{
		if (count($segments) > 1)
		{
			$namespace = implode('\\', array_slice($segments, 0, -1));

			return str_replace('{{namespace}}', ' namespace '.$namespace.';', $stub);
		}

		return str_replace('{{namespace}}', '', $stub);
	}

	/**
	 * Add the method stubs to the controller.
	 *
	 * @param  string  $stub
	 * @param  array   $options
	 * @return string
	 */
	protected function addMethods($stub, array $options)
	{
		// Once we have the applicable methods, we can just spin through those methods
		// and add each one to our array of method stubs. Then we will implode them
		// them all with end-of-line characters and return the final joined list.
		$stubs = $this->getMethodStubs($options);

		$methods = implode(PHP_EOL.PHP_EOL, $stubs);

		return str_replace('{{methods}}', $methods, $stub);
	}

	/**
	 * Get all of the method stubs for the given options.
	 *
	 * @param  array  $options
	 * @return array
	 */
	protected function getMethodStubs($options)
	{
		$stubs = array();

		// Each stub is conveniently kept in its own file so we can just grab the ones
		// we need from disk to build the controller file. Once we have them all in
		// an array we will return this list of methods so they can be joined up.
		foreach ($this->getMethods($options) as $method)
		{
			$stubs[] = $this->files->get(__DIR__."/stubs/{$method}.stub");
		}

		return $stubs;
	}

	/**
	 * Get the applicable methods based on the options.
	 *
	 * @param  array  $options
	 * @return array
	 */
	protected function getMethods($options)
	{
		if (isset($options['only']) && count($options['only']) > 0)
		{
			return $options['only'];
		}
		elseif (isset($options['except']) && count($options['except']) > 0)
		{
			return array_diff($this->defaults, $options['except']);
		}

		return $this->defaults;
	}

}

Commits for Nextrek/Aiba_backup/vendor/laravel/framework/src/Illuminate/Routing/Generators/ControllerGenerator.php

Diff revisions: vs.
Revision Author Commited Message
1464 MOliva picture MOliva Tue 13 Oct, 2020 11:16:56 +0000