Skip to content

Commit a3911a2

Browse files
committed
v1.0.0
0 parents  commit a3911a2

File tree

70 files changed

+10748
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+10748
-0
lines changed

.github/workflows/php.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: CI
2+
3+
on: [push]
4+
5+
jobs:
6+
build-test:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v2
11+
12+
- uses: php-actions/composer@v5
13+
14+
- name: PHPUnit Tests
15+
uses: php-actions/phpunit@v3
16+
with:
17+
configuration: phpunit.xml.dist

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.idea
2+
build
3+
composer.phar
4+
vendor
5+
phpunit.xml
6+
index.php
7+
.phpunit.result.cache

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Vitor Siqueira
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.

README.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# Laravel CSV
2+
PHP Laravel package to create CSV files in a memory-optimized way.
3+
4+
# Description
5+
Generate CSV files from any of these data sources: _PHP arrays_, _Laravel Collections_ or _Laravel Queries_. You can either prompt the user to download the file or store it in a Laravel disk. For larger datasets, you can generate the file in background as a Laravel Job.
6+
7+
This project was inspired on https://github.com/maatwebsite/Laravel-Excel which is a great project and can handle many formats (Excel, PDF, OpenOffice and CSV). But since it uses PhpSpreadsheet, it is not optimized for exporting large CSV files (thousands of records) causing the PHP memory exhaustion.
8+
9+
The memory usage is optimized in this project by retrieving small chunks of results at a time and outputting the CSV content directly to the client browser or to a persistent with the use of [PHP streams](https://www.php.net/manual/en/intro.stream.php).
10+
11+
This project is using some of Laravel-Excel design principles because it is both a solid work and a reference, and by doing that, it also reduces the learning curve and adaption to this library.
12+
13+
# Requirements
14+
* PHP >= 7.4
15+
* Laravel >= 6.x
16+
17+
# Installation
18+
Step 1) Add composer dependency
19+
```bash
20+
composer require vitorccs/laravel-csv
21+
```
22+
23+
Step 2) Publish the config file
24+
```bash
25+
php artisan vendor:publish --provider="Vitorccs\LaravelCsv\ServiceProviders\CsvServiceProvider" --tag=config
26+
```
27+
28+
Step 3) Edit your local `config\csv.php` file per your project preferences
29+
30+
Step 4) Create an Export class file as show below.
31+
32+
Note: you may implement _FromArray_, _FromCollection_ or _FromQuery_
33+
34+
```php
35+
namespace App\Exports;
36+
37+
use App\User;
38+
use Vitorccs\LaravelCsv\Concerns\Exportable;
39+
use Vitorccs\LaravelCsv\Concerns\FromQuery;
40+
41+
class UsersExport implements FromQuery
42+
{
43+
use Exportable;
44+
45+
public function query();
46+
{
47+
return User::query();
48+
}
49+
}
50+
```
51+
52+
Step 5) The file can now be generated by using a single line:
53+
```php
54+
# prompt the client browser to download the file
55+
return (new UsersExport)->download('users.csv');
56+
```
57+
58+
In case you want the file to be stored in the disk:
59+
```php
60+
# will save the file in 's3' disk
61+
return (new UsersExport)->store('users.csv', 's3');
62+
```
63+
64+
For larger files, you may want to generate the file in background as a Laravel Job
65+
```php
66+
# generate a {uuid-v4}.csv filename
67+
$filename = CsvHelper::filename();
68+
69+
# will create a job to create and store the file in disk
70+
# and afterwards notify the user
71+
(new BillsExport())
72+
->queue($filename, 's3')
73+
->allOnQueue('default')
74+
->chain([
75+
new NotifyCsvCreated($filename)
76+
]);
77+
```
78+
79+
# Implementations
80+
81+
## Headings
82+
`WithHeadings` adds a heading row
83+
84+
```php
85+
use Vitorccs\LaravelCsv\Concerns\Exportable;
86+
use Vitorccs\LaravelCsv\Concerns\FromArray;
87+
use Vitorccs\LaravelCsv\Concerns\WithHeadings;
88+
89+
class UsersExport implements FromArray, WithHeadings
90+
{
91+
use Exportable;
92+
93+
public function headings(): array
94+
{
95+
return ['ID', 'Name', 'Email'];
96+
}
97+
}
98+
```
99+
100+
## Mapping rows
101+
Implement `WithMapping` if you either need to set the value of each column or apply some custom formatting.
102+
103+
```php
104+
use Vitorccs\LaravelCsv\Concerns\Exportable;
105+
use Vitorccs\LaravelCsv\Concerns\FromArray;
106+
use Vitorccs\LaravelCsv\Concerns\WithMapping;
107+
108+
class UsersExport implements FromArray, WithMapping
109+
{
110+
use Exportable;
111+
112+
public function map($user): array
113+
{
114+
return [
115+
$user->id,
116+
$user->name,
117+
$user->email ?: 'N/A'
118+
];
119+
}
120+
}
121+
```
122+
123+
## Formatting columns
124+
Implement `WithColumnFormatting` to format Date and Number fields.
125+
Note: The Date must be a Carbon or Datetime object, and the number must be numeric string, integer or float. The formatting preferences are set in the config file `csv.php`.
126+
127+
```php
128+
use Vitorccs\LaravelCsv\Concerns\Exportable;
129+
use Vitorccs\LaravelCsv\Concerns\FromArray;
130+
use Vitorccs\LaravelCsv\Concerns\WithColumnFormatting;
131+
use Vitorccs\LaravelCsv\Enum\CellFormat;
132+
use Carbon\Carbon;
133+
134+
class UsersExport implements FromArray, WithColumnFormatting
135+
{
136+
use Exportable;
137+
138+
public function array(): array
139+
{
140+
return [
141+
[ Carbon::now(), Carbon::now(), 2.50, 1.00 ],
142+
[ new DateTime(), new DateTime(), 3, 2.00 ]
143+
];
144+
}
145+
146+
public function columnFormats(): array
147+
{
148+
return [
149+
'A' => CellFormat::DATE,
150+
'B' => CellFormat::DATETIME,
151+
'C' => CellFormat::DECIMAL,
152+
'D' => CellFormat::INTEGER,
153+
];
154+
}
155+
}
156+
```
157+
158+
## Limiting the results
159+
Implement the method below if you need to limit the quantity of results.
160+
161+
```php
162+
use Vitorccs\LaravelCsv\Concerns\Exportable;
163+
use Vitorccs\LaravelCsv\Concerns\FromQuery;
164+
165+
class UsersExport implements FromQuery
166+
{
167+
use Exportable;
168+
169+
public function limit(): ?int
170+
{
171+
return 5000;
172+
}
173+
}
174+
```
175+
176+
## License
177+
Released under the [MIT License](LICENSE).

composer.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "vitorccs/laravel-csv",
3+
"description": "PHP Laravel package to create CSV files in a memory-optimized way",
4+
"license": "GPL-3.0",
5+
"keywords": [
6+
"laravel",
7+
"php",
8+
"csv",
9+
"export"
10+
],
11+
"authors": [
12+
{
13+
"name": "Vitor Siqueira",
14+
"email": "vitorccsiqueira@gmail.com"
15+
}
16+
],
17+
"require": {
18+
"php": ">=7.4"
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "^9.5",
22+
"ext-sqlite3": "*",
23+
"laravel/legacy-factories": "1.x-dev",
24+
"orchestra/testbench": "^6.0"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Vitorccs\\LaravelCsv\\": "src/"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Vitorccs\\LaravelCsv\\Tests\\": "tests/"
34+
}
35+
},
36+
"extra": {
37+
"laravel": {
38+
"providers": [
39+
"Vitorccs\\LaravelCsv\\ServiceProviders\\CsvServiceProvider"
40+
]
41+
}
42+
},
43+
"minimum-stability": "dev",
44+
"prefer-stable": true,
45+
"suggest": {
46+
"league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^1.0)."
47+
},
48+
"scripts": {
49+
"test": "phpunit --testdox"
50+
}
51+
}

0 commit comments

Comments
 (0)