f828dd12 by prami

Campaign : upload user

1 parent 2ace4d0d
Showing 551 changed files with 4785 additions and 8 deletions
...@@ -8,6 +8,8 @@ use App\Services\FormBuilder; ...@@ -8,6 +8,8 @@ use App\Services\FormBuilder;
8 use Illuminate\Http\Request; 8 use Illuminate\Http\Request;
9 use Response; 9 use Response;
10 use App\Models\User; 10 use App\Models\User;
11 use Excel;
12 use File;
11 13
12 class CampaignController extends Controller { 14 class CampaignController extends Controller {
13 15
...@@ -80,6 +82,63 @@ class CampaignController extends Controller { ...@@ -80,6 +82,63 @@ class CampaignController extends Controller {
80 // { 82 // {
81 // return view("layout.module.campaign.campaignTabData"); 83 // return view("layout.module.campaign.campaignTabData");
82 // } 84 // }
85
86 if($action == 'upload'){
87 try{
88 if($request->hasFile('file')){
89 $extension = File::extension($request->file->getClientOriginalName());
90 if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
91
92 $path = $request->file->getRealPath();
93 $data = Excel::load($path, function($reader) {
94 })->get();
95 if(!empty($data) && $data->count()){
96
97 foreach ($data as $key => $value) {
98
99 if(!empty($value->name)){
100 $campaign = Input::get("selectCampaignField");
101
102 if($campaign=="all"){
103 $campaign="";
104 $allcampaign=Campaign::where('mtype',"=","company")->orderBy('mkey')->get();
105 foreach ($allcampaign as $mkeycampaign)
106 {
107 $campaign .= $mkeycampaign->mkey.",";
108 }
109 $campaign=substr($campaign, 0, -1);
110 }
111
112 $usercampaign=User::where('username','=',trim($value->name))->first();
113 $campaignList =$usercampaign->clients.",".$campaign;
114 $data=json_decode($usercampaign->data,true);
115 if(isset($data['hrmsdata']))
116 {
117 $hrmsdata=unserialize($data['hrmsdata']);
118 $hrmsdata['clientsownerlist'] = $campaignList;
119 $data['hrmsdata']=serialize($hrmsdata);
120 }else
121 {
122 $hrmsdata['clientsownerlist'] = $campaignList;
123 $data['hrmsdata']=serialize($hrmsdata);
124 }
125 $data=json_encode($data);
126 $usercampaign->updated_at=date("Y-m-d H:i:s");
127 $usercampaign->clients=$campaignList;
128 $usercampaign->data=$data;
129 $usercampaign->save();
130 }
131
132 }
133
134 }
135
136 }
137 }
138 }catch(Exception $e) {
139 Log::error($e->getMessage());
140 }
141 }
83 } 142 }
84 143
85 public function show($id) 144 public function show($id)
......
...@@ -9,7 +9,8 @@ ...@@ -9,7 +9,8 @@
9 "laravel/framework": "5.2.*", 9 "laravel/framework": "5.2.*",
10 "league/flysystem-aws-s3-v3": "~1.0", 10 "league/flysystem-aws-s3-v3": "~1.0",
11 "marcelog/pami": "dev-master", 11 "marcelog/pami": "dev-master",
12 "marcelog/pagi": "2.*" 12 "marcelog/pagi": "2.*",
13 "maatwebsite/excel": "~2.1.0"
13 }, 14 },
14 "require-dev": { 15 "require-dev": {
15 "fzaninotto/faker": "~1.4", 16 "fzaninotto/faker": "~1.4",
......
...@@ -45,6 +45,8 @@ return [ ...@@ -45,6 +45,8 @@ return [
45 App\Providers\EventServiceProvider::class, 45 App\Providers\EventServiceProvider::class,
46 App\Providers\RouteServiceProvider::class, 46 App\Providers\RouteServiceProvider::class,
47 47
48 Maatwebsite\Excel\ExcelServiceProvider::class,
49
48 ], 50 ],
49 51
50 'aliases' => [ 52 'aliases' => [
...@@ -80,6 +82,7 @@ return [ ...@@ -80,6 +82,7 @@ return [
80 'Validator' => Illuminate\Support\Facades\Validator::class, 82 'Validator' => Illuminate\Support\Facades\Validator::class,
81 'View' => Illuminate\Support\Facades\View::class, 83 'View' => Illuminate\Support\Facades\View::class,
82 'Input' => 'Illuminate\Support\Facades\Input', 84 'Input' => 'Illuminate\Support\Facades\Input',
85 'Excel' => Maatwebsite\Excel\Facades\Excel::class,
83 86
84 ], 87 ],
85 88
......
...@@ -43,6 +43,11 @@ $dashboarduser=Auth::user(); ...@@ -43,6 +43,11 @@ $dashboarduser=Auth::user();
43 43
44 //$data['client'] = $client; 44 //$data['client'] = $client;
45 // if(isset($username))$data->username = $username; 45 // if(isset($username))$data->username = $username;
46
47 if(isset($_POST['uploadSet'])){
48 print_r($_FILES);
49 exit('test');
50 }
46 ?> 51 ?>
47 <div class="row"> 52 <div class="row">
48 <div class="col-md-12"> 53 <div class="col-md-12">
...@@ -66,16 +71,19 @@ $dashboarduser=Auth::user(); ...@@ -66,16 +71,19 @@ $dashboarduser=Auth::user();
66 71
67 <div class="panel panel-info"> 72 <div class="panel panel-info">
68 <div class="panel-body"> 73 <div class="panel-body">
74 <form method="post" id="upload_form" enctype="multipart/form-data">
75 <input type=hidden name=uploadSet value='1'>
76 {{ csrf_field() }}
69 <div class="col-md-2"> 77 <div class="col-md-2">
70 <label>Upload Users:</label> 78 <label>Upload Users:</label>
71 </div> 79 </div>
72 <div class="col-md-6"> 80 <div class="col-md-6">
73 <input type="file" name="user"> 81 <input type="file" name="file" id="file" required />
74 </div> 82 </div>
75 <div class="col-md-2"> 83 <div class="col-md-2">
76 <button id="upload" class="btn btn-xs btn-info">Upload 84 <input type="submit" name="upload" id="upload" class="btn btn-primary" value="Upload">
77 </button>
78 </div> 85 </div>
86 </form>
79 </div> 87 </div>
80 </div> 88 </div>
81 89
...@@ -113,6 +121,31 @@ $dashboarduser=Auth::user(); ...@@ -113,6 +121,31 @@ $dashboarduser=Auth::user();
113 </div> 121 </div>
114 122
115 <script> 123 <script>
124 $(document).ready(function(){
125 $('#upload_form').on('submit', function(event){
126 var selectCampaignField = $('#selectCampaignField').val();
127 var formData = new FormData(this);
128 formData.append('selectCampaignField', selectCampaignField);
129 event.preventDefault();
130 $.ajax({
131 url:"campaign?action=upload",
132 method:"POST",
133 data:formData,
134 dataType:'JSON',
135 contentType: false,
136 cache: false,
137 processData: false,
138 success:function(data)
139 {
140 doAjax("campaign/show","campaign="+selectCampaignField,"campaignDetailsArea","","","GET");
141 simpleNotification('Success','topRight',"Campaign assign suceesfully!");
142 }
143 });
144 });
145 });
146 </script>
147
148 <script>
116 function addInQueue(){ 149 function addInQueue(){
117 var data = 'radioValue='+$("input[name='query']:checked").val(); 150 var data = 'radioValue='+$("input[name='query']:checked").val();
118 151
......
This diff could not be displayed because it is too large.
...@@ -8,16 +8,16 @@ $baseDir = dirname($vendorDir); ...@@ -8,16 +8,16 @@ $baseDir = dirname($vendorDir);
8 return array( 8 return array(
9 '0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php', 9 '0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php',
10 '1d1b89d124cc9cb8219922c9d5569199' => $vendorDir . '/hamcrest/hamcrest-php/hamcrest/Hamcrest.php', 10 '1d1b89d124cc9cb8219922c9d5569199' => $vendorDir . '/hamcrest/hamcrest-php/hamcrest/Hamcrest.php',
11 'a0edc8309cc5e1d60e3047b5df6b7052' => $vendorDir . '/guzzlehttp/psr7/src/functions_include.php',
12 'c964ee0ededf28c96ebd9db5099ef910' => $vendorDir . '/guzzlehttp/promises/src/functions_include.php', 11 'c964ee0ededf28c96ebd9db5099ef910' => $vendorDir . '/guzzlehttp/promises/src/functions_include.php',
13 'b067bc7112e384b61c701452d53a14a8' => $vendorDir . '/mtdowling/jmespath.php/src/JmesPath.php', 12 'a0edc8309cc5e1d60e3047b5df6b7052' => $vendorDir . '/guzzlehttp/psr7/src/functions_include.php',
14 '37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php', 13 '37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php',
14 'b067bc7112e384b61c701452d53a14a8' => $vendorDir . '/mtdowling/jmespath.php/src/JmesPath.php',
15 '667aeda72477189d0494fecd327c3641' => $vendorDir . '/symfony/var-dumper/Resources/functions/dump.php', 15 '667aeda72477189d0494fecd327c3641' => $vendorDir . '/symfony/var-dumper/Resources/functions/dump.php',
16 '8a9dc1de0ca7e01f3e08231539562f61' => $vendorDir . '/aws/aws-sdk-php/src/functions.php', 16 '8a9dc1de0ca7e01f3e08231539562f61' => $vendorDir . '/aws/aws-sdk-php/src/functions.php',
17 '2c102faa651ef8ea5874edb585946bce' => $vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php', 17 '2c102faa651ef8ea5874edb585946bce' => $vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php',
18 'bd9634f2d41831496de0d3dfe4c94881' => $vendorDir . '/symfony/polyfill-php56/bootstrap.php', 18 'bd9634f2d41831496de0d3dfe4c94881' => $vendorDir . '/symfony/polyfill-php56/bootstrap.php',
19 'e7223560d890eab89cda23685e711e2c' => $vendorDir . '/psy/psysh/src/Psy/functions.php',
20 '5255c38a0faeba867671b61dfda6d864' => $vendorDir . '/paragonie/random_compat/lib/random.php', 19 '5255c38a0faeba867671b61dfda6d864' => $vendorDir . '/paragonie/random_compat/lib/random.php',
20 'e7223560d890eab89cda23685e711e2c' => $vendorDir . '/psy/psysh/src/Psy/functions.php',
21 'f0906e6318348a765ffb6eb24e0d0938' => $vendorDir . '/laravel/framework/src/Illuminate/Foundation/helpers.php', 21 'f0906e6318348a765ffb6eb24e0d0938' => $vendorDir . '/laravel/framework/src/Illuminate/Foundation/helpers.php',
22 '58571171fd5812e6e447dce228f52f4d' => $vendorDir . '/laravel/framework/src/Illuminate/Support/helpers.php', 22 '58571171fd5812e6e447dce228f52f4d' => $vendorDir . '/laravel/framework/src/Illuminate/Support/helpers.php',
23 ); 23 );
......
...@@ -9,7 +9,9 @@ return array( ...@@ -9,7 +9,9 @@ return array(
9 'Psr\\Log\\' => array($vendorDir . '/psr/log'), 9 'Psr\\Log\\' => array($vendorDir . '/psr/log'),
10 'Prophecy\\' => array($vendorDir . '/phpspec/prophecy/src'), 10 'Prophecy\\' => array($vendorDir . '/phpspec/prophecy/src'),
11 'PhpSpec' => array($vendorDir . '/phpspec/phpspec/src'), 11 'PhpSpec' => array($vendorDir . '/phpspec/phpspec/src'),
12 'PHPExcel' => array($vendorDir . '/phpoffice/phpexcel/Classes'),
12 'Mockery' => array($vendorDir . '/mockery/mockery/library'), 13 'Mockery' => array($vendorDir . '/mockery/mockery/library'),
14 'Maatwebsite\\Excel\\' => array($vendorDir . '/maatwebsite/excel/src'),
13 'JakubOnderka\\PhpConsoleHighlighter' => array($vendorDir . '/jakub-onderka/php-console-highlighter/src'), 15 'JakubOnderka\\PhpConsoleHighlighter' => array($vendorDir . '/jakub-onderka/php-console-highlighter/src'),
14 'JakubOnderka\\PhpConsoleColor' => array($vendorDir . '/jakub-onderka/php-console-color/src'), 16 'JakubOnderka\\PhpConsoleColor' => array($vendorDir . '/jakub-onderka/php-console-color/src'),
15 'Doctrine\\Common\\Inflector\\' => array($vendorDir . '/doctrine/inflector/lib'), 17 'Doctrine\\Common\\Inflector\\' => array($vendorDir . '/doctrine/inflector/lib'),
......
...@@ -6,9 +6,10 @@ $vendorDir = dirname(dirname(__FILE__)); ...@@ -6,9 +6,10 @@ $vendorDir = dirname(dirname(__FILE__));
6 $baseDir = dirname($vendorDir); 6 $baseDir = dirname($vendorDir);
7 7
8 return array( 8 return array(
9 'phpDocumentor\\Reflection\\' => array($vendorDir . '/phpdocumentor/reflection-common/src', $vendorDir . '/phpdocumentor/type-resolver/src', $vendorDir . '/phpdocumentor/reflection-docblock/src'), 9 'phpDocumentor\\Reflection\\' => array($vendorDir . '/phpdocumentor/reflection-common/src', $vendorDir . '/phpdocumentor/reflection-docblock/src', $vendorDir . '/phpdocumentor/type-resolver/src'),
10 'XdgBaseDir\\' => array($vendorDir . '/dnoegel/php-xdg-base-dir/src'), 10 'XdgBaseDir\\' => array($vendorDir . '/dnoegel/php-xdg-base-dir/src'),
11 'Webmozart\\Assert\\' => array($vendorDir . '/webmozart/assert/src'), 11 'Webmozart\\Assert\\' => array($vendorDir . '/webmozart/assert/src'),
12 'TijsVerkoyen\\CssToInlineStyles\\' => array($vendorDir . '/tijsverkoyen/css-to-inline-styles/src'),
12 'Symfony\\Polyfill\\Util\\' => array($vendorDir . '/symfony/polyfill-util'), 13 'Symfony\\Polyfill\\Util\\' => array($vendorDir . '/symfony/polyfill-util'),
13 'Symfony\\Polyfill\\Php56\\' => array($vendorDir . '/symfony/polyfill-php56'), 14 'Symfony\\Polyfill\\Php56\\' => array($vendorDir . '/symfony/polyfill-php56'),
14 'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'), 15 'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'),
......
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
1 ## Laravel Excel v2.1.* for Laravel 5
2
3 Looking for Laravel Excel for Laravel 4? Visit the [`1.3` branch](https://github.com/Maatwebsite/Laravel-Excel/tree/1.3)
4
5 [<img src="http://www.maatwebsite.nl/img/excel_banner.jpg"/>](http://www.maatwebsite.nl/laravel-excel/docs)
6 [<img src="https://cloud.githubusercontent.com/assets/7728097/6332170/1b495af2-bb84-11e4-9a93-34a9abc01840.jpg"/>](http://www.maatwebsite.nl/vacature-php-programmeur-maastricht)
7
8 Laravel Excel brings the power of PHPOffice's PHPExcel to Laravel 5 with a touch of the Laravel Magic. It includes features like: importing Excel and CSV to collections, exporting models, array's and views to Excel, importing batches of files and importing a file by a config file.
9
10 - Import into Laravel **Collections**
11 - Export **Blade views** to Excel and CSV with optional CSS styling
12 - **Batch** imports
13 - A lot of optional **config settings**
14 - Easy **cell caching**
15 - Chunked and queued importer
16 - ExcelFile method injections
17 - Editing existing Excel files
18 - **Advanced import** by config files
19 - and many more...
20
21 ---
22
23 ```php
24 Excel::create('Laravel Excel', function($excel) {
25
26 $excel->sheet('Excel sheet', function($sheet) {
27
28 $sheet->setOrientation('landscape');
29
30 });
31
32 })->export('xls');
33 ```
34
35 ---
36
37 [![Build Status](https://travis-ci.org/Maatwebsite/Laravel-Excel.svg?branch=master)](https://travis-ci.org/Maatwebsite/Laravel-Excel)
38 [![Latest Stable Version](https://poser.pugx.org/maatwebsite/excel/v/stable.png)](https://packagist.org/packages/maatwebsite/excel) [![Total Downloads](https://poser.pugx.org/maatwebsite/excel/downloads.png)](https://packagist.org/packages/maatwebsite/excel) [![License](https://poser.pugx.org/maatwebsite/excel/license.png)](https://packagist.org/packages/maatwebsite/excel)
39 [![Monthly Downloads](https://poser.pugx.org/maatwebsite/excel/d/monthly.png)](https://packagist.org/packages/maatwebsite/excel)
40 [![Daily Downloads](https://poser.pugx.org/maatwebsite/excel/d/daily.png)](https://packagist.org/packages/maatwebsite/excel)
41
42 [![Help the project](http://www.pledgie.com/campaigns/30385.png?skin_name=chrome)](http://pledgie.com/campaigns/30385)
43
44 #Installation
45
46 Require this package in your `composer.json` and update composer. This will download the package and PHPExcel of PHPOffice.
47
48 ```php
49 "maatwebsite/excel": "~2.1.0"
50 ```
51
52 After updating composer, add the ServiceProvider to the providers array in `config/app.php`
53
54 ```php
55 Maatwebsite\Excel\ExcelServiceProvider::class,
56 ```
57
58 You can use the facade for shorter code. Add this to your aliases:
59
60 ```php
61 'Excel' => Maatwebsite\Excel\Facades\Excel::class,
62 ```
63
64 The class is bound to the ioC as `excel`
65
66 ```php
67 $excel = App::make('excel');
68 ```
69
70 To publish the config settings in Laravel 5 use:
71
72 ```php
73 php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
74 ```
75
76 This will add an `excel.php` config file to your config folder.
77
78 # Documentation
79
80 The complete documentation can be found at: [http://www.maatwebsite.nl/laravel-excel/docs](http://www.maatwebsite.nl/laravel-excel/docs)
81
82 # Support
83
84 Support only through Github. Please don't mail us about issues, make a Github issue instead.
85
86 # Contributing
87
88 **ALL** bug fixes should be made to appropriate branch (e.g. `2.0` for 2.0.* bug fixes). Bug fixes should never be sent to the `master` branch.
89
90 More about contributing can be found at: [http://www.maatwebsite.nl/laravel-excel/docs/getting-started#contributing](http://www.maatwebsite.nl/laravel-excel/docs/getting-started#contributing)
91
92 # License
93
94 This package is licensed under LGPL. You are free to use it in personal and commercial projects. The code can be forked and modified, but the original copyright author should always be included!
1 {
2 "name": "maatwebsite/excel",
3 "description": "An eloquent way of importing and exporting Excel and CSV in Laravel 4 with the power of PHPExcel",
4 "license": "LGPL",
5 "keywords": [
6 "laravel",
7 "phpexcel",
8 "excel",
9 "csv",
10 "export",
11 "import",
12 "batch"
13 ],
14 "authors": [
15 {
16 "name": "Maatwebsite.nl",
17 "email": "[email protected]"
18 }
19 ],
20 "require": {
21 "php": ">=5.5",
22 "phpoffice/phpexcel": "1.8.*",
23 "illuminate/cache": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*",
24 "illuminate/config": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*",
25 "illuminate/filesystem": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*",
26 "illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*",
27 "nesbot/carbon": "~1.0",
28 "tijsverkoyen/css-to-inline-styles": "~2.0"
29 },
30 "require-dev": {
31 "phpseclib/phpseclib": "~1.0",
32 "phpunit/phpunit": "~4.0",
33 "mockery/mockery": "~0.9",
34 "orchestra/testbench": "3.1.*"
35 },
36 "suggest": {
37 "illuminate/http": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*",
38 "illuminate/routing": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*",
39 "illuminate/view": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*",
40 "illuminate/queue": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*"
41 },
42 "autoload": {
43 "classmap": [
44 "src/Maatwebsite/Excel"
45 ],
46 "psr-0": {
47 "Maatwebsite\\Excel\\": "src/"
48 }
49 },
50 "autoload-dev": {
51 "classmap": [
52 "tests/TestCase.php"
53 ]
54 }
55 }
1 REDIS0006ܳCZV
...\ No newline at end of file ...\ No newline at end of file
1 {
2 "providers": [
3 "Maatwebsite\\Excel\\ExcelServiceProvider"
4 ],
5 "aliases": [
6 {
7 "alias": "Excel",
8 "facade": "Maatwebsite\\Excel\\Facades\\Excel"
9 }
10 ]
11 }
1 <?php namespace Maatwebsite\Excel\Classes;
2
3 use PHPExcel_Settings;
4 use PHPExcel_CachedObjectStorageFactory;
5
6 class Cache {
7
8 /**
9 * PHPExcel cache class
10 * @var string
11 */
12 protected $class = 'PHPExcel_CachedObjectStorageFactory';
13
14 /**
15 * Available caching drivers
16 * @var array
17 */
18 protected $available = [
19 'memory' => 'cache_in_memory',
20 'gzip' => 'cache_in_memory_gzip',
21 'serialized' => 'cache_in_memory_serialized',
22 'igbinary' => 'cache_igbinary',
23 'discISAM' => 'cache_to_discISAM',
24 'apc' => 'cache_to_apc',
25 'memcache' => 'cache_to_memcache',
26 'temp' => 'cache_to_phpTemp',
27 'wincache' => 'cache_to_wincache',
28 'sqlite' => 'cache_to_sqlite',
29 'sqlite3' => 'cache_to_sqlite3'
30 ];
31
32 /**
33 * The name of the config file
34 * @var string
35 */
36 private $configName = 'excel.cache';
37
38 /**
39 * Cache constructor
40 */
41 public function __construct()
42 {
43 // Get driver and settings from the config
44 $this->driver = config($this->configName . '.driver', 'memory');
45 $this->settings = config($this->configName . '.settings', []);
46
47 // Init if caching is enabled
48 if ($this->isEnabled())
49 $this->init();
50 }
51
52 /**
53 * Init the cache
54 * @return void
55 */
56 public function init()
57 {
58 // Find the cache driver
59 $this->findDriver();
60
61 // Set the storage driver
62 PHPExcel_Settings::setCacheStorageMethod($this->method, $this->settings);
63 }
64
65 /**
66 * Set the right driver
67 * @return void
68 */
69 public function findDriver()
70 {
71 $property = $this->detect();
72 $this->method = constant($this->class . '::' . $property);
73 }
74
75 /**
76 * Detect the caching driver
77 * @return string $driver
78 */
79 protected function detect()
80 {
81 // Add additional settings
82 $this->addAdditionalSettings();
83
84 // return the driver
85 return isset($this->available[$this->driver]) ? $this->available[$this->driver] : reset($this->available);
86 }
87
88 /**
89 * Add additional settings for the current driver
90 * @return void
91 */
92 protected function addAdditionalSettings()
93 {
94 switch ($this->driver)
95 {
96 case 'memcache':
97
98 // Add extra memcache settings
99 $this->settings = array_merge($this->settings, [
100 'memcacheServer' => config($this->configName . '.memcache.host', 'localhost'),
101 'memcachePort' => config($this->configName . '.memcache.port', 11211)
102 ]);
103
104 break;
105
106 case 'discISAM':
107
108 // Add dir
109 $this->settings = array_merge($this->settings, [
110 'dir' => config($this->configName . '.dir', storage_path('cache')),
111 ]);
112
113 break;
114 }
115 }
116
117 /**
118 * Check if caching is enabled
119 * @return boolean
120 */
121 public function isEnabled()
122 {
123 return config($this->configName . '.enable', true) ? true : false;
124 }
125 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Classes;
2
3 use PHPExcel_IOFactory;
4 use Illuminate\Filesystem\Filesystem;
5 use Maatwebsite\Excel\Exceptions\LaravelExcelException;
6
7 class FormatIdentifier {
8
9 /**
10 * Available formats
11 *
12 * @var array
13 * @access protected
14 */
15 protected $formats = [
16 'Excel2007',
17 'Excel5',
18 'Excel2003XML',
19 'OOCalc',
20 'SYLK',
21 'Gnumeric',
22 'CSV',
23 'HTML',
24 'PDF'
25 ];
26
27 /**
28 * Construct new format identifier
29 * @param Filesystem $filesystem
30 */
31 public function __construct(Filesystem $filesystem)
32 {
33 $this->filesystem = $filesystem;
34 }
35
36 /**
37 * Get the file format by file
38 * @param $file
39 * @throws LaravelExcelException
40 * @return string $format
41 */
42 public function getFormatByFile($file)
43 {
44 // get the file extension
45 $ext = $this->getExtension($file);
46
47 // get the file format
48 $format = $this->getFormatByExtension($ext);
49
50 // Check if the file can be read
51 if ($this->canRead($format, $file))
52 return $format;
53
54 // Do a last try to init the file with all available readers
55 return $this->lastResort($file, $format, $ext);
56 }
57
58 /**
59 * Identify file format
60 * @param $ext
61 * @return string $format
62 */
63 public function getFormatByExtension($ext)
64 {
65 switch ($ext)
66 {
67
68 /*
69 |--------------------------------------------------------------------------
70 | Excel 2007
71 |--------------------------------------------------------------------------
72 */
73 case 'xlsx':
74 case 'xlsm':
75 case 'xltx':
76 case 'xltm':
77 return 'Excel2007';
78 break;
79
80 /*
81 |--------------------------------------------------------------------------
82 | Excel5
83 |--------------------------------------------------------------------------
84 */
85 case 'xls':
86 case 'xlt':
87 return 'Excel5';
88 break;
89
90 /*
91 |--------------------------------------------------------------------------
92 | OOCalc
93 |--------------------------------------------------------------------------
94 */
95 case 'ods':
96 case 'ots':
97 return 'OOCalc';
98 break;
99
100 /*
101 |--------------------------------------------------------------------------
102 | SYLK
103 |--------------------------------------------------------------------------
104 */
105 case 'slk':
106 return 'SYLK';
107 break;
108
109 /*
110 |--------------------------------------------------------------------------
111 | Excel2003XML
112 |--------------------------------------------------------------------------
113 */
114 case 'xml':
115 return 'Excel2003XML';
116 break;
117
118 /*
119 |--------------------------------------------------------------------------
120 | Gnumeric
121 |--------------------------------------------------------------------------
122 */
123 case 'gnumeric':
124 return 'Gnumeric';
125 break;
126
127 /*
128 |--------------------------------------------------------------------------
129 | HTML
130 |--------------------------------------------------------------------------
131 */
132 case 'htm':
133 case 'html':
134 return 'HTML';
135 break;
136
137 /*
138 |--------------------------------------------------------------------------
139 | CSV
140 |--------------------------------------------------------------------------
141 */
142 case 'csv':
143 case 'txt':
144 return 'CSV';
145 break;
146
147 /*
148 |--------------------------------------------------------------------------
149 | PDF
150 |--------------------------------------------------------------------------
151 */
152 case 'pdf':
153 return 'PDF';
154 break;
155 }
156 }
157
158 /**
159 * Get the content type by file format
160 * @param string $format
161 * @return string $contentType
162 */
163 public function getContentTypeByFormat($format)
164 {
165 switch ($format)
166 {
167
168 /*
169 |--------------------------------------------------------------------------
170 | Excel 2007
171 |--------------------------------------------------------------------------
172 */
173 case 'Excel2007':
174 return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8';
175 break;
176
177 /*
178 |--------------------------------------------------------------------------
179 | Excel5
180 |--------------------------------------------------------------------------
181 */
182 case 'Excel5':
183 return 'application/vnd.ms-excel; charset=UTF-8';
184 break;
185
186 /*
187 |--------------------------------------------------------------------------
188 | HTML
189 |--------------------------------------------------------------------------
190 */
191 case 'HTML':
192 return 'HTML';
193 break;
194
195 /*
196 |--------------------------------------------------------------------------
197 | CSV
198 |--------------------------------------------------------------------------
199 */
200 case 'CSV':
201 return 'application/csv; charset=UTF-8';
202 break;
203
204 /*
205 |--------------------------------------------------------------------------
206 | PDF
207 |--------------------------------------------------------------------------
208 */
209 case 'PDF':
210 return'application/pdf; charset=UTF-8';
211 break;
212 }
213 }
214
215 /**
216 * Try every reader we have
217 * @param $file
218 * @param bool $wrongFormat
219 * @param string $ext
220 * @throws LaravelExcelException
221 * @return string $format
222 */
223 protected function lastResort($file, $wrongFormat = false, $ext = 'xls')
224 {
225 // Loop through all available formats
226 foreach ($this->formats as $format)
227 {
228 // Check if the file could be read
229 if ($wrongFormat != $format && $this->canRead($format, $file))
230 return $format;
231 }
232
233 // Give up searching and throw an exception
234 throw new LaravelExcelException('[ERROR] Reader could not identify file format for file [' . $file . '] with extension [' . $ext . ']');
235 }
236
237 /**
238 * Check if we can read the file
239 * @param $format
240 * @param $file
241 * @return boolean
242 */
243 protected function canRead($format, $file)
244 {
245 if ($format)
246 {
247 $reader = $this->initReader($format);
248
249 return $reader && $reader->canRead($file);
250 }
251
252 return false;
253 }
254
255 /**
256 * Init the reader based on the format
257 * @param string $format
258 * @return \PHPExcel_Reader_IReader
259 */
260 protected function initReader($format)
261 {
262 return PHPExcel_IOFactory::createReader($format);
263 }
264
265 /**
266 * Get the file extension
267 * @param string $file
268 * @return string
269 */
270 protected function getExtension($file)
271 {
272 return strtolower($this->filesystem->extension($file));
273 }
274 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Classes;
2
3 use PHPExcel as PHPOffice_PHPExcel;
4
5 /**
6 *
7 * Laravel wrapper for PHPExcel
8 *
9 * @category Laravel Excel
10 * @package maatwebsite/excel
11 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
12 * @copyright Original Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
13 * @author Maatwebsite <[email protected]>
14 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
15 */
16 class PHPExcel extends PHPOffice_PHPExcel {
17
18 /**
19 * Allowed autofill properties
20 * @var array
21 */
22 public $allowedProperties = [
23 'creator',
24 'lastModifiedBy',
25 'description',
26 'subject',
27 'keywords',
28 'category',
29 'manager',
30 'company'
31 ];
32
33 /**
34 * Create sheet and add it to this workbook
35 *
36 * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
37 * @param bool|string $title
38 * @throws \PHPExcel_Exception
39 * @return LaravelExcelWorksheet
40 */
41 public function createSheet($iSheetIndex = null, $title = false)
42 {
43 // Init new Laravel Excel worksheet
44 $newSheet = new LaravelExcelWorksheet($this, $title);
45
46 // Add the sheet
47 $this->addSheet($newSheet, $iSheetIndex);
48
49 // Return the sheet
50 return $newSheet;
51 }
52
53 /**
54 * Check if the user change change the workbook property
55 * @param string $method
56 * @return boolean
57 */
58 public function isChangeableProperty($method)
59 {
60 $name = lcfirst(str_replace('set', '', $method));
61
62 return in_array($name, $this->getAllowedProperties()) ? true : false;
63 }
64
65 /**
66 * Set default properties
67 * @param array $custom
68 * @return void
69 */
70 public function setDefaultProperties($custom = [])
71 {
72 // Get the properties
73 $properties = $this->getProperties();
74
75 // Get fillable properties
76 foreach ($this->getAllowedProperties() as $prop)
77 {
78 // Get the method
79 $method = 'set' . ucfirst($prop);
80
81 // get the value
82 $value = in_array($prop, array_keys($custom)) ? $custom[$prop] : config('excel.properties.' . $prop, null);
83
84 // set the property
85 call_user_func_array([$properties, $method], [$value]);
86 }
87 }
88
89 /**
90 * load info from parent obj
91 * @param \PHPExcel $object
92 * @return $this
93 */
94 function cloneParent(\PHPExcel $object)
95 {
96 // Init new reflection object
97 $class = new \ReflectionClass(get_class($object));
98
99 // Loop through all properties
100 foreach($class->getProperties() as $property)
101 {
102 // Make the property public
103 $property->setAccessible(true);
104
105 // Set the found value to this sheet
106 $property->setValue(
107 $this,
108 $property->getValue($object)
109 );
110 }
111
112 return $this;
113 }
114
115 /**
116 * Return all allowed properties
117 * @return array
118 */
119 public function getAllowedProperties()
120 {
121 return $this->allowedProperties;
122 }
123 }
1 <?php namespace Maatwebsite\Excel\Collections;
2
3 /**
4 *
5 * LaravelExcel CellCollection
6 *
7 * @category Laravel Excel
8 * @package maatwebsite/excel
9 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
10 * @author Maatwebsite <[email protected]>
11 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
12 */
13 class CellCollection extends ExcelCollection {
14
15 /**
16 * Create a new collection.
17 * @param array $items
18 * @return \Maatwebsite\Excel\Collections\CellCollection
19 */
20 public function __construct(array $items = [])
21 {
22 $this->setItems($items);
23 }
24
25 /**
26 * Set the items
27 * @param array $items
28 * @return void
29 */
30 public function setItems($items)
31 {
32 foreach ($items as $name => $value)
33 {
34 $value = !empty($value) || is_numeric($value) ? $value : null;
35 $this->put($name, $value);
36 }
37 }
38
39 /**
40 * Dynamically get values
41 * @param string $key
42 * @return string
43 */
44 public function __get($key)
45 {
46 if ($this->has($key))
47 return $this->get($key);
48 }
49
50 /**
51 * Determine if an attribute exists on the model.
52 *
53 * @param string $key
54 * @return bool
55 */
56 public function __isset($key)
57 {
58 return $this->has($key);
59 }
60 }
1 <?php namespace Maatwebsite\Excel\Collections;
2
3 use Illuminate\Support\Collection;
4
5 /**
6 *
7 * LaravelExcel ExcelCollection
8 *
9 * @category Laravel Excel
10 * @version 1.0.0
11 * @package maatwebsite/excel
12 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
13 * @author Maatwebsite <[email protected]>
14 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
15 */
16 class ExcelCollection extends Collection {
17
18 /**
19 * Sheet title
20 * @var [type]
21 */
22 protected $title;
23
24 /**
25 * Get the title
26 * @return string
27 */
28 public function getTitle()
29 {
30 return $this->title;
31 }
32
33 /**
34 * Set the title
35 * @param $title
36 */
37 public function setTitle($title)
38 {
39 $this->title = $title;
40 }
41 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Collections;
2
3 /**
4 *
5 * LaravelExcel RowCollection
6 *
7 * @category Laravel Excel
8 * @version 1.0.0
9 * @package maatwebsite/excel
10 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
11 * @author Maatwebsite <[email protected]>
12 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
13 */
14 class RowCollection extends ExcelCollection {
15
16 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Collections;
2
3 /**
4 *
5 * LaravelExcel SheetCollection
6 *
7 * @category Laravel Excel
8 * @version 1.0.0
9 * @package maatwebsite/excel
10 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
11 * @author Maatwebsite <[email protected]>
12 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
13 */
14 class SheetCollection extends ExcelCollection {
15
16 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel;
2
3 use Closure;
4 use Maatwebsite\Excel\Readers\Batch;
5 use Maatwebsite\Excel\Classes\PHPExcel;
6 use Maatwebsite\Excel\Readers\LaravelExcelReader;
7 use Maatwebsite\Excel\Writers\LaravelExcelWriter;
8 use Maatwebsite\Excel\Exceptions\LaravelExcelException;
9
10 /**
11 *
12 * Laravel wrapper for PHPExcel
13 *
14 * @category Laravel Excel
15 * @version 1.0.0
16 * @package maatwebsite/excel
17 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
18 * @author Maatwebsite <[email protected]>
19 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
20 */
21 class Excel {
22
23 /**
24 * Filter
25 * @var array
26 */
27 protected $filters = [
28 'registered' => [],
29 'enabled' => []
30 ];
31
32 /**
33 * Excel object
34 * @var PHPExcel
35 */
36 protected $excel;
37
38 /**
39 * Reader object
40 * @var LaravelExcelReader
41 */
42 protected $reader;
43
44 /**
45 * Writer object
46 * @var LaravelExcelWriter
47 */
48 protected $writer;
49
50 /**
51 * Construct Excel
52 * @param PHPExcel $excel
53 * @param LaravelExcelReader $reader
54 * @param LaravelExcelWriter $writer
55 */
56 public function __construct(PHPExcel $excel, LaravelExcelReader $reader, LaravelExcelWriter $writer)
57 {
58 // Set Excel dependencies
59 $this->excel = $excel;
60 $this->reader = $reader;
61 $this->writer = $writer;
62 }
63
64 /**
65 * Create a new file
66 * @param $filename
67 * @param callable|null $callback
68 * @return LaravelExcelWriter
69 */
70 public function create($filename, $callback = null)
71 {
72 // Writer instance
73 $writer = clone $this->writer;
74
75 // Disconnect worksheets to prevent unnecessary ones
76 $this->excel->disconnectWorksheets();
77
78 // Inject our excel object
79 $writer->injectExcel($this->excel);
80
81 // Set the filename and title
82 $writer->setFileName($filename);
83 $writer->setTitle($filename);
84
85 // Do the callback
86 if ($callback instanceof Closure)
87 call_user_func($callback, $writer);
88
89 // Return the writer object
90 return $writer;
91 }
92
93 /**
94 *
95 * Load an existing file
96 *
97 * @param string $file The file we want to load
98 * @param callback|null $callback
99 * @param string|null $encoding
100 * @param bool $noBasePath
101 * @param callback|null $callbackConfigReader
102 * @return LaravelExcelReader
103 */
104 public function load($file, $callback = null, $encoding = null, $noBasePath = false, $callbackConfigReader = null)
105 {
106 // Reader instance
107 $reader = clone $this->reader;
108
109 // Inject excel object
110 $reader->injectExcel($this->excel);
111
112 // Enable filters
113 $reader->setFilters($this->filters);
114
115 // Set the encoding
116 $encoding = is_string($callback) ? $callback : $encoding;
117
118 // Start loading
119 $reader->load($file, $encoding, $noBasePath, $callbackConfigReader);
120
121 // Do the callback
122 if ($callback instanceof Closure)
123 call_user_func($callback, $reader);
124
125 // Return the reader object
126 return $reader;
127 }
128
129 /**
130 * Set select sheets
131 * @param $sheets
132 * @return LaravelExcelReader
133 */
134 public function selectSheets($sheets = [])
135 {
136 $sheets = is_array($sheets) ? $sheets : func_get_args();
137 $this->reader->setSelectedSheets($sheets);
138
139 return $this;
140 }
141
142 /**
143 * Select sheets by index
144 * @param array $sheets
145 * @return $this
146 */
147 public function selectSheetsByIndex($sheets = [])
148 {
149 $sheets = is_array($sheets) ? $sheets : func_get_args();
150 $this->reader->setSelectedSheetIndices($sheets);
151
152 return $this;
153 }
154
155 /**
156 * Batch import
157 * @param $files
158 * @param callback $callback
159 * @return PHPExcel
160 */
161 public function batch($files, Closure $callback)
162 {
163 $batch = new Batch;
164
165 return $batch->start($this, $files, $callback);
166 }
167
168 /**
169 * Create a new file and share a view
170 * @param string $view
171 * @param array $data
172 * @param array $mergeData
173 * @return LaravelExcelWriter
174 */
175 public function shareView($view, $data = [], $mergeData = [])
176 {
177 return $this->create($view)->shareView($view, $data, $mergeData);
178 }
179
180 /**
181 * Create a new file and load a view
182 * @param string $view
183 * @param array $data
184 * @param array $mergeData
185 * @return LaravelExcelWriter
186 */
187 public function loadView($view, $data = [], $mergeData = [])
188 {
189 return $this->shareView($view, $data, $mergeData);
190 }
191
192 /**
193 * Set filters
194 * @param array $filters
195 * @return Excel
196 */
197 public function registerFilters($filters = [])
198 {
199 // If enabled array key exists
200 if(array_key_exists('enabled', $filters))
201 {
202 // Set registered array
203 $registered = $filters['registered'];
204
205 // Filter on enabled
206 $this->filter($filters['enabled']);
207 }
208 else
209 {
210 $registered = $filters;
211 }
212
213 // Register the filters
214 $this->filters['registered'] = !empty($this->filters['registered']) ? array_merge($this->filters['registered'], $registered) : $registered;
215 return $this;
216 }
217
218 /**
219 * Enable certain filters
220 * @param string|array $filter
221 * @param bool|false|string $class
222 * @return Excel
223 */
224 public function filter($filter, $class = false)
225 {
226 // Add multiple filters
227 if(is_array($filter))
228 {
229 $this->filters['enabled'] = !empty($this->filters['enabled']) ? array_merge($this->filters['enabled'], $filter) : $filter;
230 }
231 else
232 {
233 // Add single filter
234 $this->filters['enabled'][] = $filter;
235
236 // Overrule filter class for this request
237 if($class)
238 $this->filters['registered'][$filter] = $class;
239 }
240
241 // Remove duplicates
242 $this->filters['enabled'] = array_unique($this->filters['enabled']);
243
244 return $this;
245 }
246
247 /**
248 * Get register, enabled (or both) filters
249 * @param string|boolean $key [description]
250 * @return array
251 */
252 public function getFilters($key = false)
253 {
254 return $key ? $this->filters[$key] : $this->filters;
255 }
256
257 /**
258 * Dynamically call methods
259 * @throws LaravelExcelException
260 */
261 public function __call($method, $params)
262 {
263 // If the dynamic call starts with "with", add the var to the data array
264 if (method_exists($this->excel, $method))
265 {
266 // Call the method from the excel object with the given params
267 return call_user_func_array([$this->excel, $method], $params);
268 }
269
270 // If reader method exists, call that one
271 if (method_exists($this->reader, $method))
272 {
273 // Call the method from the reader object with the given params
274 return call_user_func_array([$this->reader, $method], $params);
275 }
276
277 throw new LaravelExcelException('Laravel Excel method [' . $method . '] does not exist');
278 }
279 }
1 <?php namespace Maatwebsite\Excel;
2
3 use PHPExcel_Settings;
4 use PHPExcel_Shared_Font;
5 use Maatwebsite\Excel\Readers\Html;
6 use Maatwebsite\Excel\Classes\Cache;
7 use Maatwebsite\Excel\Classes\PHPExcel;
8 use Illuminate\Support\ServiceProvider;
9 use Illuminate\Support\Facades\Response;
10 use Maatwebsite\Excel\Parsers\CssParser;
11 use Maatwebsite\Excel\Parsers\ViewParser;
12 use Maatwebsite\Excel\Classes\FormatIdentifier;
13 use Maatwebsite\Excel\Readers\LaravelExcelReader;
14 use Maatwebsite\Excel\Writers\LaravelExcelWriter;
15 use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
16 use Laravel\Lumen\Application as LumenApplication;
17
18 /**
19 *
20 * LaravelExcel Excel ServiceProvider
21 *
22 * @category Laravel Excel
23 * @package maatwebsite/excel
24 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
25 * @author Maatwebsite <[email protected]>
26 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
27 */
28 class ExcelServiceProvider extends ServiceProvider {
29
30 /**
31 * Indicates if loading of the provider is deferred.
32 *
33 * @var bool
34 */
35 protected $defer = false;
36
37 /**
38 * Bootstrap the application events.
39 *
40 * @return void
41 */
42
43 public function boot()
44 {
45 if ($this->app instanceof LumenApplication) {
46 $this->app->configure('excel');
47 } else {
48 $this->publishes([
49 __DIR__ . '/../../config/excel.php' => config_path('excel.php'),
50 ]);
51 }
52
53 $this->mergeConfigFrom(
54 __DIR__ . '/../../config/excel.php', 'excel'
55 );
56
57 //Set the autosizing settings
58 $this->setAutoSizingSettings();
59 }
60
61 /**
62 * Register the service provider.
63 *
64 * @return void
65 */
66 public function register()
67 {
68 $this->bindClasses();
69 $this->bindCssParser();
70 $this->bindReaders();
71 $this->bindParsers();
72 $this->bindPHPExcelClass();
73 $this->bindWriters();
74 $this->bindExcel();
75 }
76
77 /**
78 * Bind PHPExcel classes
79 * @return void
80 */
81 protected function bindPHPExcelClass()
82 {
83 // Set object
84 $me = $this;
85
86 // Bind the PHPExcel class
87 $this->app->singleton('phpexcel', function () use ($me)
88 {
89 // Set locale
90 $me->setLocale();
91
92 // Set the caching settings
93 $me->setCacheSettings();
94
95 // Init phpExcel
96 $excel = new PHPExcel();
97 $excel->setDefaultProperties();
98 return $excel;
99 });
100 }
101
102 /**
103 * Bind the css parser
104 */
105 protected function bindCssParser()
106 {
107 // Bind css parser
108 $this->app->singleton('excel.parsers.css', function ()
109 {
110 return new CssParser(
111 new CssToInlineStyles()
112 );
113 });
114 }
115
116 /**
117 * Bind writers
118 * @return void
119 */
120 protected function bindReaders()
121 {
122 // Bind the laravel excel reader
123 $this->app->singleton('excel.reader', function ($app)
124 {
125 return new LaravelExcelReader(
126 $app['files'],
127 $app['excel.identifier'],
128 $app['Illuminate\Contracts\Bus\Dispatcher']
129 );
130 });
131
132 // Bind the html reader class
133 $this->app->singleton('excel.readers.html', function ($app)
134 {
135 return new Html(
136 $app['excel.parsers.css']
137 );
138 });
139 }
140
141 /**
142 * Bind writers
143 * @return void
144 */
145 protected function bindParsers()
146 {
147 // Bind the view parser
148 $this->app->singleton('excel.parsers.view', function ($app)
149 {
150 return new ViewParser(
151 $app['excel.readers.html']
152 );
153 });
154 }
155
156 /**
157 * Bind writers
158 * @return void
159 */
160 protected function bindWriters()
161 {
162 // Bind the excel writer
163 $this->app->singleton('excel.writer', function ($app)
164 {
165 return new LaravelExcelWriter(
166 $app->make(Response::class),
167 $app['files'],
168 $app['excel.identifier']
169 );
170 });
171 }
172
173 /**
174 * Bind Excel class
175 * @return void
176 */
177 protected function bindExcel()
178 {
179 // Bind the Excel class and inject its dependencies
180 $this->app->singleton('excel', function ($app)
181 {
182 $excel = new Excel(
183 $app['phpexcel'],
184 $app['excel.reader'],
185 $app['excel.writer'],
186 $app['excel.parsers.view']
187 );
188
189 $excel->registerFilters($app['config']->get('excel.filters', array()));
190
191 return $excel;
192 });
193
194 $this->app->alias('phpexcel', PHPExcel::class);
195 }
196
197 /**
198 * Bind other classes
199 * @return void
200 */
201 protected function bindClasses()
202 {
203 // Bind the format identifier
204 $this->app->singleton('excel.identifier', function ($app)
205 {
206 return new FormatIdentifier($app['files']);
207 });
208 }
209
210 /**
211 * Set cache settings
212 * @return Cache
213 */
214 public function setCacheSettings()
215 {
216 return new Cache();
217 }
218
219 /**
220 * Set locale
221 */
222 public function setLocale()
223 {
224 $locale = config('app.locale', 'en_us');
225 PHPExcel_Settings::setLocale($locale);
226 }
227
228 /**
229 * Set the autosizing settings
230 */
231 public function setAutoSizingSettings()
232 {
233 $method = config('excel.export.autosize-method', PHPExcel_Shared_Font::AUTOSIZE_METHOD_APPROX);
234 PHPExcel_Shared_Font::setAutoSizeMethod($method);
235 }
236
237 /**
238 * Get the services provided by the provider.
239 *
240 * @return array
241 */
242 public function provides()
243 {
244 return [
245 'excel',
246 'phpexcel',
247 'excel.reader',
248 'excel.readers.html',
249 'excel.parsers.view',
250 'excel.writer'
251 ];
252 }
253 }
1 <?php namespace Maatwebsite\Excel\Exceptions;
2
3 use PHPExcel_Exception;
4
5 /**
6 *
7 * LaravelExcel Exception
8 *
9 * @category Laravel Excel
10 * @version 1.0.0
11 * @package maatwebsite/excel
12 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
13 * @author Maatwebsite <[email protected]>
14 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
15 */
16 class LaravelExcelException extends PHPExcel_Exception {
17
18 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Facades;
2
3 use Illuminate\Support\Facades\Facade;
4
5 /**
6 *
7 * LaravelExcel Facade
8 *
9 * @category Laravel Excel
10 * @version 1.0.0
11 * @package maatwebsite/excel
12 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
13 * @author Maatwebsite <[email protected]>
14 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
15 */
16 class Excel extends Facade {
17
18 /**
19 * Return facade accessor
20 * @return string
21 */
22 protected static function getFacadeAccessor()
23 {
24 return 'excel';
25 }
26 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Files;
2
3 use Illuminate\Foundation\Application;
4 use Maatwebsite\Excel\Excel;
5 use Maatwebsite\Excel\Exceptions\LaravelExcelException;
6
7 abstract class ExcelFile extends File {
8
9 /**
10 * @var bool|string
11 */
12 protected $delimiter;
13
14 /**
15 * @var bool|string
16 */
17 protected $enclosure;
18
19 /**
20 * @var null
21 */
22 protected $encoding = null;
23
24 /**
25 * @param Application $app
26 * @param Excel $excel
27 */
28 public function __construct(Application $app, Excel $excel)
29 {
30 parent::__construct($app, $excel);
31 $this->file = $this->loadFile();
32 }
33
34 /**
35 * Get file
36 * @return string
37 */
38 abstract public function getFile();
39
40 /**
41 * Get delimiter
42 * @return string
43 */
44 protected function getDelimiter()
45 {
46 return $this->delimiter;
47 }
48
49 /**
50 * Get enclosure
51 * @return string
52 */
53 protected function getEnclosure()
54 {
55 return $this->enclosure;
56 }
57
58 /**
59 * Get filters
60 * @return array
61 */
62 public function getFilters()
63 {
64 return [];
65 }
66
67 /**
68 * Start importing
69 */
70 public function handleImport()
71 {
72 return $this->handle(
73 get_class($this)
74 );
75 }
76
77 /**
78 * Load the file
79 * @return \Maatwebsite\Excel\Readers\LaravelExcelReader
80 */
81 public function loadFile()
82 {
83 // Load filters
84 $this->loadFilters();
85
86 // Load base settings
87 $this->loadBaseSettings();
88
89 // Load the file
90 $file = $this->excel->load(
91 $this->getFile(),
92 null,
93 $this->encoding
94 );
95
96 return $file;
97 }
98
99 /**
100 * Load the filter
101 * @return void
102 */
103 protected function loadFilters()
104 {
105 // Register the filters
106 $this->excel->registerFilters(
107 $this->getFilters()
108 );
109
110 // Loop through the filters
111 foreach($this->getFilters() as $filter)
112 {
113 // Enable the filter
114 $this->excel->filter($filter);
115 }
116 }
117
118 /**
119 * Load base settings
120 */
121 protected function loadBaseSettings()
122 {
123 $this->loadCSVSettings();
124 }
125
126 /**
127 * Load CSV Settings
128 */
129 protected function loadCSVSettings()
130 {
131 // Get user provided delimiter
132 $delimiter = $this->getDelimiter();
133
134 // Set it when given
135 if($delimiter)
136 $this->excel->setDelimiter($delimiter);
137
138 // Get user provided enclosure
139 $enclosure = $this->getEnclosure();
140
141 // Set it when given
142 if($enclosure)
143 $this->excel->setEnclosure($enclosure);
144 }
145
146 /**
147 * Dynamically call methods
148 * @param string $method
149 * @param array $params
150 * @return mixed
151 */
152 public function __call($method, $params)
153 {
154 return call_user_func_array([$this->file, $method], $params);
155 }
156 }
1 <?php namespace Maatwebsite\Excel\Files;
2
3 interface ExportHandler {
4
5 /**
6 * Handle the export
7 * @param $file
8 * @return mixed
9 */
10 public function handle($file);
11
12 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Files;
2
3 use Illuminate\Foundation\Application;
4 use Maatwebsite\Excel\Excel;
5 use Maatwebsite\Excel\Exceptions\LaravelExcelException;
6
7 abstract class File {
8
9 /**
10 * @var Application
11 */
12 protected $app;
13
14 /**
15 * Excel instance
16 * @var Excel
17 */
18 protected $excel;
19
20 /**
21 * Loaded file
22 * @var \Maatwebsite\Excel\Readers\LaravelExcelReader
23 */
24 protected $file;
25
26 /**
27 * @param Application $app
28 * @param Excel $excel
29 */
30 public function __construct(Application $app, Excel $excel)
31 {
32 $this->app = $app;
33 $this->excel = $excel;
34 }
35
36 /**
37 * Handle the import/export of the file
38 * @param $type
39 * @throws LaravelExcelException
40 * @return mixed
41 */
42 public function handle($type)
43 {
44 // Get the handler
45 $handler = $this->getHandler($type);
46
47 // Call the handle method and inject the file
48 return $handler->handle($this);
49 }
50
51 /**
52 * Get handler
53 * @param $type
54 * @throws LaravelExcelException
55 * @return mixed
56 */
57 protected function getHandler($type)
58 {
59 return $this->app->make(
60 $this->getHandlerClassName($type)
61 );
62 }
63
64 /**
65 * Get the file instance
66 * @return mixed
67 */
68 public function getFileInstance()
69 {
70 return $this->file;
71 }
72
73 /**
74 * Get the handler class name
75 * @throws LaravelExcelException
76 * @return string
77 */
78 protected function getHandlerClassName($type)
79 {
80 // Translate the file into a FileHandler
81 $class = get_class($this);
82 $handler = substr_replace($class, $type . 'Handler', strrpos($class, $type));
83
84 // Check if the handler exists
85 if (!class_exists($handler))
86 throw new LaravelExcelException("$type handler [$handler] does not exist.");
87
88 return $handler;
89 }
90 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Files;
2
3 interface ImportHandler {
4
5 /**
6 * Handle the import
7 * @param $file
8 * @return mixed
9 */
10 public function handle($file);
11
12 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Files;
2
3 use Illuminate\Foundation\Application;
4 use Maatwebsite\Excel\Excel;
5
6 abstract class NewExcelFile extends File {
7
8 /**
9 * @param Application $app
10 * @param Excel $excel
11 */
12 public function __construct(Application $app, Excel $excel)
13 {
14 parent::__construct($app, $excel);
15 $this->file = $this->createNewFile();
16 }
17
18 /**
19 * Get file
20 * @return string
21 */
22 abstract public function getFilename();
23
24 /**
25 * Start importing
26 */
27 public function handleExport()
28 {
29 return $this->handle(
30 get_class($this)
31 );
32 }
33
34
35 /**
36 * Load the file
37 * @return \Maatwebsite\Excel\Readers\LaravelExcelReader
38 */
39 public function createNewFile()
40 {
41 // Load the file
42 $file = $this->excel->create(
43 $this->getFilename()
44 );
45
46 return $file;
47 }
48
49 /**
50 * Dynamically call methods
51 * @param string $method
52 * @param array $params
53 * @return mixed
54 */
55 public function __call($method, $params)
56 {
57 return call_user_func_array([$this->file, $method], $params);
58 }
59
60 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Filters;
2
3 use PHPExcel_Reader_IReadFilter;
4
5 class ChunkReadFilter implements PHPExcel_Reader_IReadFilter
6 {
7 /**
8 * Start row
9 * @var integer
10 */
11 private $_startRow = 0;
12
13 /**
14 * End row
15 * @var integer
16 */
17 private $_endRow = 0;
18
19 /**
20 * Set the list of rows that we want to read
21 * @param integer $startRow
22 * @param integer $chunkSize
23 * @return void
24 */
25 public function setRows($startRow, $chunkSize)
26 {
27 $this->_startRow = $startRow;
28 $this->_endRow = $startRow + $chunkSize;
29 }
30
31 /**
32 * Read the cell
33 * @param string $column
34 * @param integer $row
35 * @param string $worksheetName
36 * @return booleaan
37 */
38 public function readCell($column, $row, $worksheetName = '')
39 {
40 // Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow
41 if (($row == config('excel.import.startRow')) || ($row >= $this->_startRow && $row <= $this->_endRow)) {
42 return true;
43 }
44 return false;
45 }
46 }
1 <?php namespace Maatwebsite\Excel\Parsers;
2
3 use DOMDocument;
4 use Illuminate\Support\Facades\URL;
5 use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
6
7 /**
8 *
9 * LaravelExcel CSS Parser
10 *
11 * @category Laravel Excel
12 * @version 1.0.0
13 * @package maatwebsite/excel
14 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
15 * @author Maatwebsite <[email protected]>
16 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
17 */
18 class CssParser {
19
20 /**
21 * @var CssToInlineStyles
22 */
23 protected $cssInliner;
24
25 /**
26 * DOM xml
27 * @var \SimpleXMLElement
28 */
29 protected $xml;
30
31 /**
32 * Style sheet links
33 * @var array
34 */
35 protected $links = [];
36
37 /**
38 * Construct the css parser
39 * @param CssToInlineStyles $cssInliner
40 */
41 public function __construct(CssToInlineStyles $cssInliner)
42 {
43 $this->cssInliner = $cssInliner;
44 }
45
46 /**
47 * Transform the found css to inline styles
48 */
49 public function transformCssToInlineStyles($html)
50 {
51 $css = '';
52
53 // Loop through all stylesheets
54 foreach($this->links as $link)
55 {
56 $css .= file_get_contents($link);
57 }
58
59 return $this->cssInliner->convert($html, $css);
60 }
61
62 /**
63 * Find the stylesheets inside the view
64 * @param DOMDocument $dom
65 * @return CssParser
66 */
67 public function findStyleSheets(DOMDocument $dom)
68 {
69 // Import the dom
70 $this->importDom($dom);
71
72 // Get all stylesheet tags
73 $tags = $this->getStyleSheetTags();
74
75 foreach ($tags as $node)
76 {
77 $this->links[] = $this->getCleanStyleSheetLink($node);
78 }
79
80 // We don't need duplicate css files
81 $this->links = array_unique($this->links);
82
83 return $this;
84 }
85
86 /**
87 * Import the dom
88 * @return SimpleXMLElement
89 */
90 protected function importDom(DOMDocument $dom)
91 {
92 return $this->xml = simplexml_import_dom($dom);
93 }
94
95 /**
96 * Get all stylesheet tags
97 * @return array
98 */
99 protected function getStyleSheetTags()
100 {
101 return $this->xml->xpath('//link[@rel="stylesheet"]');
102 }
103
104 /**
105 * Get the clean link to the stylesheet
106 * @param string $node
107 * @return string
108 */
109 protected function getCleanStyleSheetLink($node)
110 {
111 // Get the link
112 $link = $node->attributes()->href;
113
114 return $link;
115 }
116
117 /**
118 * Get css from link
119 * @param string $link
120 * @return string|boolean
121 */
122 protected function getCssFromLink($link)
123 {
124 return file_get_contents($link);
125 }
126 }
1 <?php namespace Maatwebsite\Excel\Parsers;
2
3 use Maatwebsite\Excel\Readers\Html;
4 use Illuminate\Support\Facades\View;
5
6 /**
7 *
8 * LaravelExcel ViewParser
9 *
10 * @category Laravel Excel
11 * @version 1.0.0
12 * @package maatwebsite/excel
13 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
14 * @author Maatwebsite <[email protected]>
15 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
16 */
17 class ViewParser {
18
19 /**
20 * View file
21 * @var string
22 */
23 public $view;
24
25 /**
26 * Data array
27 * @var array
28 */
29 public $data = [];
30
31 /**
32 * View merge data
33 * @var array
34 */
35 public $mergeData = [];
36
37 /**
38 * Construct the view parser
39 * @param Html $reader
40 * @return \Maatwebsite\Excel\Parsers\ViewParser
41 */
42 public function __construct(Html $reader)
43 {
44 $this->reader = $reader;
45 }
46
47 /**
48 * Parse the view
49 * @param \Maatwebsite\Excel\Classes\LaravelExcelWorksheet $sheet
50 * @return \Maatwebsite\Excel\Classes\LaravelExcelWorksheet
51 */
52 public function parse($sheet)
53 {
54 $html = View::make($this->getView(), $this->getData(), $this->getMergeData())->render();
55
56 return $this->_loadHTML($sheet, $html);
57 }
58
59 /**
60 * Load the HTML
61 * @param \Maatwebsite\Excel\Classes\LaravelExcelWorksheet $sheet
62 * @param string $html
63 * @return \Maatwebsite\Excel\Classes\LaravelExcelWorksheet
64 */
65 protected function _loadHTML($sheet, $html)
66 {
67 return $this->reader->load($html, true, $sheet);
68 }
69
70 /**
71 * Get the view
72 * @return string
73 */
74 public function getView()
75 {
76 return $this->view;
77 }
78
79 /**
80 * Get data
81 * @return array
82 */
83 public function getData()
84 {
85 return $this->data;
86 }
87
88 /**
89 * Get merge data
90 * @return array
91 */
92 public function getMergeData()
93 {
94 return $this->mergeData;
95 }
96
97 /**
98 * Set the view
99 * @param bool|string $view
100 */
101 public function setView($view = false)
102 {
103 if ($view)
104 $this->view = $view;
105 }
106
107 /**
108 * Set the data
109 * @param array $data
110 */
111 public function setData($data = [])
112 {
113 if (!empty($data))
114 $this->data = array_merge($this->data, $data);
115 }
116
117 /**
118 * Set the merge data
119 * @param array $mergeData
120 */
121 public function setMergeData($mergeData = [])
122 {
123 if (!empty($mergeData))
124 $this->mergeData = array_merge($this->mergeData, $mergeData);
125 }
126 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Readers;
2
3 use Closure;
4 use Maatwebsite\Excel\Excel;
5 use Maatwebsite\Excel\Exceptions\LaravelExcelException;
6
7 /**
8 *
9 * LaravelExcel Batch Importer
10 *
11 * @category Laravel Excel
12 * @version 1.0.0
13 * @package maatwebsite/excel
14 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
15 * @author Maatwebsite <[email protected]>
16 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
17 */
18 class Batch {
19
20 /**
21 * Excel object
22 * @var Excel
23 */
24 protected $excel;
25
26 /**
27 * Batch files
28 * @var array
29 */
30 public $files = [];
31
32 /**
33 * Set allowed file extensions
34 * @var array
35 */
36 protected $allowedFileExtensions = [
37 'xls',
38 'xlsx',
39 'csv'
40 ];
41
42 /**
43 * Start the Batach
44 * @param Excel $excel
45 * @param array $files
46 * @param Closure $callback
47 * @return Excel
48 */
49 public function start(Excel $excel, $files, Closure $callback)
50 {
51 // Set excel object
52 $this->excel = $excel;
53
54 // Set files
55 $this->_setFiles($files);
56
57 // Do the callback
58 if ($callback instanceof Closure)
59 {
60 foreach ($this->getFiles() as $file)
61 {
62 // Load the file
63 $excel = $this->excel->load($file);
64
65 // Do a callback with the loaded file
66 call_user_func($callback, $excel, $file);
67 }
68 }
69
70 // Return our excel object
71 return $this->excel;
72 }
73
74 /**
75 * Get the files
76 * @return array
77 */
78 public function getFiles()
79 {
80 return $this->files;
81 }
82
83 /**
84 * Set the batch files
85 * @param array|string $files
86 * @throws LaravelExcelException
87 * @return void
88 */
89 protected function _setFiles($files)
90 {
91 // If the param is an array, these will be the files for the batch import
92 if (is_array($files))
93 {
94 $this->files = $this->_getFilesByArray($files);
95 }
96
97 // Get all the files inside a folder
98 elseif (is_string($files))
99 {
100 $this->files = $this->_getFilesByFolder($files);
101 }
102
103 // Check if files were found
104 if (empty($this->files))
105 throw new LaravelExcelException('[ERROR]: No files were found. Batch terminated.');
106 }
107
108 /**
109 * Set files by array
110 * @param array $array
111 * @return array
112 */
113 protected function _getFilesByArray($array)
114 {
115 $files = [];
116 // Make sure we have real paths
117 foreach ($array as $i => $file)
118 {
119 $files[$i] = realpath($file) ? $file : base_path($file);
120 }
121
122 return $files;
123 }
124
125 /**
126 * Get all files inside a folder
127 * @param string $folder
128 * @return array
129 */
130 protected function _getFilesByFolder($folder)
131 {
132 // Check if it's a real path
133 if (!realpath($folder))
134 $folder = base_path($folder);
135
136 // Find path names matching our pattern of excel extensions
137 $glob = glob($folder . '/*.{' . implode(',', $this->allowedFileExtensions) . '}', GLOB_BRACE);
138
139 // If no matches, return empty array
140 if ($glob === false) return [];
141
142 // Return files
143 return array_filter($glob, function ($file)
144 {
145 return filetype($file) == 'file';
146 });
147 }
148 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php
2
3 namespace Maatwebsite\Excel\Readers;
4
5 use Illuminate\Bus\Queueable;
6 use Illuminate\Contracts\Queue\ShouldQueue;
7 use Maatwebsite\Excel\Filters\ChunkReadFilter;
8 use SuperClosure\Serializer;
9
10 class ChunkedReadJob implements ShouldQueue
11 {
12 use Queueable;
13
14 /**
15 * @var int
16 */
17 private $startRow;
18
19 /**
20 * @var callable
21 */
22 private $callback;
23
24 /**
25 * @var
26 */
27 private $chunkSize;
28
29 /**
30 * @var
31 */
32 private $startIndex;
33
34 /**
35 * @var
36 */
37 private $file;
38
39 /**
40 * @var null
41 */
42 private $sheets;
43
44 /**
45 * @var bool
46 */
47 private $shouldQueue;
48
49 /**
50 * ChunkedReadJob constructor.
51 *
52 * @param $file
53 * @param null $sheets
54 * @param int $startRow
55 * @param $startIndex
56 * @param $chunkSize
57 * @param callable $callback
58 * @param bool $shouldQueue
59 */
60 public function __construct(
61 $file,
62 $sheets = null,
63 $startRow,
64 $startIndex,
65 $chunkSize,
66 callable $callback,
67 $shouldQueue = true
68 ) {
69 $this->startRow = $startRow;
70 $this->chunkSize = $chunkSize;
71 $this->startIndex = $startIndex;
72 $this->file = $file;
73
74 $this->callback = $shouldQueue ? (new Serializer)->serialize($callback) : $callback;
75 $this->sheets = $sheets;
76 $this->shouldQueue = $shouldQueue;
77 }
78
79 /***
80 * Handle the read job
81 */
82 public function handle()
83 {
84 $reader = app('excel.reader');
85 $reader->injectExcel(app('phpexcel'));
86 $reader->_init($this->file);
87
88 $filter = new ChunkReadFilter();
89 $reader->reader->setLoadSheetsOnly($this->sheets);
90 $reader->reader->setReadFilter($filter);
91 $reader->reader->setReadDataOnly(true);
92
93 // Set the rows for the chunking
94 $filter->setRows($this->startRow, $this->chunkSize);
95
96 // Load file with chunk filter enabled
97 $reader->excel = $reader->reader->load($this->file);
98
99 // Slice the results
100 $results = $reader->get()->slice($this->startIndex, $this->chunkSize);
101
102 $callback = $this->shouldQueue ? (new Serializer)->unserialize($this->callback) : $this->callback;
103
104 // Do a callback
105 if (is_callable($callback)) {
106 $break = call_user_func($callback, $results);
107 }
108
109 $reader->_reset();
110 unset($reader, $results);
111
112 if ($break) {
113 return true;
114 }
115 }
116 }
1 <?php namespace Maatwebsite\Excel\Readers;
2
3 use Closure;
4 use PHPExcel;
5 use Maatwebsite\Excel\Excel;
6 use Maatwebsite\Excel\Collections\SheetCollection;
7 use Maatwebsite\Excel\Exceptions\LaravelExcelException;
8
9 /**
10 *
11 * LaravelExcel ConfigReader
12 *
13 * @category Laravel Excel
14 * @version 1.0.0
15 * @package maatwebsite/excel
16 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
17 * @author Maatwebsite <[email protected]>
18 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
19 */
20 class ConfigReader {
21
22 /**
23 * Excel object
24 * @var PHPExcel
25 */
26 public $excel;
27
28 /**
29 * The sheet
30 * @var LaravelExcelWorksheet
31 */
32 public $sheet;
33
34 /**
35 * The sheetname
36 * @var string
37 */
38 public $sheetName;
39
40 /**
41 * Collection of sheets (through the config reader)
42 * @var SheetCollection
43 */
44 public $sheetCollection;
45
46 /**
47 * Constructor
48 * @param PHPExcel $excel
49 * @param string $config
50 * @param callback $callback
51 */
52 public function __construct(PHPExcel $excel, $config = 'excel.import', $callback = null)
53 {
54 // Set excel object
55 $this->excel = $excel;
56
57 // config name
58 $this->configName = $config;
59
60 // start
61 $this->start($callback);
62 }
63
64 /**
65 * Start the import
66 * @param bool|callable $callback $callback
67 * @throws \PHPExcel_Exception
68 * @return void
69 */
70 public function start($callback = false)
71 {
72 // Init a new sheet collection
73 $this->sheetCollection = new SheetCollection();
74
75 // Get the sheet names
76 if ($sheets = $this->excel->getSheetNames())
77 {
78 // Loop through the sheets
79 foreach ($sheets as $index => $name)
80 {
81 // Set sheet name
82 $this->sheetName = $name;
83
84 // Set sheet
85 $this->sheet = $this->excel->setActiveSheetIndex($index);
86
87 // Do the callback
88 if ($callback instanceof Closure)
89 {
90 call_user_func($callback, $this);
91 }
92 // If no callback, put it inside the sheet collection
93 else
94 {
95 $this->sheetCollection->push(clone $this);
96 }
97 }
98 }
99 }
100
101 /**
102 * Get the sheet collection
103 * @return SheetCollection
104 */
105 public function getSheetCollection()
106 {
107 return $this->sheetCollection;
108 }
109
110 /**
111 * Get value by index
112 * @param string $field
113 * @return string|null
114 */
115 protected function valueByIndex($field)
116 {
117 // Convert field name
118 $field = snake_case($field);
119
120 // Get coordinate
121 if ($coordinate = $this->getCoordinateByKey($field))
122 {
123 // return cell value by coordinate
124 return $this->getCellValueByCoordinate($coordinate);
125 }
126
127 return null;
128 }
129
130 /**
131 * Return cell value
132 * @param string $coordinate
133 * @return string|null
134 */
135 protected function getCellValueByCoordinate($coordinate)
136 {
137 if ($this->sheet)
138 {
139 if (str_contains($coordinate, ':'))
140 {
141 // We want to get a range of cells
142 $values = $this->sheet->rangeToArray($coordinate);
143
144 return $values;
145 }
146 else
147 {
148 // We want 1 specific cell
149 return $this->sheet->getCell($coordinate)->getValue();
150 }
151 }
152
153 return null;
154 }
155
156 /**
157 * Get the coordinates from the config file
158 * @param string $field
159 * @return string|boolean
160 */
161 protected function getCoordinateByKey($field)
162 {
163 return config($this->configName . '.' . $this->sheetName . '.' . $field, false);
164 }
165
166 /**
167 * Dynamically get a value by config
168 * @param string $field
169 * @return string
170 */
171 public function __get($field)
172 {
173 return $this->valueByIndex($field);
174 }
175 }
...\ No newline at end of file ...\ No newline at end of file
1 <?php namespace Maatwebsite\Excel\Writers;
2
3 use Maatwebsite\Excel\Classes\LaravelExcelWorksheet;
4
5 /**
6 *
7 * LaravelExcel Excel writer
8 *
9 * @category Laravel Excel
10 * @version 1.0.0
11 * @package maatwebsite/excel
12 * @copyright Copyright (c) 2013 - 2014 Maatwebsite (http://www.maatwebsite.nl)
13 * @author Maatwebsite <[email protected]>
14 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
15 */
16 class CellWriter {
17
18 /**
19 * Current $sheet
20 * @var LaravelExcelWorksheet
21 */
22 public $sheet;
23
24 /**
25 * Selected cells
26 * @var array
27 */
28 public $cells;
29
30 /**
31 * Constructor
32 * @param array $cells
33 * @param LaravelExcelWorksheet $sheet
34 */
35 public function __construct($cells, LaravelExcelWorksheet $sheet)
36 {
37 $this->cells = $cells;
38 $this->sheet = $sheet;
39 }
40
41 /**
42 * Set cell value
43 * @param [type] $value
44 * @return CellWriter
45 */
46 public function setValue($value)
47 {
48 // Only set cell value for single cells
49 if (!str_contains($this->cells, ':'))
50 {
51 $this->sheet->setCellValue($this->cells, $value);
52 }
53
54 return $this;
55 }
56
57 /**
58 * Set the background
59 * @param string $color
60 * @param string $type
61 * @param string $colorType
62 * @return CellWriter
63 */
64 public function setBackground($color, $type = 'solid', $colorType = 'rgb')
65 {
66 return $this->setColorStyle('fill', $color, $type, $colorType);
67 }
68
69 /**
70 * Set the font color
71 * @param string $color
72 * @param string $colorType
73 * @return CellWriter
74 */
75 public function setFontColor($color, $colorType = 'rgb')
76 {
77 return $this->setColorStyle('font', $color, false, $colorType);
78 }
79
80 /**
81 * Set the font
82 * @param $styles
83 * @return CellWriter
84 */
85 public function setFont($styles)
86 {
87 return $this->setStyle('font', $styles);
88 }
89
90 /**
91 * Set font family
92 * @param string $family
93 * @return CellWriter
94 */
95 public function setFontFamily($family)
96 {
97 return $this->setStyle('font', [
98 'name' => $family
99 ]);
100 }
101
102 /**
103 * Set font size
104 * @param string $size
105 * @return CellWriter
106 */
107 public function setFontSize($size)
108 {
109 return $this->setStyle('font', [
110 'size' => $size
111 ]);
112 }
113
114 /**
115 * Set font weight
116 * @param boolean|string $bold
117 * @return CellWriter
118 */
119 public function setFontWeight($bold = true)
120 {
121 return $this->setStyle('font', [
122 'bold' => ($bold === 'bold' || $bold === true)
123 ]);
124 }
125
126 /**
127 * Set border
128 * @param string $top
129 * @param bool|string $right
130 * @param bool|string $bottom
131 * @param bool|string $left
132 * @return CellWriter
133 */
134 public function setBorder($top = 'none', $right = 'none', $bottom = 'none', $left = 'none')
135 {
136 // Set the border styles
137 $styles = is_array($top) ? $top : [
138 'top' => [
139 'style' => $top
140 ],
141 'left' => [
142 'style' => $left,
143 ],
144 'right' => [
145 'style' => $right,
146 ],
147 'bottom' => [
148 'style' => $bottom,
149 ]
150 ];
151
152 return $this->setStyle('borders', $styles);
153 }
154
155 /**
156 * Set the text rotation
157 * @param integer $alignment
158 * @return CellWriter
159 */
160 public function setTextRotation($degrees)
161 {
162 $style = $this->getCellStyle()->getAlignment()->setTextRotation($degrees);
163 return $this;
164 }
165
166 /**
167 * Set the alignment
168 * @param string $alignment
169 * @return CellWriter
170 */
171 public function setAlignment($alignment)
172 {
173 return $this->setStyle('alignment', [
174 'horizontal' => $alignment
175 ]);
176 }
177
178 /**
179 * Set vertical alignment
180 * @param string $alignment
181 * @return CellWriter
182 */
183 public function setValignment($alignment)
184 {
185 return $this->setStyle('alignment', [
186 'vertical' => $alignment
187 ]);
188 }
189
190 /**
191 * Set the text indent
192 * @param integer $indent
193 * @return CellWriter
194 */
195 public function setTextIndent($indent)
196 {
197 $style = $this->getCellStyle()->getAlignment()->setIndent((int)$indent);
198 return $this;
199 }
200
201 /**
202 * Set the color style
203 * @param $styleType
204 * @param string $color
205 * @param boolean $type
206 * @param string $colorType
207 * @return CellWriter
208 */
209 protected function setColorStyle($styleType, $color, $type = false, $colorType = 'rgb')
210 {
211 // Set the styles
212 $styles = is_array($color) ? $color : [
213 'type' => $type,
214 'color' => [$colorType => str_replace('#', '', $color)]
215 ];
216
217 return $this->setStyle($styleType, $styles);
218 }
219
220 /**
221 * Set style
222 * @param $styleType
223 * @param string $styles
224 * @return CellWriter
225 */
226 protected function setStyle($styleType, $styles)
227 {
228 // Get the cell style
229 $style = $this->getCellStyle();
230
231 // Apply style from array
232 $style->applyFromArray([
233 $styleType => $styles
234 ]);
235
236 return $this;
237 }
238
239 /**
240 * Get the cell style
241 * @return \PHPExcel_Style
242 */
243 protected function getCellStyle()
244 {
245 return $this->sheet->getStyle($this->cells);
246 }
247 }
1 /Build export-ignore
2 /unitTests export-ignore
3 README.md export-ignore
1 build/PHPExcel.phar
2 unitTests/codeCoverage
3 analysis
4
5 ## IDE support
6 *.buildpath
7 *.project
8 /.settings
9 /.idea
1 language: php
2
3 php:
4 - 5.4
5 - 5.5
6 - 5.6
7 - 7.0
8 - hhvm
9
10 matrix:
11 allow_failures:
12 - php: hhvm
13
14 before_script:
15 ## Packages
16 - sudo apt-get -qq update > /dev/null
17 ## Composer
18 - composer self-update
19 - composer install --prefer-source --dev
20 - phpenv global "$TRAVIS_PHP_VERSION"
21
22 script:
23 ## PHP_CodeSniffer
24 - ./vendor/bin/phpcs --report-width=200 --report-summary --report-full Classes/ unitTests/ --standard=PSR2 -n
25 ## PHPUnit
26 - phpunit -c ./unitTests/
27
28 notifications:
29 email: false
1 <?php
2
3 PHPExcel_Autoloader::register();
4 // As we always try to run the autoloader before anything else, we can use it to do a few
5 // simple checks and initialisations
6 //PHPExcel_Shared_ZipStreamWrapper::register();
7 // check mbstring.func_overload
8 if (ini_get('mbstring.func_overload') & 2) {
9 throw new PHPExcel_Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
10 }
11 PHPExcel_Shared_String::buildCharacterSets();
12
13 /**
14 * PHPExcel
15 *
16 * Copyright (c) 2006 - 2015 PHPExcel
17 *
18 * This library is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
22 *
23 * This library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with this library; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 *
32 * @category PHPExcel
33 * @package PHPExcel
34 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
35 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
36 * @version ##VERSION##, ##DATE##
37 */
38 class PHPExcel_Autoloader
39 {
40 /**
41 * Register the Autoloader with SPL
42 *
43 */
44 public static function register()
45 {
46 if (function_exists('__autoload')) {
47 // Register any existing autoloader function with SPL, so we don't get any clashes
48 spl_autoload_register('__autoload');
49 }
50 // Register ourselves with SPL
51 if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
52 return spl_autoload_register(array('PHPExcel_Autoloader', 'load'), true, true);
53 } else {
54 return spl_autoload_register(array('PHPExcel_Autoloader', 'load'));
55 }
56 }
57
58 /**
59 * Autoload a class identified by name
60 *
61 * @param string $pClassName Name of the object to load
62 */
63 public static function load($pClassName)
64 {
65 if ((class_exists($pClassName, false)) || (strpos($pClassName, 'PHPExcel') !== 0)) {
66 // Either already loaded, or not a PHPExcel class request
67 return false;
68 }
69
70 $pClassFilePath = PHPEXCEL_ROOT .
71 str_replace('_', DIRECTORY_SEPARATOR, $pClassName) .
72 '.php';
73
74 if ((file_exists($pClassFilePath) === false) || (is_readable($pClassFilePath) === false)) {
75 // Can't load
76 return false;
77 }
78
79 require($pClassFilePath);
80 }
81 }
1 <?php
2
3 /**
4 * PHPExcel_CachedObjectStorage_APC
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * @category PHPExcel
23 * @package PHPExcel_CachedObjectStorage
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
27 */
28 class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
29 {
30 /**
31 * Prefix used to uniquely identify cache data for this worksheet
32 *
33 * @access private
34 * @var string
35 */
36 private $cachePrefix = null;
37
38 /**
39 * Cache timeout
40 *
41 * @access private
42 * @var integer
43 */
44 private $cacheTime = 600;
45
46 /**
47 * Store cell data in cache for the current cell object if it's "dirty",
48 * and the 'nullify' the current cell object
49 *
50 * @access private
51 * @return void
52 * @throws PHPExcel_Exception
53 */
54 protected function storeData()
55 {
56 if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
57 $this->currentObject->detach();
58
59 if (!apc_store(
60 $this->cachePrefix . $this->currentObjectID . '.cache',
61 serialize($this->currentObject),
62 $this->cacheTime
63 )) {
64 $this->__destruct();
65 throw new PHPExcel_Exception('Failed to store cell ' . $this->currentObjectID . ' in APC');
66 }
67 $this->currentCellIsDirty = false;
68 }
69 $this->currentObjectID = $this->currentObject = null;
70 }
71
72 /**
73 * Add or Update a cell in cache identified by coordinate address
74 *
75 * @access public
76 * @param string $pCoord Coordinate address of the cell to update
77 * @param PHPExcel_Cell $cell Cell to update
78 * @return PHPExcel_Cell
79 * @throws PHPExcel_Exception
80 */
81 public function addCacheData($pCoord, PHPExcel_Cell $cell)
82 {
83 if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
84 $this->storeData();
85 }
86 $this->cellCache[$pCoord] = true;
87
88 $this->currentObjectID = $pCoord;
89 $this->currentObject = $cell;
90 $this->currentCellIsDirty = true;
91
92 return $cell;
93 }
94
95 /**
96 * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
97 *
98 * @access public
99 * @param string $pCoord Coordinate address of the cell to check
100 * @throws PHPExcel_Exception
101 * @return boolean
102 */
103 public function isDataSet($pCoord)
104 {
105 // Check if the requested entry is the current object, or exists in the cache
106 if (parent::isDataSet($pCoord)) {
107 if ($this->currentObjectID == $pCoord) {
108 return true;
109 }
110 // Check if the requested entry still exists in apc
111 $success = apc_fetch($this->cachePrefix.$pCoord.'.cache');
112 if ($success === false) {
113 // Entry no longer exists in APC, so clear it from the cache array
114 parent::deleteCacheData($pCoord);
115 throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in APC cache');
116 }
117 return true;
118 }
119 return false;
120 }
121
122 /**
123 * Get cell at a specific coordinate
124 *
125 * @access public
126 * @param string $pCoord Coordinate of the cell
127 * @throws PHPExcel_Exception
128 * @return PHPExcel_Cell Cell that was found, or null if not found
129 */
130 public function getCacheData($pCoord)
131 {
132 if ($pCoord === $this->currentObjectID) {
133 return $this->currentObject;
134 }
135 $this->storeData();
136
137 // Check if the entry that has been requested actually exists
138 if (parent::isDataSet($pCoord)) {
139 $obj = apc_fetch($this->cachePrefix . $pCoord . '.cache');
140 if ($obj === false) {
141 // Entry no longer exists in APC, so clear it from the cache array
142 parent::deleteCacheData($pCoord);
143 throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in APC cache');
144 }
145 } else {
146 // Return null if requested entry doesn't exist in cache
147 return null;
148 }
149
150 // Set current entry to the requested entry
151 $this->currentObjectID = $pCoord;
152 $this->currentObject = unserialize($obj);
153 // Re-attach this as the cell's parent
154 $this->currentObject->attach($this);
155
156 // Return requested entry
157 return $this->currentObject;
158 }
159
160 /**
161 * Get a list of all cell addresses currently held in cache
162 *
163 * @return string[]
164 */
165 public function getCellList()
166 {
167 if ($this->currentObjectID !== null) {
168 $this->storeData();
169 }
170
171 return parent::getCellList();
172 }
173
174 /**
175 * Delete a cell in cache identified by coordinate address
176 *
177 * @access public
178 * @param string $pCoord Coordinate address of the cell to delete
179 * @throws PHPExcel_Exception
180 */
181 public function deleteCacheData($pCoord)
182 {
183 // Delete the entry from APC
184 apc_delete($this->cachePrefix.$pCoord.'.cache');
185
186 // Delete the entry from our cell address array
187 parent::deleteCacheData($pCoord);
188 }
189
190 /**
191 * Clone the cell collection
192 *
193 * @access public
194 * @param PHPExcel_Worksheet $parent The new worksheet
195 * @throws PHPExcel_Exception
196 * @return void
197 */
198 public function copyCellCollection(PHPExcel_Worksheet $parent)
199 {
200 parent::copyCellCollection($parent);
201 // Get a new id for the new file name
202 $baseUnique = $this->getUniqueID();
203 $newCachePrefix = substr(md5($baseUnique), 0, 8) . '.';
204 $cacheList = $this->getCellList();
205 foreach ($cacheList as $cellID) {
206 if ($cellID != $this->currentObjectID) {
207 $obj = apc_fetch($this->cachePrefix . $cellID . '.cache');
208 if ($obj === false) {
209 // Entry no longer exists in APC, so clear it from the cache array
210 parent::deleteCacheData($cellID);
211 throw new PHPExcel_Exception('Cell entry ' . $cellID . ' no longer exists in APC');
212 }
213 if (!apc_store($newCachePrefix . $cellID . '.cache', $obj, $this->cacheTime)) {
214 $this->__destruct();
215 throw new PHPExcel_Exception('Failed to store cell ' . $cellID . ' in APC');
216 }
217 }
218 }
219 $this->cachePrefix = $newCachePrefix;
220 }
221
222 /**
223 * Clear the cell collection and disconnect from our parent
224 *
225 * @return void
226 */
227 public function unsetWorksheetCells()
228 {
229 if ($this->currentObject !== null) {
230 $this->currentObject->detach();
231 $this->currentObject = $this->currentObjectID = null;
232 }
233
234 // Flush the APC cache
235 $this->__destruct();
236
237 $this->cellCache = array();
238
239 // detach ourself from the worksheet, so that it can then delete this object successfully
240 $this->parent = null;
241 }
242
243 /**
244 * Initialise this new cell collection
245 *
246 * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
247 * @param array of mixed $arguments Additional initialisation arguments
248 */
249 public function __construct(PHPExcel_Worksheet $parent, $arguments)
250 {
251 $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
252
253 if ($this->cachePrefix === null) {
254 $baseUnique = $this->getUniqueID();
255 $this->cachePrefix = substr(md5($baseUnique), 0, 8) . '.';
256 $this->cacheTime = $cacheTime;
257
258 parent::__construct($parent);
259 }
260 }
261
262 /**
263 * Destroy this cell collection
264 */
265 public function __destruct()
266 {
267 $cacheList = $this->getCellList();
268 foreach ($cacheList as $cellID) {
269 apc_delete($this->cachePrefix . $cellID . '.cache');
270 }
271 }
272
273 /**
274 * Identify whether the caching method is currently available
275 * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
276 *
277 * @return boolean
278 */
279 public static function cacheMethodIsAvailable()
280 {
281 if (!function_exists('apc_store')) {
282 return false;
283 }
284 if (apc_sma_info() === false) {
285 return false;
286 }
287
288 return true;
289 }
290 }
1 <?php
2
3 /**
4 * PHPExcel_CachedObjectStorage_DiscISAM
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * @category PHPExcel
23 * @package PHPExcel_CachedObjectStorage
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
27 */
28 class PHPExcel_CachedObjectStorage_DiscISAM extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
29 {
30 /**
31 * Name of the file for this cache
32 *
33 * @var string
34 */
35 private $fileName = null;
36
37 /**
38 * File handle for this cache file
39 *
40 * @var resource
41 */
42 private $fileHandle = null;
43
44 /**
45 * Directory/Folder where the cache file is located
46 *
47 * @var string
48 */
49 private $cacheDirectory = null;
50
51 /**
52 * Store cell data in cache for the current cell object if it's "dirty",
53 * and the 'nullify' the current cell object
54 *
55 * @return void
56 * @throws PHPExcel_Exception
57 */
58 protected function storeData()
59 {
60 if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
61 $this->currentObject->detach();
62
63 fseek($this->fileHandle, 0, SEEK_END);
64
65 $this->cellCache[$this->currentObjectID] = array(
66 'ptr' => ftell($this->fileHandle),
67 'sz' => fwrite($this->fileHandle, serialize($this->currentObject))
68 );
69 $this->currentCellIsDirty = false;
70 }
71 $this->currentObjectID = $this->currentObject = null;
72 }
73
74 /**
75 * Add or Update a cell in cache identified by coordinate address
76 *
77 * @param string $pCoord Coordinate address of the cell to update
78 * @param PHPExcel_Cell $cell Cell to update
79 * @return PHPExcel_Cell
80 * @throws PHPExcel_Exception
81 */
82 public function addCacheData($pCoord, PHPExcel_Cell $cell)
83 {
84 if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
85 $this->storeData();
86 }
87
88 $this->currentObjectID = $pCoord;
89 $this->currentObject = $cell;
90 $this->currentCellIsDirty = true;
91
92 return $cell;
93 }
94
95 /**
96 * Get cell at a specific coordinate
97 *
98 * @param string $pCoord Coordinate of the cell
99 * @throws PHPExcel_Exception
100 * @return PHPExcel_Cell Cell that was found, or null if not found
101 */
102 public function getCacheData($pCoord)
103 {
104 if ($pCoord === $this->currentObjectID) {
105 return $this->currentObject;
106 }
107 $this->storeData();
108
109 // Check if the entry that has been requested actually exists
110 if (!isset($this->cellCache[$pCoord])) {
111 // Return null if requested entry doesn't exist in cache
112 return null;
113 }
114
115 // Set current entry to the requested entry
116 $this->currentObjectID = $pCoord;
117 fseek($this->fileHandle, $this->cellCache[$pCoord]['ptr']);
118 $this->currentObject = unserialize(fread($this->fileHandle, $this->cellCache[$pCoord]['sz']));
119 // Re-attach this as the cell's parent
120 $this->currentObject->attach($this);
121
122 // Return requested entry
123 return $this->currentObject;
124 }
125
126 /**
127 * Get a list of all cell addresses currently held in cache
128 *
129 * @return string[]
130 */
131 public function getCellList()
132 {
133 if ($this->currentObjectID !== null) {
134 $this->storeData();
135 }
136
137 return parent::getCellList();
138 }
139
140 /**
141 * Clone the cell collection
142 *
143 * @param PHPExcel_Worksheet $parent The new worksheet
144 */
145 public function copyCellCollection(PHPExcel_Worksheet $parent)
146 {
147 parent::copyCellCollection($parent);
148 // Get a new id for the new file name
149 $baseUnique = $this->getUniqueID();
150 $newFileName = $this->cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache';
151 // Copy the existing cell cache file
152 copy($this->fileName, $newFileName);
153 $this->fileName = $newFileName;
154 // Open the copied cell cache file
155 $this->fileHandle = fopen($this->fileName, 'a+');
156 }
157
158 /**
159 * Clear the cell collection and disconnect from our parent
160 *
161 */
162 public function unsetWorksheetCells()
163 {
164 if (!is_null($this->currentObject)) {
165 $this->currentObject->detach();
166 $this->currentObject = $this->currentObjectID = null;
167 }
168 $this->cellCache = array();
169
170 // detach ourself from the worksheet, so that it can then delete this object successfully
171 $this->parent = null;
172
173 // Close down the temporary cache file
174 $this->__destruct();
175 }
176
177 /**
178 * Initialise this new cell collection
179 *
180 * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
181 * @param array of mixed $arguments Additional initialisation arguments
182 */
183 public function __construct(PHPExcel_Worksheet $parent, $arguments)
184 {
185 $this->cacheDirectory = ((isset($arguments['dir'])) && ($arguments['dir'] !== null))
186 ? $arguments['dir']
187 : PHPExcel_Shared_File::sys_get_temp_dir();
188
189 parent::__construct($parent);
190 if (is_null($this->fileHandle)) {
191 $baseUnique = $this->getUniqueID();
192 $this->fileName = $this->cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache';
193 $this->fileHandle = fopen($this->fileName, 'a+');
194 }
195 }
196
197 /**
198 * Destroy this cell collection
199 */
200 public function __destruct()
201 {
202 if (!is_null($this->fileHandle)) {
203 fclose($this->fileHandle);
204 unlink($this->fileName);
205 }
206 $this->fileHandle = null;
207 }
208 }
1 <?php
2
3 /**
4 * PHPExcel_CachedObjectStorage_ICache
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * @category PHPExcel
23 * @package PHPExcel_CachedObjectStorage
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
27 */
28 interface PHPExcel_CachedObjectStorage_ICache
29 {
30 /**
31 * Add or Update a cell in cache identified by coordinate address
32 *
33 * @param string $pCoord Coordinate address of the cell to update
34 * @param PHPExcel_Cell $cell Cell to update
35 * @return PHPExcel_Cell
36 * @throws PHPExcel_Exception
37 */
38 public function addCacheData($pCoord, PHPExcel_Cell $cell);
39
40 /**
41 * Add or Update a cell in cache
42 *
43 * @param PHPExcel_Cell $cell Cell to update
44 * @return PHPExcel_Cell
45 * @throws PHPExcel_Exception
46 */
47 public function updateCacheData(PHPExcel_Cell $cell);
48
49 /**
50 * Fetch a cell from cache identified by coordinate address
51 *
52 * @param string $pCoord Coordinate address of the cell to retrieve
53 * @return PHPExcel_Cell Cell that was found, or null if not found
54 * @throws PHPExcel_Exception
55 */
56 public function getCacheData($pCoord);
57
58 /**
59 * Delete a cell in cache identified by coordinate address
60 *
61 * @param string $pCoord Coordinate address of the cell to delete
62 * @throws PHPExcel_Exception
63 */
64 public function deleteCacheData($pCoord);
65
66 /**
67 * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
68 *
69 * @param string $pCoord Coordinate address of the cell to check
70 * @return boolean
71 */
72 public function isDataSet($pCoord);
73
74 /**
75 * Get a list of all cell addresses currently held in cache
76 *
77 * @return string[]
78 */
79 public function getCellList();
80
81 /**
82 * Get the list of all cell addresses currently held in cache sorted by column and row
83 *
84 * @return string[]
85 */
86 public function getSortedCellList();
87
88 /**
89 * Clone the cell collection
90 *
91 * @param PHPExcel_Worksheet $parent The new worksheet
92 * @return void
93 */
94 public function copyCellCollection(PHPExcel_Worksheet $parent);
95
96 /**
97 * Identify whether the caching method is currently available
98 * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
99 *
100 * @return boolean
101 */
102 public static function cacheMethodIsAvailable();
103 }
1 <?php
2
3 /**
4 * PHPExcel_CachedObjectStorage_Igbinary
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * @category PHPExcel
23 * @package PHPExcel_CachedObjectStorage
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
27 */
28 class PHPExcel_CachedObjectStorage_Igbinary extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
29 {
30 /**
31 * Store cell data in cache for the current cell object if it's "dirty",
32 * and the 'nullify' the current cell object
33 *
34 * @return void
35 * @throws PHPExcel_Exception
36 */
37 protected function storeData()
38 {
39 if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
40 $this->currentObject->detach();
41
42 $this->cellCache[$this->currentObjectID] = igbinary_serialize($this->currentObject);
43 $this->currentCellIsDirty = false;
44 }
45 $this->currentObjectID = $this->currentObject = null;
46 } // function _storeData()
47
48
49 /**
50 * Add or Update a cell in cache identified by coordinate address
51 *
52 * @param string $pCoord Coordinate address of the cell to update
53 * @param PHPExcel_Cell $cell Cell to update
54 * @return PHPExcel_Cell
55 * @throws PHPExcel_Exception
56 */
57 public function addCacheData($pCoord, PHPExcel_Cell $cell)
58 {
59 if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
60 $this->storeData();
61 }
62
63 $this->currentObjectID = $pCoord;
64 $this->currentObject = $cell;
65 $this->currentCellIsDirty = true;
66
67 return $cell;
68 } // function addCacheData()
69
70
71 /**
72 * Get cell at a specific coordinate
73 *
74 * @param string $pCoord Coordinate of the cell
75 * @throws PHPExcel_Exception
76 * @return PHPExcel_Cell Cell that was found, or null if not found
77 */
78 public function getCacheData($pCoord)
79 {
80 if ($pCoord === $this->currentObjectID) {
81 return $this->currentObject;
82 }
83 $this->storeData();
84
85 // Check if the entry that has been requested actually exists
86 if (!isset($this->cellCache[$pCoord])) {
87 // Return null if requested entry doesn't exist in cache
88 return null;
89 }
90
91 // Set current entry to the requested entry
92 $this->currentObjectID = $pCoord;
93 $this->currentObject = igbinary_unserialize($this->cellCache[$pCoord]);
94 // Re-attach this as the cell's parent
95 $this->currentObject->attach($this);
96
97 // Return requested entry
98 return $this->currentObject;
99 } // function getCacheData()
100
101
102 /**
103 * Get a list of all cell addresses currently held in cache
104 *
105 * @return string[]
106 */
107 public function getCellList()
108 {
109 if ($this->currentObjectID !== null) {
110 $this->storeData();
111 }
112
113 return parent::getCellList();
114 }
115
116
117 /**
118 * Clear the cell collection and disconnect from our parent
119 *
120 * @return void
121 */
122 public function unsetWorksheetCells()
123 {
124 if (!is_null($this->currentObject)) {
125 $this->currentObject->detach();
126 $this->currentObject = $this->currentObjectID = null;
127 }
128 $this->cellCache = array();
129
130 // detach ourself from the worksheet, so that it can then delete this object successfully
131 $this->parent = null;
132 } // function unsetWorksheetCells()
133
134
135 /**
136 * Identify whether the caching method is currently available
137 * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
138 *
139 * @return boolean
140 */
141 public static function cacheMethodIsAvailable()
142 {
143 if (!function_exists('igbinary_serialize')) {
144 return false;
145 }
146
147 return true;
148 }
149 }
1 <?php
2
3 /**
4 * PHPExcel_CachedObjectStorage_Memory
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * @category PHPExcel
23 * @package PHPExcel_CachedObjectStorage
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
27 */
28 class PHPExcel_CachedObjectStorage_Memory extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
29 {
30 /**
31 * Dummy method callable from CacheBase, but unused by Memory cache
32 *
33 * @return void
34 */
35 protected function storeData()
36 {
37 }
38
39 /**
40 * Add or Update a cell in cache identified by coordinate address
41 *
42 * @param string $pCoord Coordinate address of the cell to update
43 * @param PHPExcel_Cell $cell Cell to update
44 * @return PHPExcel_Cell
45 * @throws PHPExcel_Exception
46 */
47 public function addCacheData($pCoord, PHPExcel_Cell $cell)
48 {
49 $this->cellCache[$pCoord] = $cell;
50
51 // Set current entry to the new/updated entry
52 $this->currentObjectID = $pCoord;
53
54 return $cell;
55 }
56
57
58 /**
59 * Get cell at a specific coordinate
60 *
61 * @param string $pCoord Coordinate of the cell
62 * @throws PHPExcel_Exception
63 * @return PHPExcel_Cell Cell that was found, or null if not found
64 */
65 public function getCacheData($pCoord)
66 {
67 // Check if the entry that has been requested actually exists
68 if (!isset($this->cellCache[$pCoord])) {
69 $this->currentObjectID = null;
70 // Return null if requested entry doesn't exist in cache
71 return null;
72 }
73
74 // Set current entry to the requested entry
75 $this->currentObjectID = $pCoord;
76
77 // Return requested entry
78 return $this->cellCache[$pCoord];
79 }
80
81
82 /**
83 * Clone the cell collection
84 *
85 * @param PHPExcel_Worksheet $parent The new worksheet
86 */
87 public function copyCellCollection(PHPExcel_Worksheet $parent)
88 {
89 parent::copyCellCollection($parent);
90
91 $newCollection = array();
92 foreach ($this->cellCache as $k => &$cell) {
93 $newCollection[$k] = clone $cell;
94 $newCollection[$k]->attach($this);
95 }
96
97 $this->cellCache = $newCollection;
98 }
99
100 /**
101 * Clear the cell collection and disconnect from our parent
102 *
103 */
104 public function unsetWorksheetCells()
105 {
106 // Because cells are all stored as intact objects in memory, we need to detach each one from the parent
107 foreach ($this->cellCache as $k => &$cell) {
108 $cell->detach();
109 $this->cellCache[$k] = null;
110 }
111 unset($cell);
112
113 $this->cellCache = array();
114
115 // detach ourself from the worksheet, so that it can then delete this object successfully
116 $this->parent = null;
117 }
118 }
1 <?php
2
3 /**
4 * PHPExcel_CachedObjectStorage_MemoryGZip
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * @category PHPExcel
23 * @package PHPExcel_CachedObjectStorage
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
27 */
28 class PHPExcel_CachedObjectStorage_MemoryGZip extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
29 {
30 /**
31 * Store cell data in cache for the current cell object if it's "dirty",
32 * and the 'nullify' the current cell object
33 *
34 * @return void
35 * @throws PHPExcel_Exception
36 */
37 protected function storeData()
38 {
39 if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
40 $this->currentObject->detach();
41
42 $this->cellCache[$this->currentObjectID] = gzdeflate(serialize($this->currentObject));
43 $this->currentCellIsDirty = false;
44 }
45 $this->currentObjectID = $this->currentObject = null;
46 }
47
48
49 /**
50 * Add or Update a cell in cache identified by coordinate address
51 *
52 * @param string $pCoord Coordinate address of the cell to update
53 * @param PHPExcel_Cell $cell Cell to update
54 * @return PHPExcel_Cell
55 * @throws PHPExcel_Exception
56 */
57 public function addCacheData($pCoord, PHPExcel_Cell $cell)
58 {
59 if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
60 $this->storeData();
61 }
62
63 $this->currentObjectID = $pCoord;
64 $this->currentObject = $cell;
65 $this->currentCellIsDirty = true;
66
67 return $cell;
68 }
69
70
71 /**
72 * Get cell at a specific coordinate
73 *
74 * @param string $pCoord Coordinate of the cell
75 * @throws PHPExcel_Exception
76 * @return PHPExcel_Cell Cell that was found, or null if not found
77 */
78 public function getCacheData($pCoord)
79 {
80 if ($pCoord === $this->currentObjectID) {
81 return $this->currentObject;
82 }
83 $this->storeData();
84
85 // Check if the entry that has been requested actually exists
86 if (!isset($this->cellCache[$pCoord])) {
87 // Return null if requested entry doesn't exist in cache
88 return null;
89 }
90
91 // Set current entry to the requested entry
92 $this->currentObjectID = $pCoord;
93 $this->currentObject = unserialize(gzinflate($this->cellCache[$pCoord]));
94 // Re-attach this as the cell's parent
95 $this->currentObject->attach($this);
96
97 // Return requested entry
98 return $this->currentObject;
99 }
100
101
102 /**
103 * Get a list of all cell addresses currently held in cache
104 *
105 * @return string[]
106 */
107 public function getCellList()
108 {
109 if ($this->currentObjectID !== null) {
110 $this->storeData();
111 }
112
113 return parent::getCellList();
114 }
115
116
117 /**
118 * Clear the cell collection and disconnect from our parent
119 *
120 * @return void
121 */
122 public function unsetWorksheetCells()
123 {
124 if (!is_null($this->currentObject)) {
125 $this->currentObject->detach();
126 $this->currentObject = $this->currentObjectID = null;
127 }
128 $this->cellCache = array();
129
130 // detach ourself from the worksheet, so that it can then delete this object successfully
131 $this->parent = null;
132 }
133 }
1 <?php
2
3 /**
4 * PHPExcel_CachedObjectStorage_MemorySerialized
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * @category PHPExcel
23 * @package PHPExcel_CachedObjectStorage
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
27 */
28 class PHPExcel_CachedObjectStorage_MemorySerialized extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
29 {
30 /**
31 * Store cell data in cache for the current cell object if it's "dirty",
32 * and the 'nullify' the current cell object
33 *
34 * @return void
35 * @throws PHPExcel_Exception
36 */
37 protected function storeData()
38 {
39 if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
40 $this->currentObject->detach();
41
42 $this->cellCache[$this->currentObjectID] = serialize($this->currentObject);
43 $this->currentCellIsDirty = false;
44 }
45 $this->currentObjectID = $this->currentObject = null;
46 }
47
48 /**
49 * Add or Update a cell in cache identified by coordinate address
50 *
51 * @param string $pCoord Coordinate address of the cell to update
52 * @param PHPExcel_Cell $cell Cell to update
53 * @return PHPExcel_Cell
54 * @throws PHPExcel_Exception
55 */
56 public function addCacheData($pCoord, PHPExcel_Cell $cell)
57 {
58 if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
59 $this->storeData();
60 }
61
62 $this->currentObjectID = $pCoord;
63 $this->currentObject = $cell;
64 $this->currentCellIsDirty = true;
65
66 return $cell;
67 }
68
69 /**
70 * Get cell at a specific coordinate
71 *
72 * @param string $pCoord Coordinate of the cell
73 * @throws PHPExcel_Exception
74 * @return PHPExcel_Cell Cell that was found, or null if not found
75 */
76 public function getCacheData($pCoord)
77 {
78 if ($pCoord === $this->currentObjectID) {
79 return $this->currentObject;
80 }
81 $this->storeData();
82
83 // Check if the entry that has been requested actually exists
84 if (!isset($this->cellCache[$pCoord])) {
85 // Return null if requested entry doesn't exist in cache
86 return null;
87 }
88
89 // Set current entry to the requested entry
90 $this->currentObjectID = $pCoord;
91 $this->currentObject = unserialize($this->cellCache[$pCoord]);
92 // Re-attach this as the cell's parent
93 $this->currentObject->attach($this);
94
95 // Return requested entry
96 return $this->currentObject;
97 }
98
99 /**
100 * Get a list of all cell addresses currently held in cache
101 *
102 * @return string[]
103 */
104 public function getCellList()
105 {
106 if ($this->currentObjectID !== null) {
107 $this->storeData();
108 }
109
110 return parent::getCellList();
111 }
112
113 /**
114 * Clear the cell collection and disconnect from our parent
115 *
116 * @return void
117 */
118 public function unsetWorksheetCells()
119 {
120 if (!is_null($this->currentObject)) {
121 $this->currentObject->detach();
122 $this->currentObject = $this->currentObjectID = null;
123 }
124 $this->cellCache = array();
125
126 // detach ourself from the worksheet, so that it can then delete this object successfully
127 $this->parent = null;
128 }
129 }
1 <?php
2
3 /**
4 * PHPExcel_CachedObjectStorage_PHPTemp
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * @category PHPExcel
23 * @package PHPExcel_CachedObjectStorage
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
27 */
28 class PHPExcel_CachedObjectStorage_PHPTemp extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
29 {
30 /**
31 * Name of the file for this cache
32 *
33 * @var string
34 */
35 private $fileHandle = null;
36
37 /**
38 * Memory limit to use before reverting to file cache
39 *
40 * @var integer
41 */
42 private $memoryCacheSize = null;
43
44 /**
45 * Store cell data in cache for the current cell object if it's "dirty",
46 * and the 'nullify' the current cell object
47 *
48 * @return void
49 * @throws PHPExcel_Exception
50 */
51 protected function storeData()
52 {
53 if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
54 $this->currentObject->detach();
55
56 fseek($this->fileHandle, 0, SEEK_END);
57
58 $this->cellCache[$this->currentObjectID] = array(
59 'ptr' => ftell($this->fileHandle),
60 'sz' => fwrite($this->fileHandle, serialize($this->currentObject))
61 );
62 $this->currentCellIsDirty = false;
63 }
64 $this->currentObjectID = $this->currentObject = null;
65 }
66
67
68 /**
69 * Add or Update a cell in cache identified by coordinate address
70 *
71 * @param string $pCoord Coordinate address of the cell to update
72 * @param PHPExcel_Cell $cell Cell to update
73 * @return PHPExcel_Cell
74 * @throws PHPExcel_Exception
75 */
76 public function addCacheData($pCoord, PHPExcel_Cell $cell)
77 {
78 if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
79 $this->storeData();
80 }
81
82 $this->currentObjectID = $pCoord;
83 $this->currentObject = $cell;
84 $this->currentCellIsDirty = true;
85
86 return $cell;
87 }
88
89
90 /**
91 * Get cell at a specific coordinate
92 *
93 * @param string $pCoord Coordinate of the cell
94 * @throws PHPExcel_Exception
95 * @return PHPExcel_Cell Cell that was found, or null if not found
96 */
97 public function getCacheData($pCoord)
98 {
99 if ($pCoord === $this->currentObjectID) {
100 return $this->currentObject;
101 }
102 $this->storeData();
103
104 // Check if the entry that has been requested actually exists
105 if (!isset($this->cellCache[$pCoord])) {
106 // Return null if requested entry doesn't exist in cache
107 return null;
108 }
109
110 // Set current entry to the requested entry
111 $this->currentObjectID = $pCoord;
112 fseek($this->fileHandle, $this->cellCache[$pCoord]['ptr']);
113 $this->currentObject = unserialize(fread($this->fileHandle, $this->cellCache[$pCoord]['sz']));
114 // Re-attach this as the cell's parent
115 $this->currentObject->attach($this);
116
117 // Return requested entry
118 return $this->currentObject;
119 }
120
121 /**
122 * Get a list of all cell addresses currently held in cache
123 *
124 * @return string[]
125 */
126 public function getCellList()
127 {
128 if ($this->currentObjectID !== null) {
129 $this->storeData();
130 }
131
132 return parent::getCellList();
133 }
134
135 /**
136 * Clone the cell collection
137 *
138 * @param PHPExcel_Worksheet $parent The new worksheet
139 * @return void
140 */
141 public function copyCellCollection(PHPExcel_Worksheet $parent)
142 {
143 parent::copyCellCollection($parent);
144 // Open a new stream for the cell cache data
145 $newFileHandle = fopen('php://temp/maxmemory:' . $this->memoryCacheSize, 'a+');
146 // Copy the existing cell cache data to the new stream
147 fseek($this->fileHandle, 0);
148 while (!feof($this->fileHandle)) {
149 fwrite($newFileHandle, fread($this->fileHandle, 1024));
150 }
151 $this->fileHandle = $newFileHandle;
152 }
153
154 /**
155 * Clear the cell collection and disconnect from our parent
156 *
157 * @return void
158 */
159 public function unsetWorksheetCells()
160 {
161 if (!is_null($this->currentObject)) {
162 $this->currentObject->detach();
163 $this->currentObject = $this->currentObjectID = null;
164 }
165 $this->cellCache = array();
166
167 // detach ourself from the worksheet, so that it can then delete this object successfully
168 $this->parent = null;
169
170 // Close down the php://temp file
171 $this->__destruct();
172 }
173
174 /**
175 * Initialise this new cell collection
176 *
177 * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
178 * @param array of mixed $arguments Additional initialisation arguments
179 */
180 public function __construct(PHPExcel_Worksheet $parent, $arguments)
181 {
182 $this->memoryCacheSize = (isset($arguments['memoryCacheSize'])) ? $arguments['memoryCacheSize'] : '1MB';
183
184 parent::__construct($parent);
185 if (is_null($this->fileHandle)) {
186 $this->fileHandle = fopen('php://temp/maxmemory:' . $this->memoryCacheSize, 'a+');
187 }
188 }
189
190 /**
191 * Destroy this cell collection
192 */
193 public function __destruct()
194 {
195 if (!is_null($this->fileHandle)) {
196 fclose($this->fileHandle);
197 }
198 $this->fileHandle = null;
199 }
200 }
1 <?php
2
3 /**
4 * PHPExcel_CachedObjectStorage_Wincache
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * @category PHPExcel
23 * @package PHPExcel_CachedObjectStorage
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
27 */
28 class PHPExcel_CachedObjectStorage_Wincache extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
29 {
30 /**
31 * Prefix used to uniquely identify cache data for this worksheet
32 *
33 * @var string
34 */
35 private $cachePrefix = null;
36
37 /**
38 * Cache timeout
39 *
40 * @var integer
41 */
42 private $cacheTime = 600;
43
44
45 /**
46 * Store cell data in cache for the current cell object if it's "dirty",
47 * and the 'nullify' the current cell object
48 *
49 * @return void
50 * @throws PHPExcel_Exception
51 */
52 protected function storeData()
53 {
54 if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
55 $this->currentObject->detach();
56
57 $obj = serialize($this->currentObject);
58 if (wincache_ucache_exists($this->cachePrefix.$this->currentObjectID.'.cache')) {
59 if (!wincache_ucache_set($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) {
60 $this->__destruct();
61 throw new PHPExcel_Exception('Failed to store cell '.$this->currentObjectID.' in WinCache');
62 }
63 } else {
64 if (!wincache_ucache_add($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) {
65 $this->__destruct();
66 throw new PHPExcel_Exception('Failed to store cell '.$this->currentObjectID.' in WinCache');
67 }
68 }
69 $this->currentCellIsDirty = false;
70 }
71
72 $this->currentObjectID = $this->currentObject = null;
73 }
74
75 /**
76 * Add or Update a cell in cache identified by coordinate address
77 *
78 * @param string $pCoord Coordinate address of the cell to update
79 * @param PHPExcel_Cell $cell Cell to update
80 * @return PHPExcel_Cell
81 * @throws PHPExcel_Exception
82 */
83 public function addCacheData($pCoord, PHPExcel_Cell $cell)
84 {
85 if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
86 $this->storeData();
87 }
88 $this->cellCache[$pCoord] = true;
89
90 $this->currentObjectID = $pCoord;
91 $this->currentObject = $cell;
92 $this->currentCellIsDirty = true;
93
94 return $cell;
95 }
96
97 /**
98 * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
99 *
100 * @param string $pCoord Coordinate address of the cell to check
101 * @return boolean
102 */
103 public function isDataSet($pCoord)
104 {
105 // Check if the requested entry is the current object, or exists in the cache
106 if (parent::isDataSet($pCoord)) {
107 if ($this->currentObjectID == $pCoord) {
108 return true;
109 }
110 // Check if the requested entry still exists in cache
111 $success = wincache_ucache_exists($this->cachePrefix.$pCoord.'.cache');
112 if ($success === false) {
113 // Entry no longer exists in Wincache, so clear it from the cache array
114 parent::deleteCacheData($pCoord);
115 throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
116 }
117 return true;
118 }
119 return false;
120 }
121
122
123 /**
124 * Get cell at a specific coordinate
125 *
126 * @param string $pCoord Coordinate of the cell
127 * @throws PHPExcel_Exception
128 * @return PHPExcel_Cell Cell that was found, or null if not found
129 */
130 public function getCacheData($pCoord)
131 {
132 if ($pCoord === $this->currentObjectID) {
133 return $this->currentObject;
134 }
135 $this->storeData();
136
137 // Check if the entry that has been requested actually exists
138 $obj = null;
139 if (parent::isDataSet($pCoord)) {
140 $success = false;
141 $obj = wincache_ucache_get($this->cachePrefix.$pCoord.'.cache', $success);
142 if ($success === false) {
143 // Entry no longer exists in WinCache, so clear it from the cache array
144 parent::deleteCacheData($pCoord);
145 throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
146 }
147 } else {
148 // Return null if requested entry doesn't exist in cache
149 return null;
150 }
151
152 // Set current entry to the requested entry
153 $this->currentObjectID = $pCoord;
154 $this->currentObject = unserialize($obj);
155 // Re-attach this as the cell's parent
156 $this->currentObject->attach($this);
157
158 // Return requested entry
159 return $this->currentObject;
160 }
161
162
163 /**
164 * Get a list of all cell addresses currently held in cache
165 *
166 * @return string[]
167 */
168 public function getCellList()
169 {
170 if ($this->currentObjectID !== null) {
171 $this->storeData();
172 }
173
174 return parent::getCellList();
175 }
176
177 /**
178 * Delete a cell in cache identified by coordinate address
179 *
180 * @param string $pCoord Coordinate address of the cell to delete
181 * @throws PHPExcel_Exception
182 */
183 public function deleteCacheData($pCoord)
184 {
185 // Delete the entry from Wincache
186 wincache_ucache_delete($this->cachePrefix.$pCoord.'.cache');
187
188 // Delete the entry from our cell address array
189 parent::deleteCacheData($pCoord);
190 }
191
192 /**
193 * Clone the cell collection
194 *
195 * @param PHPExcel_Worksheet $parent The new worksheet
196 * @return void
197 */
198 public function copyCellCollection(PHPExcel_Worksheet $parent)
199 {
200 parent::copyCellCollection($parent);
201 // Get a new id for the new file name
202 $baseUnique = $this->getUniqueID();
203 $newCachePrefix = substr(md5($baseUnique), 0, 8) . '.';
204 $cacheList = $this->getCellList();
205 foreach ($cacheList as $cellID) {
206 if ($cellID != $this->currentObjectID) {
207 $success = false;
208 $obj = wincache_ucache_get($this->cachePrefix.$cellID.'.cache', $success);
209 if ($success === false) {
210 // Entry no longer exists in WinCache, so clear it from the cache array
211 parent::deleteCacheData($cellID);
212 throw new PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in Wincache');
213 }
214 if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->cacheTime)) {
215 $this->__destruct();
216 throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in Wincache');
217 }
218 }
219 }
220 $this->cachePrefix = $newCachePrefix;
221 }
222
223
224 /**
225 * Clear the cell collection and disconnect from our parent
226 *
227 * @return void
228 */
229 public function unsetWorksheetCells()
230 {
231 if (!is_null($this->currentObject)) {
232 $this->currentObject->detach();
233 $this->currentObject = $this->currentObjectID = null;
234 }
235
236 // Flush the WinCache cache
237 $this->__destruct();
238
239 $this->cellCache = array();
240
241 // detach ourself from the worksheet, so that it can then delete this object successfully
242 $this->parent = null;
243 }
244
245 /**
246 * Initialise this new cell collection
247 *
248 * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
249 * @param array of mixed $arguments Additional initialisation arguments
250 */
251 public function __construct(PHPExcel_Worksheet $parent, $arguments)
252 {
253 $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
254
255 if (is_null($this->cachePrefix)) {
256 $baseUnique = $this->getUniqueID();
257 $this->cachePrefix = substr(md5($baseUnique), 0, 8).'.';
258 $this->cacheTime = $cacheTime;
259
260 parent::__construct($parent);
261 }
262 }
263
264 /**
265 * Destroy this cell collection
266 */
267 public function __destruct()
268 {
269 $cacheList = $this->getCellList();
270 foreach ($cacheList as $cellID) {
271 wincache_ucache_delete($this->cachePrefix.$cellID.'.cache');
272 }
273 }
274
275 /**
276 * Identify whether the caching method is currently available
277 * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
278 *
279 * @return boolean
280 */
281 public static function cacheMethodIsAvailable()
282 {
283 if (!function_exists('wincache_ucache_add')) {
284 return false;
285 }
286
287 return true;
288 }
289 }
1 <?php
2
3 /**
4 * PHPExcel_CachedObjectStorageFactory
5 *
6 * Copyright (c) 2006 - 2015 PHPExcel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * @category PHPExcel
23 * @package PHPExcel_CachedObjectStorage
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
27 */
28 class PHPExcel_CachedObjectStorageFactory
29 {
30 const cache_in_memory = 'Memory';
31 const cache_in_memory_gzip = 'MemoryGZip';
32 const cache_in_memory_serialized = 'MemorySerialized';
33 const cache_igbinary = 'Igbinary';
34 const cache_to_discISAM = 'DiscISAM';
35 const cache_to_apc = 'APC';
36 const cache_to_memcache = 'Memcache';
37 const cache_to_phpTemp = 'PHPTemp';
38 const cache_to_wincache = 'Wincache';
39 const cache_to_sqlite = 'SQLite';
40 const cache_to_sqlite3 = 'SQLite3';
41
42 /**
43 * Name of the method used for cell cacheing
44 *
45 * @var string
46 */
47 private static $cacheStorageMethod = null;
48
49 /**
50 * Name of the class used for cell cacheing
51 *
52 * @var string
53 */
54 private static $cacheStorageClass = null;
55
56 /**
57 * List of all possible cache storage methods
58 *
59 * @var string[]
60 */
61 private static $storageMethods = array(
62 self::cache_in_memory,
63 self::cache_in_memory_gzip,
64 self::cache_in_memory_serialized,
65 self::cache_igbinary,
66 self::cache_to_phpTemp,
67 self::cache_to_discISAM,
68 self::cache_to_apc,
69 self::cache_to_memcache,
70 self::cache_to_wincache,
71 self::cache_to_sqlite,
72 self::cache_to_sqlite3,
73 );
74
75 /**
76 * Default arguments for each cache storage method
77 *
78 * @var array of mixed array
79 */
80 private static $storageMethodDefaultParameters = array(
81 self::cache_in_memory => array(
82 ),
83 self::cache_in_memory_gzip => array(
84 ),
85 self::cache_in_memory_serialized => array(
86 ),
87 self::cache_igbinary => array(
88 ),
89 self::cache_to_phpTemp => array( 'memoryCacheSize' => '1MB'
90 ),
91 self::cache_to_discISAM => array( 'dir' => null
92 ),
93 self::cache_to_apc => array( 'cacheTime' => 600
94 ),
95 self::cache_to_memcache => array( 'memcacheServer' => 'localhost',
96 'memcachePort' => 11211,
97 'cacheTime' => 600
98 ),
99 self::cache_to_wincache => array( 'cacheTime' => 600
100 ),
101 self::cache_to_sqlite => array(
102 ),
103 self::cache_to_sqlite3 => array(
104 ),
105 );
106
107 /**
108 * Arguments for the active cache storage method
109 *
110 * @var array of mixed array
111 */
112 private static $storageMethodParameters = array();
113
114 /**
115 * Return the current cache storage method
116 *
117 * @return string|null
118 **/
119 public static function getCacheStorageMethod()
120 {
121 return self::$cacheStorageMethod;
122 }
123
124 /**
125 * Return the current cache storage class
126 *
127 * @return PHPExcel_CachedObjectStorage_ICache|null
128 **/
129 public static function getCacheStorageClass()
130 {
131 return self::$cacheStorageClass;
132 }
133
134 /**
135 * Return the list of all possible cache storage methods
136 *
137 * @return string[]
138 **/
139 public static function getAllCacheStorageMethods()
140 {
141 return self::$storageMethods;
142 }
143
144 /**
145 * Return the list of all available cache storage methods
146 *
147 * @return string[]
148 **/
149 public static function getCacheStorageMethods()
150 {
151 $activeMethods = array();
152 foreach (self::$storageMethods as $storageMethod) {
153 $cacheStorageClass = 'PHPExcel_CachedObjectStorage_' . $storageMethod;
154 if (call_user_func(array($cacheStorageClass, 'cacheMethodIsAvailable'))) {
155 $activeMethods[] = $storageMethod;
156 }
157 }
158 return $activeMethods;
159 }
160
161 /**
162 * Identify the cache storage method to use
163 *
164 * @param string $method Name of the method to use for cell cacheing
165 * @param array of mixed $arguments Additional arguments to pass to the cell caching class
166 * when instantiating
167 * @return boolean
168 **/
169 public static function initialize($method = self::cache_in_memory, $arguments = array())
170 {
171 if (!in_array($method, self::$storageMethods)) {
172 return false;
173 }
174
175 $cacheStorageClass = 'PHPExcel_CachedObjectStorage_'.$method;
176 if (!call_user_func(array( $cacheStorageClass,
177 'cacheMethodIsAvailable'))) {
178 return false;
179 }
180
181 self::$storageMethodParameters[$method] = self::$storageMethodDefaultParameters[$method];
182 foreach ($arguments as $k => $v) {
183 if (array_key_exists($k, self::$storageMethodParameters[$method])) {
184 self::$storageMethodParameters[$method][$k] = $v;
185 }
186 }
187
188 if (self::$cacheStorageMethod === null) {
189 self::$cacheStorageClass = 'PHPExcel_CachedObjectStorage_' . $method;
190 self::$cacheStorageMethod = $method;
191 }
192 return true;
193 }
194
195 /**
196 * Initialise the cache storage
197 *
198 * @param PHPExcel_Worksheet $parent Enable cell caching for this worksheet
199 * @return PHPExcel_CachedObjectStorage_ICache
200 **/
201 public static function getInstance(PHPExcel_Worksheet $parent)
202 {
203 $cacheMethodIsAvailable = true;
204 if (self::$cacheStorageMethod === null) {
205 $cacheMethodIsAvailable = self::initialize();
206 }
207
208 if ($cacheMethodIsAvailable) {
209 $instance = new self::$cacheStorageClass(
210 $parent,
211 self::$storageMethodParameters[self::$cacheStorageMethod]
212 );
213 if ($instance !== null) {
214 return $instance;
215 }
216 }
217
218 return false;
219 }
220
221 /**
222 * Clear the cache storage
223 *
224 **/
225 public static function finalize()
226 {
227 self::$cacheStorageMethod = null;
228 self::$cacheStorageClass = null;
229 self::$storageMethodParameters = array();
230 }
231 }
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!