Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Session;
4
 
5
use Closure;
6
use Illuminate\Contracts\Session\Session;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\MessageBag;
9
use Illuminate\Support\Str;
10
use Illuminate\Support\Traits\Macroable;
11
use Illuminate\Support\ViewErrorBag;
12
use SessionHandlerInterface;
13
use stdClass;
14
 
15
class Store implements Session
16
{
17
    use Macroable;
18
 
19
    /**
20
     * The session ID.
21
     *
22
     * @var string
23
     */
24
    protected $id;
25
 
26
    /**
27
     * The session name.
28
     *
29
     * @var string
30
     */
31
    protected $name;
32
 
33
    /**
34
     * The session attributes.
35
     *
36
     * @var array
37
     */
38
    protected $attributes = [];
39
 
40
    /**
41
     * The session handler implementation.
42
     *
43
     * @var \SessionHandlerInterface
44
     */
45
    protected $handler;
46
 
47
    /**
48
     * The session store's serialization strategy.
49
     *
50
     * @var string
51
     */
52
    protected $serialization = 'php';
53
 
54
    /**
55
     * Session store started status.
56
     *
57
     * @var bool
58
     */
59
    protected $started = false;
60
 
61
    /**
62
     * Create a new session instance.
63
     *
64
     * @param  string  $name
65
     * @param  \SessionHandlerInterface  $handler
66
     * @param  string|null  $id
67
     * @param  string  $serialization
68
     * @return void
69
     */
70
    public function __construct($name, SessionHandlerInterface $handler, $id = null, $serialization = 'php')
71
    {
72
        $this->setId($id);
73
        $this->name = $name;
74
        $this->handler = $handler;
75
        $this->serialization = $serialization;
76
    }
77
 
78
    /**
79
     * Start the session, reading the data from a handler.
80
     *
81
     * @return bool
82
     */
83
    public function start()
84
    {
85
        $this->loadSession();
86
 
87
        if (! $this->has('_token')) {
88
            $this->regenerateToken();
89
        }
90
 
91
        return $this->started = true;
92
    }
93
 
94
    /**
95
     * Load the session data from the handler.
96
     *
97
     * @return void
98
     */
99
    protected function loadSession()
100
    {
101
        $this->attributes = array_merge($this->attributes, $this->readFromHandler());
102
 
103
        $this->marshalErrorBag();
104
    }
105
 
106
    /**
107
     * Read the session data from the handler.
108
     *
109
     * @return array
110
     */
111
    protected function readFromHandler()
112
    {
113
        if ($data = $this->handler->read($this->getId())) {
114
            if ($this->serialization === 'json') {
115
                $data = json_decode($this->prepareForUnserialize($data), true);
116
            } else {
117
                $data = @unserialize($this->prepareForUnserialize($data));
118
            }
119
 
120
            if ($data !== false && is_array($data)) {
121
                return $data;
122
            }
123
        }
124
 
125
        return [];
126
    }
127
 
128
    /**
129
     * Prepare the raw string data from the session for unserialization.
130
     *
131
     * @param  string  $data
132
     * @return string
133
     */
134
    protected function prepareForUnserialize($data)
135
    {
136
        return $data;
137
    }
138
 
139
    /**
140
     * Marshal the ViewErrorBag when using JSON serialization for sessions.
141
     *
142
     * @return void
143
     */
144
    protected function marshalErrorBag()
145
    {
146
        if ($this->serialization !== 'json' || $this->missing('errors')) {
147
            return;
148
        }
149
 
150
        $errorBag = new ViewErrorBag;
151
 
152
        foreach ($this->get('errors') as $key => $value) {
153
            $messageBag = new MessageBag($value['messages']);
154
 
155
            $errorBag->put($key, $messageBag->setFormat($value['format']));
156
        }
157
 
158
        $this->put('errors', $errorBag);
159
    }
160
 
161
    /**
162
     * Save the session data to storage.
163
     *
164
     * @return void
165
     */
166
    public function save()
167
    {
168
        $this->ageFlashData();
169
 
170
        $this->prepareErrorBagForSerialization();
171
 
172
        $this->handler->write($this->getId(), $this->prepareForStorage(
173
            $this->serialization === 'json' ? json_encode($this->attributes) : serialize($this->attributes)
174
        ));
175
 
176
        $this->started = false;
177
    }
178
 
179
    /**
180
     * Prepare the ViewErrorBag instance for JSON serialization.
181
     *
182
     * @return void
183
     */
184
    protected function prepareErrorBagForSerialization()
185
    {
186
        if ($this->serialization !== 'json' || $this->missing('errors')) {
187
            return;
188
        }
189
 
190
        $errors = [];
191
 
192
        foreach ($this->attributes['errors']->getBags() as $key => $value) {
193
            $errors[$key] = [
194
                'format' => $value->getFormat(),
195
                'messages' => $value->getMessages(),
196
            ];
197
        }
198
 
199
        $this->attributes['errors'] = $errors;
200
    }
201
 
202
    /**
203
     * Prepare the serialized session data for storage.
204
     *
205
     * @param  string  $data
206
     * @return string
207
     */
208
    protected function prepareForStorage($data)
209
    {
210
        return $data;
211
    }
212
 
213
    /**
214
     * Age the flash data for the session.
215
     *
216
     * @return void
217
     */
218
    public function ageFlashData()
219
    {
220
        $this->forget($this->get('_flash.old', []));
221
 
222
        $this->put('_flash.old', $this->get('_flash.new', []));
223
 
224
        $this->put('_flash.new', []);
225
    }
226
 
227
    /**
228
     * Get all of the session data.
229
     *
230
     * @return array
231
     */
232
    public function all()
233
    {
234
        return $this->attributes;
235
    }
236
 
237
    /**
238
     * Get a subset of the session data.
239
     *
240
     * @param  array  $keys
241
     * @return array
242
     */
243
    public function only(array $keys)
244
    {
245
        return Arr::only($this->attributes, $keys);
246
    }
247
 
248
    /**
249
     * Checks if a key exists.
250
     *
251
     * @param  string|array  $key
252
     * @return bool
253
     */
254
    public function exists($key)
255
    {
256
        $placeholder = new stdClass;
257
 
258
        return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) use ($placeholder) {
259
            return $this->get($key, $placeholder) === $placeholder;
260
        });
261
    }
262
 
263
    /**
264
     * Determine if the given key is missing from the session data.
265
     *
266
     * @param  string|array  $key
267
     * @return bool
268
     */
269
    public function missing($key)
270
    {
271
        return ! $this->exists($key);
272
    }
273
 
274
    /**
275
     * Checks if a key is present and not null.
276
     *
277
     * @param  string|array  $key
278
     * @return bool
279
     */
280
    public function has($key)
281
    {
282
        return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) {
283
            return is_null($this->get($key));
284
        });
285
    }
286
 
287
    /**
288
     * Get an item from the session.
289
     *
290
     * @param  string  $key
291
     * @param  mixed  $default
292
     * @return mixed
293
     */
294
    public function get($key, $default = null)
295
    {
296
        return Arr::get($this->attributes, $key, $default);
297
    }
298
 
299
    /**
300
     * Get the value of a given key and then forget it.
301
     *
302
     * @param  string  $key
303
     * @param  mixed  $default
304
     * @return mixed
305
     */
306
    public function pull($key, $default = null)
307
    {
308
        return Arr::pull($this->attributes, $key, $default);
309
    }
310
 
311
    /**
312
     * Determine if the session contains old input.
313
     *
314
     * @param  string|null  $key
315
     * @return bool
316
     */
317
    public function hasOldInput($key = null)
318
    {
319
        $old = $this->getOldInput($key);
320
 
321
        return is_null($key) ? count($old) > 0 : ! is_null($old);
322
    }
323
 
324
    /**
325
     * Get the requested item from the flashed input array.
326
     *
327
     * @param  string|null  $key
328
     * @param  mixed  $default
329
     * @return mixed
330
     */
331
    public function getOldInput($key = null, $default = null)
332
    {
333
        return Arr::get($this->get('_old_input', []), $key, $default);
334
    }
335
 
336
    /**
337
     * Replace the given session attributes entirely.
338
     *
339
     * @param  array  $attributes
340
     * @return void
341
     */
342
    public function replace(array $attributes)
343
    {
344
        $this->put($attributes);
345
    }
346
 
347
    /**
348
     * Put a key / value pair or array of key / value pairs in the session.
349
     *
350
     * @param  string|array  $key
351
     * @param  mixed  $value
352
     * @return void
353
     */
354
    public function put($key, $value = null)
355
    {
356
        if (! is_array($key)) {
357
            $key = [$key => $value];
358
        }
359
 
360
        foreach ($key as $arrayKey => $arrayValue) {
361
            Arr::set($this->attributes, $arrayKey, $arrayValue);
362
        }
363
    }
364
 
365
    /**
366
     * Get an item from the session, or store the default value.
367
     *
368
     * @param  string  $key
369
     * @param  \Closure  $callback
370
     * @return mixed
371
     */
372
    public function remember($key, Closure $callback)
373
    {
374
        if (! is_null($value = $this->get($key))) {
375
            return $value;
376
        }
377
 
378
        return tap($callback(), function ($value) use ($key) {
379
            $this->put($key, $value);
380
        });
381
    }
382
 
383
    /**
384
     * Push a value onto a session array.
385
     *
386
     * @param  string  $key
387
     * @param  mixed  $value
388
     * @return void
389
     */
390
    public function push($key, $value)
391
    {
392
        $array = $this->get($key, []);
393
 
394
        $array[] = $value;
395
 
396
        $this->put($key, $array);
397
    }
398
 
399
    /**
400
     * Increment the value of an item in the session.
401
     *
402
     * @param  string  $key
403
     * @param  int  $amount
404
     * @return mixed
405
     */
406
    public function increment($key, $amount = 1)
407
    {
408
        $this->put($key, $value = $this->get($key, 0) + $amount);
409
 
410
        return $value;
411
    }
412
 
413
    /**
414
     * Decrement the value of an item in the session.
415
     *
416
     * @param  string  $key
417
     * @param  int  $amount
418
     * @return int
419
     */
420
    public function decrement($key, $amount = 1)
421
    {
422
        return $this->increment($key, $amount * -1);
423
    }
424
 
425
    /**
426
     * Flash a key / value pair to the session.
427
     *
428
     * @param  string  $key
429
     * @param  mixed  $value
430
     * @return void
431
     */
432
    public function flash(string $key, $value = true)
433
    {
434
        $this->put($key, $value);
435
 
436
        $this->push('_flash.new', $key);
437
 
438
        $this->removeFromOldFlashData([$key]);
439
    }
440
 
441
    /**
442
     * Flash a key / value pair to the session for immediate use.
443
     *
444
     * @param  string  $key
445
     * @param  mixed  $value
446
     * @return void
447
     */
448
    public function now($key, $value)
449
    {
450
        $this->put($key, $value);
451
 
452
        $this->push('_flash.old', $key);
453
    }
454
 
455
    /**
456
     * Reflash all of the session flash data.
457
     *
458
     * @return void
459
     */
460
    public function reflash()
461
    {
462
        $this->mergeNewFlashes($this->get('_flash.old', []));
463
 
464
        $this->put('_flash.old', []);
465
    }
466
 
467
    /**
468
     * Reflash a subset of the current flash data.
469
     *
470
     * @param  array|mixed  $keys
471
     * @return void
472
     */
473
    public function keep($keys = null)
474
    {
475
        $this->mergeNewFlashes($keys = is_array($keys) ? $keys : func_get_args());
476
 
477
        $this->removeFromOldFlashData($keys);
478
    }
479
 
480
    /**
481
     * Merge new flash keys into the new flash array.
482
     *
483
     * @param  array  $keys
484
     * @return void
485
     */
486
    protected function mergeNewFlashes(array $keys)
487
    {
488
        $values = array_unique(array_merge($this->get('_flash.new', []), $keys));
489
 
490
        $this->put('_flash.new', $values);
491
    }
492
 
493
    /**
494
     * Remove the given keys from the old flash data.
495
     *
496
     * @param  array  $keys
497
     * @return void
498
     */
499
    protected function removeFromOldFlashData(array $keys)
500
    {
501
        $this->put('_flash.old', array_diff($this->get('_flash.old', []), $keys));
502
    }
503
 
504
    /**
505
     * Flash an input array to the session.
506
     *
507
     * @param  array  $value
508
     * @return void
509
     */
510
    public function flashInput(array $value)
511
    {
512
        $this->flash('_old_input', $value);
513
    }
514
 
515
    /**
516
     * Remove an item from the session, returning its value.
517
     *
518
     * @param  string  $key
519
     * @return mixed
520
     */
521
    public function remove($key)
522
    {
523
        return Arr::pull($this->attributes, $key);
524
    }
525
 
526
    /**
527
     * Remove one or many items from the session.
528
     *
529
     * @param  string|array  $keys
530
     * @return void
531
     */
532
    public function forget($keys)
533
    {
534
        Arr::forget($this->attributes, $keys);
535
    }
536
 
537
    /**
538
     * Remove all of the items from the session.
539
     *
540
     * @return void
541
     */
542
    public function flush()
543
    {
544
        $this->attributes = [];
545
    }
546
 
547
    /**
548
     * Flush the session data and regenerate the ID.
549
     *
550
     * @return bool
551
     */
552
    public function invalidate()
553
    {
554
        $this->flush();
555
 
556
        return $this->migrate(true);
557
    }
558
 
559
    /**
560
     * Generate a new session identifier.
561
     *
562
     * @param  bool  $destroy
563
     * @return bool
564
     */
565
    public function regenerate($destroy = false)
566
    {
567
        return tap($this->migrate($destroy), function () {
568
            $this->regenerateToken();
569
        });
570
    }
571
 
572
    /**
573
     * Generate a new session ID for the session.
574
     *
575
     * @param  bool  $destroy
576
     * @return bool
577
     */
578
    public function migrate($destroy = false)
579
    {
580
        if ($destroy) {
581
            $this->handler->destroy($this->getId());
582
        }
583
 
584
        $this->setExists(false);
585
 
586
        $this->setId($this->generateSessionId());
587
 
588
        return true;
589
    }
590
 
591
    /**
592
     * Determine if the session has been started.
593
     *
594
     * @return bool
595
     */
596
    public function isStarted()
597
    {
598
        return $this->started;
599
    }
600
 
601
    /**
602
     * Get the name of the session.
603
     *
604
     * @return string
605
     */
606
    public function getName()
607
    {
608
        return $this->name;
609
    }
610
 
611
    /**
612
     * Set the name of the session.
613
     *
614
     * @param  string  $name
615
     * @return void
616
     */
617
    public function setName($name)
618
    {
619
        $this->name = $name;
620
    }
621
 
622
    /**
623
     * Get the current session ID.
624
     *
625
     * @return string
626
     */
627
    public function getId()
628
    {
629
        return $this->id;
630
    }
631
 
632
    /**
633
     * Set the session ID.
634
     *
635
     * @param  string  $id
636
     * @return void
637
     */
638
    public function setId($id)
639
    {
640
        $this->id = $this->isValidId($id) ? $id : $this->generateSessionId();
641
    }
642
 
643
    /**
644
     * Determine if this is a valid session ID.
645
     *
646
     * @param  string  $id
647
     * @return bool
648
     */
649
    public function isValidId($id)
650
    {
651
        return is_string($id) && ctype_alnum($id) && strlen($id) === 40;
652
    }
653
 
654
    /**
655
     * Get a new, random session ID.
656
     *
657
     * @return string
658
     */
659
    protected function generateSessionId()
660
    {
661
        return Str::random(40);
662
    }
663
 
664
    /**
665
     * Set the existence of the session on the handler if applicable.
666
     *
667
     * @param  bool  $value
668
     * @return void
669
     */
670
    public function setExists($value)
671
    {
672
        if ($this->handler instanceof ExistenceAwareInterface) {
673
            $this->handler->setExists($value);
674
        }
675
    }
676
 
677
    /**
678
     * Get the CSRF token value.
679
     *
680
     * @return string
681
     */
682
    public function token()
683
    {
684
        return $this->get('_token');
685
    }
686
 
687
    /**
688
     * Regenerate the CSRF token value.
689
     *
690
     * @return void
691
     */
692
    public function regenerateToken()
693
    {
694
        $this->put('_token', Str::random(40));
695
    }
696
 
697
    /**
698
     * Get the previous URL from the session.
699
     *
700
     * @return string|null
701
     */
702
    public function previousUrl()
703
    {
704
        return $this->get('_previous.url');
705
    }
706
 
707
    /**
708
     * Set the "previous" URL in the session.
709
     *
710
     * @param  string  $url
711
     * @return void
712
     */
713
    public function setPreviousUrl($url)
714
    {
715
        $this->put('_previous.url', $url);
716
    }
717
 
718
    /**
719
     * Specify that the user has confirmed their password.
720
     *
721
     * @return void
722
     */
723
    public function passwordConfirmed()
724
    {
725
        $this->put('auth.password_confirmed_at', time());
726
    }
727
 
728
    /**
729
     * Get the underlying session handler implementation.
730
     *
731
     * @return \SessionHandlerInterface
732
     */
733
    public function getHandler()
734
    {
735
        return $this->handler;
736
    }
737
 
738
    /**
739
     * Set the underlying session handler implementation.
740
     *
741
     * @param  \SessionHandlerInterface  $handler
742
     * @return void
743
     */
744
    public function setHandler(SessionHandlerInterface $handler)
745
    {
746
        return $this->handler = $handler;
747
    }
748
 
749
    /**
750
     * Determine if the session handler needs a request.
751
     *
752
     * @return bool
753
     */
754
    public function handlerNeedsRequest()
755
    {
756
        return $this->handler instanceof CookieSessionHandler;
757
    }
758
 
759
    /**
760
     * Set the request on the handler instance.
761
     *
762
     * @param  \Illuminate\Http\Request  $request
763
     * @return void
764
     */
765
    public function setRequestOnHandler($request)
766
    {
767
        if ($this->handlerNeedsRequest()) {
768
            $this->handler->setRequest($request);
769
        }
770
    }
771
}