├── .gitignore ├── src ├── Routes │ └── api.php ├── Builder │ └── Apify.php ├── ApifyServiceProvider.php ├── Config │ └── apify.php └── Controllers │ └── ApifyController.php ├── .gitattributes ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | phpunit.xml 3 | vendor 4 | -------------------------------------------------------------------------------- /src/Routes/api.php: -------------------------------------------------------------------------------- 1 | 'api', 'namespace' => 'ConsoleTVs\Apify\Controllers', 'prefix' => 'api/'.config('apify.prefix'), 'as' => 'apify::'], function () { 5 | Route::get('/{table}/{accessor?}/{data?}', 'ApifyController@table')->name('table'); 6 | }); 7 | } 8 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "consoletvs/apify", 3 | "description": "API generator for laravel 5", 4 | "require": { 5 | "php": ">=5.5.9", 6 | "laravel/framework": "5.*", 7 | "doctrine/dbal": "^2.5" 8 | }, 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Erik Campobadal", 13 | "email": "soc@erik.cat" 14 | } 15 | ], 16 | "autoload": { 17 | "psr-4": { 18 | "ConsoleTVs\\Apify\\": "src/" 19 | } 20 | }, 21 | "minimum-stability": "dev" 22 | } 23 | -------------------------------------------------------------------------------- /src/Builder/Apify.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace ConsoleTVs\Apify\Builder; 13 | 14 | /** 15 | * This is the Apify class. 16 | * 17 | * @author Erik Campobadal 18 | */ 19 | class Apify 20 | { 21 | public $table; 22 | 23 | public function __construct($table) 24 | { 25 | $this->table = $table; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/ApifyServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 17 | __DIR__.'/Config/apify.php' => config_path('apify.php'), 18 | ], 'apify_config'); 19 | 20 | if (!$this->app->routesAreCached()) { 21 | require __DIR__.'/Routes/api.php'; 22 | } 23 | } 24 | 25 | /** 26 | * Register the application services. 27 | * 28 | * @return void 29 | */ 30 | public function register() 31 | { 32 | $this->mergeConfigFrom( 33 | __DIR__.'/Config/apify.php', 'apify' 34 | ); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Config/apify.php: -------------------------------------------------------------------------------- 1 | 'apify', 10 | 11 | /* 12 | |-------------------------------------------------------------------------- 13 | | Enables or disabled the whole API endpoints 14 | |-------------------------------------------------------------------------- 15 | */ 16 | 'enabled' => true, 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | The tables enabled for the api and it's columns 21 | |-------------------------------------------------------------------------- 22 | */ 23 | 'tables' => [ 24 | 25 | // Specify all the tables below 26 | 27 | 'users' => [ 28 | 29 | // The columns from the table that will be displayed in the JSON 30 | 31 | 'id', 'name', 'email', 'created_at', 'updated_at', 32 | 33 | ], 34 | 35 | ], 36 | ]; 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Laralum (Èrik Campobadal - ConsoleTVs) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Controllers/ApifyController.php: -------------------------------------------------------------------------------- 1 | checkTable($table, $accessor, $data)) { 22 | return $error; 23 | } 24 | 25 | $columns = $this->getAvailableColumns($table); 26 | 27 | $data = $this->getData($table, $columns, $accessor, $data); 28 | 29 | return [$table => $data]; 30 | } 31 | 32 | public function checkTable($table, $accessor, $data) 33 | { 34 | if (!Schema::hasTable($table)) { 35 | return ['error' => "The table $table does not exist."]; 36 | } 37 | 38 | if (!array_key_exists($table, config('apify.tables'))) { 39 | return ['error' => "The table $table is not set up in the configuration."]; 40 | } 41 | 42 | if ($accessor && count($data) == 0) { 43 | return ['error' => 'If you provide an accessor you need to specify some data separated by comas']; 44 | } 45 | 46 | return false; 47 | } 48 | 49 | public function getAvailableColumns($table) 50 | { 51 | $columns = Schema::getColumnListing($table); 52 | 53 | $final_columns = []; 54 | foreach ($columns as $column) { 55 | if (in_array($column, config("apify.tables.$table"))) { 56 | array_push($final_columns, $column); 57 | } 58 | } 59 | 60 | return $final_columns; 61 | } 62 | 63 | public function getData($table, $columns, $accessor = null, $data = null) 64 | { 65 | $rows = DB::table($table); 66 | 67 | if ($accessor && $data) { 68 | $rows = $rows->whereIn($accessor, $data); 69 | $rows = $rows->get(); 70 | } else { 71 | $rows = $rows->get(); 72 | } 73 | 74 | $data = []; 75 | 76 | foreach ($rows as $row) { 77 | $row_data = []; 78 | foreach ($columns as $column) { 79 | $row_data[$column] = $row->$column; 80 | } 81 | array_push($data, $row_data); 82 | } 83 | 84 | return $data; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apify 2 | ## API generator for Laravel 5 3 | 4 | [![StyleCI](https://styleci.io/repos/73238760/shield?branch=master)](https://styleci.io/repos/73238760) 5 | ![StyleCI](https://img.shields.io/badge/Built_for-Laravel-green.svg?style=flat-square) 6 | ![StyleCI](https://img.shields.io/github/license/consoletvs/apify.svg?style=flat-square) 7 | 8 | ![Apify Logo](http://i.imgur.com/cW6EpHY.png) 9 | 10 | ## Table Of Contents 11 | 12 | - [Installation](#installation) 13 | - [Configuration](#configuration) 14 | - [Usage](#usage) 15 | 16 | ## Installation 17 | 18 | To install apify use composer 19 | 20 | ### Download 21 | 22 | ``` 23 | composer require consoletvs/apify 24 | ``` 25 | 26 | ### Add service provider & alias 27 | 28 | Add the following service provider to the array in: ```config/app.php``` 29 | 30 | ```php 31 | ConsoleTVs\Apify\ApifyServiceProvider::class, 32 | ``` 33 | 34 | ### Publish the assets 35 | 36 | ``` 37 | php artisan vendor:publish 38 | ``` 39 | 40 | ## Configuration 41 | 42 | To configure the package go to: ```config/apify.php``` 43 | 44 | The default file have a valid example and it's documented, check it out, should look like this: 45 | 46 | ```php 47 | 'apify', 56 | 57 | /* 58 | |-------------------------------------------------------------------------- 59 | | Enables or disabled the whole API endpoints 60 | |-------------------------------------------------------------------------- 61 | */ 62 | 'enabled' => true, 63 | 64 | /* 65 | |-------------------------------------------------------------------------- 66 | | The tables enabled for the api and it's columns 67 | |-------------------------------------------------------------------------- 68 | */ 69 | 'tables' => [ 70 | 71 | // Specify all the tables below 72 | 73 | 'users' => [ 74 | 75 | // The columns from the table that will be displayed in the JSON 76 | 77 | 'id', 'name', 'email', 'created_at', 'updated_at' 78 | 79 | ], 80 | 81 | ], 82 | ]; 83 | ``` 84 | 85 | ## Usage 86 | 87 | Visit the endpoint like this: 88 | 89 | ``` 90 | Website URL + /api/ + Prefix + /{table}/{accessor?}/{data?} 91 | ``` 92 | 93 | *table:* is the table you want to look at, must be an index of the table array in the configuration. 94 | 95 | *accessor:* is optional, and it's the colum to filter data. 96 | 97 | *data:* is the data you're filtering, you can add multiple data separated with a , 98 | 99 | **Note:** Remember that all calls to the API goes first to the ```api``` middleware, if you need to modify the api throttle go to: ```App\Http\Kernel.php``` and modify the api throttle. 100 | 101 | ```php 102 | 'api' => [ 103 | 'throttle:60,1', 104 | 'bindings', 105 | ], 106 | ``` 107 | 108 | The ```60``` determines the max calls / minute. 109 | 110 | The ```1``` determines the minutes to wait if the max calls are exceded. 111 | 112 | Some examples: 113 | 114 | ``` 115 | http://localhost/web/Laralum3/public/api/apify/users (example URL) 116 | ``` 117 | 118 | ```json 119 | {"users":[{"id":1,"name":"\u00c8rik Campobadal","email":"ConsoleTVs@gmail.com","created_at":"2016-09-22 16:13:28","updated_at":"2016-10-02 11:18:25"},{"id":2,"name":"Second User","email":"ConsoleTV2s@gmail.com","created_at":"2016-09-21 16:13:28","updated_at":"2016-09-22 16:20:00"},{"id":3,"name":"Third User","email":"ConsoleTV3s@gmail.com","created_at":"2016-08-22 16:13:28","updated_at":"2016-09-22 16:20:00"}]} 120 | ``` 121 | 122 | ``` 123 | http://localhost/web/Laralum3/public/api/apify/users/email/ConsoleTVs@gmail.com,ConsoleTV3s@gmail.com (example URL) 124 | ``` 125 | 126 | ```json 127 | {"users":[{"id":1,"name":"\u00c8rik Campobadal","email":"ConsoleTVs@gmail.com","created_at":"2016-09-22 16:13:28","updated_at":"2016-10-02 11:18:25"},{"id":3,"name":"Third User","email":"ConsoleTV3s@gmail.com","created_at":"2016-08-22 16:13:28","updated_at":"2016-09-22 16:20:00"}]} 128 | ``` 129 | --------------------------------------------------------------------------------