aimeos /
aimeos-slim
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
| 5 | * @copyright Aimeos (aimeos.org), 2016 |
||
| 6 | * @package Slim |
||
| 7 | * @subpackage Base |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace Aimeos\Slim\Base; |
||
| 11 | |||
| 12 | use Psr\Container\ContainerInterface; |
||
| 13 | |||
| 14 | |||
| 15 | /** |
||
| 16 | * Service providing the context objects |
||
| 17 | * |
||
| 18 | * @package Slim |
||
| 19 | * @subpackage Base |
||
| 20 | */ |
||
| 21 | class Context |
||
| 22 | { |
||
| 23 | private $container; |
||
| 24 | private $context; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Initializes the object |
||
| 29 | * |
||
| 30 | * @param ContainerInterface $container Dependency container |
||
| 31 | */ |
||
| 32 | public function __construct( ContainerInterface $container ) |
||
| 33 | { |
||
| 34 | $this->container = $container; |
||
| 35 | } |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns the current context |
||
| 40 | * |
||
| 41 | * @param bool $locale True to add locale object to context, false if not |
||
| 42 | * @param array $attributes Associative list of URL parameter |
||
| 43 | * @param string $type Configuration type ("frontend" or "backend") |
||
| 44 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
| 45 | */ |
||
| 46 | public function get( bool $locale = true, array $attributes = array(), |
||
| 47 | string $type = 'frontend' ) : \Aimeos\MShop\Context\Item\Iface |
||
| 48 | { |
||
| 49 | $config = $this->container->get( 'aimeos.config' )->get( $type ); |
||
| 50 | |||
| 51 | if( $this->context === null ) |
||
| 52 | { |
||
| 53 | $context = new \Aimeos\MShop\Context\Item\Standard(); |
||
| 54 | $context->setConfig( $config ); |
||
| 55 | |||
| 56 | $this->addDataBaseManager( $context ); |
||
| 57 | $this->addFilesystemManager( $context ); |
||
| 58 | $this->addMessageQueueManager( $context ); |
||
| 59 | $this->addLogger( $context ); |
||
| 60 | $this->addCache( $context ); |
||
| 61 | $this->addMailer( $context ); |
||
| 62 | $this->addProcess( $context ); |
||
| 63 | $this->addSession( $context ); |
||
| 64 | $this->addUser( $context ); |
||
| 65 | |||
| 66 | $this->context = $context; |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->context->setConfig( $config ); |
||
| 70 | |||
| 71 | if( $locale === true ) |
||
| 72 | { |
||
| 73 | $localeItem = $this->container->get( 'aimeos.locale' )->get( $this->context, $attributes ); |
||
| 74 | $this->context->setLocale( $localeItem ); |
||
| 75 | $this->context->setI18n( $this->container->get( 'aimeos.i18n' )->get( array( $localeItem->getLanguageId() ) ) ); |
||
| 76 | } |
||
| 77 | |||
| 78 | return $this->context; |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Adds the cache object to the context |
||
| 84 | * |
||
| 85 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config |
||
| 86 | * @return \Aimeos\MShop\Context\Item\Iface Modified context object |
||
| 87 | */ |
||
| 88 | protected function addCache( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface |
||
| 89 | { |
||
| 90 | $cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context ); |
||
| 91 | |||
| 92 | return $context->setCache( $cache ); |
||
| 93 | } |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * Adds the database manager object to the context |
||
| 98 | * |
||
| 99 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 100 | * @return \Aimeos\MShop\Context\Item\Iface Modified context object |
||
| 101 | */ |
||
| 102 | protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface |
||
| 103 | { |
||
| 104 | $dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() ); |
||
| 105 | |||
| 106 | return $context->setDatabaseManager( $dbm ); |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * Adds the filesystem manager object to the context |
||
| 112 | * |
||
| 113 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 114 | * @return \Aimeos\MShop\Context\Item\Iface Modified context object |
||
| 115 | */ |
||
| 116 | protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface |
||
| 117 | { |
||
| 118 | $fs = new \Aimeos\MW\Filesystem\Manager\Standard( $context->getConfig() ); |
||
| 119 | |||
| 120 | return $context->setFilesystemManager( $fs ); |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Adds the logger object to the context |
||
| 126 | * |
||
| 127 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 128 | * @return \Aimeos\MShop\Context\Item\Iface Modified context object |
||
| 129 | */ |
||
| 130 | protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface |
||
| 131 | { |
||
| 132 | $logger = \Aimeos\MAdmin::create( $context, 'log' ); |
||
| 133 | |||
| 134 | return $context->setLogger( $logger ); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Adds the mailer object to the context |
||
| 141 | * |
||
| 142 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 143 | * @return \Aimeos\MShop\Context\Item\Iface Modified context object |
||
| 144 | */ |
||
| 145 | protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface |
||
| 146 | { |
||
| 147 | $mail = new \Aimeos\MW\Mail\Swift( $this->container->get( 'mailer' ) ); |
||
| 148 | |||
| 149 | return $context->setMail( $mail ); |
||
| 150 | } |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Adds the message queue manager object to the context |
||
| 155 | * |
||
| 156 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 157 | * @return \Aimeos\MShop\Context\Item\Iface Modified context object |
||
| 158 | */ |
||
| 159 | protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface |
||
| 160 | { |
||
| 161 | $mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() ); |
||
| 162 | |||
| 163 | return $context->setMessageQueueManager( $mq ); |
||
| 164 | } |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Adds the process object to the context |
||
| 169 | * |
||
| 170 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 171 | * @return \Aimeos\MShop\Context\Item\Iface Modified context object |
||
| 172 | */ |
||
| 173 | protected function addProcess( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface |
||
| 174 | { |
||
| 175 | $config = $context->getConfig(); |
||
| 176 | $max = $config->get( 'pcntl_max', 4 ); |
||
| 177 | $prio = $config->get( 'pcntl_priority', 19 ); |
||
| 178 | |||
| 179 | $process = new \Aimeos\MW\Process\Pcntl( $max, $prio ); |
||
| 180 | $process = new \Aimeos\MW\Process\Decorator\Check( $process ); |
||
| 181 | |||
| 182 | return $context->setProcess( $process ); |
||
| 183 | } |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * Adds the session object to the context |
||
| 188 | * |
||
| 189 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 190 | * @return \Aimeos\MShop\Context\Item\Iface Modified context object |
||
| 191 | */ |
||
| 192 | protected function addSession( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface |
||
| 193 | { |
||
| 194 | $session = new \Aimeos\MW\Session\PHP(); |
||
| 195 | |||
| 196 | return $context->setSession( $session ); |
||
| 197 | } |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Adds user information to the context |
||
| 202 | * |
||
| 203 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 204 | * @return \Aimeos\MShop\Context\Item\Iface Modified context object |
||
| 205 | */ |
||
| 206 | protected function addUser( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface |
||
| 207 | { |
||
| 208 | $ipaddr = $this->container->request->getAttribute( 'ip_address' ); |
||
|
0 ignored issues
–
show
|
|||
| 209 | |||
| 210 | return $context->setEditor( $ipaddr ?: '' ); |
||
| 211 | } |
||
| 212 | } |
||
| 213 |