summaryrefslogtreecommitdiffstats
path: root/src/Controller/Session.php
blob: cba83b7c6455b4893535cc2a874d58a4802e2c34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

namespace Jasny\Controller;

use Jasny\Controller\Session\Flash;

/**
 * Use session in the controller
 */
trait Session
{
    /**
     * Session
     * @var array|\ArrayObject
     */
    protected $session;

    /**
     * Flash message
     * @var Flash
     */
    protected $flash;
    
    
    /**
     * Get request, set for controller
     *
     * @return ServerRequestInterface
     */
    abstract public function getRequest();
    
    
    /**
     * Link the session to the session property in the controller
     */
    public function useSession()
    {
        $this->session = $this->getRequest()->getAttribute('session');
        
        if (!isset($this->session)) {
            $this->session =& $_SESSION;
        }
    }
    
    
    /**
     * Get an/or set the flash message.
     * 
     * @param mixed $type     flash type, eg. 'error', 'notice' or 'success'
     * @param mixed $message  flash message
     * @return Flash
     */
    public function flash($type = null, $message = null)
    {
        if (!isset($this->flash)) {
            $this->flash = new Flash($this->session);
        }
        
        if ($type) {
            $this->flash->set($type, $message);
        }
        
        return $this->flash;
    }
}