Skip to content
This repository was archived by the owner on Dec 14, 2022. It is now read-only.

Commit 56eb0d3

Browse files
committed
Completed specs and tests for current generators
1 parent 02a6354 commit 56eb0d3

8 files changed

+589
-447
lines changed

readme.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
Laravel REST API Library
1+
Laravel Resource Library
22
========================
33

4-
[![Build Status](https://travis-ci.org/impleri/laravel-resource.png?branch=master)](https://travis-ci.org/impleri/laravel-resource)
5-
[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/impleri/laravel-resource/badges/quality-score.png?s=a2f2da02db7ae7e8f2fe395d2e83d4659ccbbf76)](https://scrutinizer-ci.com/g/impleri/laravel-resource/)
6-
[![Code Coverage](https://scrutinizer-ci.com/g/impleri/laravel-resource/badges/coverage.png?s=2ccdf1fcdc1af0403df59f59a757fe052c504ac6)](https://scrutinizer-ci.com/g/impleri/laravel-resource/)
7-
[![Dependency Status](https://www.versioneye.com/user/projects/531a85fcec13755bfa000aeb/badge.png)](https://www.versioneye.com/user/projects/531a85fcec13755bfa000aeb)
4+
An Alternative RESTful resource implementation for Laravel 4. This package provides
5+
a more extensive REST API interface than the stock resource controller generator.
6+
Out of the box, the package provides a lot of boilerplate code for scaffolding routes, controllers, models, and views. Routes are RESTful and API-centric. It handles outputting to JSON, XML (via a view), HTML (via a view), CSV, and plain text.
87

98

10-
Alternative RESTful resource implementation for Laravel 4. This package provides
11-
a more extensive REST API interface than the stock resource controller generator.
12-
Out of the box, the package allows others to generate routes and controllers so
13-
that developers simply plug in a model and overload methods when needed.
9+
[![Latest Release](https://poser.pugx.org/impleri/laravel-resource/v/stable.png)](https://packagist.org/packages/impleri/laravel-resource) |
10+
[![Build Status](https://travis-ci.org/impleri/laravel-resource.png?branch=master)](https://travis-ci.org/impleri/laravel-resource) |
11+
[![Code Quality](https://scrutinizer-ci.com/g/impleri/laravel-resource/badges/quality-score.png?s=a2f2da02db7ae7e8f2fe395d2e83d4659ccbbf76)](https://scrutinizer-ci.com/g/impleri/laravel-resource/) |
12+
[![Code Coverage](https://scrutinizer-ci.com/g/impleri/laravel-resource/badges/coverage.png?s=2ccdf1fcdc1af0403df59f59a757fe052c504ac6)](https://scrutinizer-ci.com/g/impleri/laravel-resource/) |
13+
[![Dependency Status](https://www.versioneye.com/user/projects/531a85fcec13755bfa000aeb/badge.png)](https://www.versioneye.com/user/projects/531a85fcec13755bfa000aeb)
14+

src/Generator.php

+45-7
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public function execute($options)
5353
$contents = View::make($view, $data)->render();
5454

5555
// Remove the old backup
56-
if (!$this->save($data['file'], $contents)) {
56+
if ($this->save($data['file'], $contents)) {
57+
$count++;
58+
} else {
5759
// Some kind of warning here
5860
}
59-
60-
$count++;
6161
}
6262

6363
// Return count to the toolbox
@@ -89,9 +89,13 @@ protected function fillOptions($options)
8989
}
9090

9191
// Flesh out the base path
92+
$basePath = $this->trailingSeparator($options['basePath']);
93+
94+
// Add base namespace to base path
9295
$baseNamespace = $options['baseNamespace'];
93-
$basePath = $options['basePath'];
94-
$basePath .= (empty($baseNamespace)) ? '' : str_replace('\\', '/', $baseNamespace);
96+
$basePath .= (empty($baseNamespace)) ? '' : $this->convertNamespace($baseNamespace);
97+
$basePath = $this->trailingSeparator($basePath);
98+
9599
$options['basePath'] = $basePath;
96100

97101
return $options;
@@ -109,14 +113,19 @@ protected function fillOptions($options)
109113
protected function fillClass($name, $class, $options)
110114
{
111115
$namespace = $options['baseNamespace'];
112-
$saveTo = $options['basePath'];
116+
$saveTo = $this->trailingSeparator($options['basePath']);
113117

114118
// Add to the save path and namespace
115119
if (isset($class['subNamespace'])) {
120+
if (!empty($namespace)) {
121+
$namespace = $this->trailingSeparator($namespace, '\\');
122+
}
116123
$namespace .= $class['subNamespace'];
117-
$saveTo .= str_replace('\\', '/', $class['subNamespace']);
124+
$saveTo .= $this->convertNamespace($class['subNamespace']);
118125
}
119126

127+
$saveTo = $this->trailingSeparator($saveTo);
128+
120129
// Override the class name with the array value
121130
if (isset($class['name'])) {
122131
$name = $class['name'];
@@ -172,4 +181,33 @@ protected function save($path, $contents)
172181

173182
return $success;
174183
}
184+
185+
/**
186+
* Trailing Separator
187+
*
188+
* Ensures there is a trailing separator on a string
189+
* @param string $string String to test
190+
* @param string $end Separator to append
191+
* @return string Modified string
192+
*/
193+
protected function trailingSeparator($string, $end = '/')
194+
{
195+
if (substr($string, -strlen($end)) !== $end) {
196+
$string .= $end;
197+
}
198+
199+
return $string;
200+
}
201+
202+
/**
203+
* Convert Namespace
204+
*
205+
* Convert backslash from namespace to forward slash for file path.
206+
* @param string $string Namespace to convert.
207+
* @return string Namespace converted to a path.
208+
*/
209+
protected function convertNamespace($string)
210+
{
211+
return str_replace('\\', '/', $string);
212+
}
175213
}

src/RouteGenerator.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ class RouteGenerator extends Generator
2222
*
2323
* Boilerplate method to inflate generator options using defaults.
2424
* @param array $options Variables to pass to the view
25+
* @return array Options
2526
* @throws BadFunctionCallException If missing element/collection from $options
2627
*/
27-
protected function fillOptions(&$options)
28+
protected function fillOptions($options)
2829
{
2930
// Need at least a name to use
3031
if (!isset($options['collection']) && !isset($options['element'])) {
@@ -36,6 +37,8 @@ protected function fillOptions(&$options)
3637
$options['basePath'] = $this->path;
3738
}
3839

40+
$options['basePath'] = $this->trailingSeparator($options['basePath']);
41+
3942
if (!isset($options['collection'])) {
4043
// Pluralize the element
4144
$options['collection'] = Str::plural($options['element']);
@@ -68,6 +71,8 @@ protected function fillOptions(&$options)
6871
if (!isset($options['allowDeleteAll'])) {
6972
$options['allowDeleteAll'] = false;
7073
}
74+
75+
return $options;
7176
}
7277

7378
/**
@@ -79,7 +84,7 @@ protected function fillOptions(&$options)
7984
*/
8085
public function execute($options)
8186
{
82-
$this->fillOptions($options);
87+
$options = $this->fillOptions($options);
8388

8489
// Pass options to the view and return the rendered string
8590
return View::make($this->view, $options)->render();

src/Router.php

-125
This file was deleted.

0 commit comments

Comments
 (0)