Skip to content

Commit bff9af6

Browse files
committed
basic func for yii2 to work with laravel components tested with https://github.com/darryldecode/laravelshoppingcart
1 parent 58d6ecd commit bff9af6

File tree

2 files changed

+344
-0
lines changed

2 files changed

+344
-0
lines changed

Illuminate/Events/Dispatcher.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
namespace papppeter\yii2Laravel\Illuminate\Events;
3+
4+
use yii\base\Component;
5+
use yii\base\Event;
6+
use yii\base\UnknownMethodException;
7+
8+
/**
9+
*
10+
*/
11+
class Dispatcher extends Event {
12+
/**
13+
* Register an event listener with the dispatcher.
14+
*
15+
* @param string|array $events
16+
* @param mixed $listener
17+
* @return void
18+
*/
19+
public function listen($events, $listener)
20+
{
21+
foreach ((array) $events as $event) {
22+
app()->on($event, $listener);
23+
}
24+
}
25+
26+
/**
27+
* Determine if a given event has listeners.
28+
*
29+
* @param string $eventName
30+
* @return bool
31+
*/
32+
public function hasListeners($eventName)
33+
{
34+
return app()->hasEventHandlers($eventName);
35+
}
36+
37+
/**
38+
* Register an event subscriber with the dispatcher.
39+
*
40+
* @param object|string $subscriber
41+
* @return void
42+
*/
43+
public function subscribe($subscriber)
44+
{
45+
throw new UnknownMethodException();
46+
}
47+
48+
/**
49+
* Dispatch an event until the first non-null response is returned.
50+
*
51+
* @param string|object $event
52+
* @param mixed $payload
53+
* @return array|null
54+
*/
55+
public function until($event, $payload = [])
56+
{
57+
throw new UnknownMethodException();
58+
}
59+
60+
/**
61+
* Dispatch an event and call the listeners.
62+
*
63+
* @param string|object $event
64+
* @param mixed $payload
65+
* @param bool $halt
66+
* @return array|null
67+
*/
68+
public function dispatch($event, $payload = [], $halt = false)
69+
{
70+
if($event instanceof Event) {
71+
return app()->trigger($event->name, $event);
72+
}
73+
74+
list($event, $payload) = $this->parseEventAndPayload(
75+
$event, $payload
76+
);
77+
78+
$name = $event;
79+
$event = new Event();
80+
$event->data = $payload[0];
81+
$event->sender = $payload[1];
82+
$event->handled = $halt;
83+
84+
return app()->trigger($name, $event);
85+
}
86+
87+
/**
88+
* Register an event and payload to be fired later.
89+
*
90+
* @param string $event
91+
* @param array $payload
92+
* @return void
93+
*/
94+
public function push($event, $payload = [])
95+
{
96+
throw new UnknownMethodException();
97+
}
98+
99+
/**
100+
* Flush a set of pushed events.
101+
*
102+
* @param string $event
103+
* @return void
104+
*/
105+
public function flush($event)
106+
{
107+
throw new UnknownMethodException();
108+
}
109+
110+
/**
111+
* Remove a set of listeners from the dispatcher.
112+
*
113+
* @param string $event
114+
* @return void
115+
*/
116+
public function forget($event)
117+
{
118+
app()->off($event);
119+
}
120+
121+
/**
122+
* Forget all of the queued listeners.
123+
*
124+
* @return void
125+
*/
126+
public function forgetPushed()
127+
{
128+
throw new UnknownMethodException();
129+
}
130+
131+
/**
132+
* Fire an event and call the listeners.
133+
*
134+
* @param string|object $event
135+
* @param mixed $payload
136+
* @param bool $halt
137+
* @return array|null
138+
*/
139+
public function fire($event, $payload = [], $halt = false)
140+
{
141+
return $this->dispatch($event, $payload, $halt);
142+
}
143+
144+
145+
/**
146+
* Parse the given event and payload and prepare them for dispatching.
147+
*
148+
* @param mixed $event
149+
* @param mixed $payload
150+
* @return array
151+
*/
152+
protected function parseEventAndPayload($event, $payload)
153+
{
154+
if (is_object($event)) {
155+
[$payload, $event] = [[$event], get_class($event)];
156+
}
157+
158+
return [$event, array_wrap($payload)];
159+
}
160+
}

Illuminate/Session/Store.php

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
namespace papppeter\yii2Laravel\Illuminate\Session;
3+
4+
use Illuminate\Contracts\Session\Session;
5+
use Illuminate\Support\Arr;
6+
7+
/**
8+
*
9+
* @property string $previousUrl
10+
* @property \Illuminate\Http\Request $requestOnHandler
11+
*/
12+
class Store extends \yii\web\Session implements Session {
13+
14+
public function put($key, $value = null) {
15+
$this->set($key, $value);
16+
}
17+
18+
/**
19+
* Start the session, reading the data from a handler.
20+
*
21+
* @return bool
22+
*/
23+
public function start()
24+
{
25+
return $this->open();
26+
}
27+
28+
/**
29+
* Save the session data to storage.
30+
*
31+
* @return bool
32+
*/
33+
public function save()
34+
{
35+
$this->freeze();
36+
}
37+
38+
/**
39+
* Get all of the session data.
40+
*
41+
* @return array
42+
*/
43+
public function all()
44+
{
45+
return $_SESSION ?? [];
46+
}
47+
48+
/**
49+
* Checks if a key exists.
50+
*
51+
* @param string|array $key
52+
* @return bool
53+
*/
54+
public function exists($key)
55+
{
56+
return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) {
57+
return ! Arr::exists(array_keys($this->all()), $key);
58+
});
59+
}
60+
61+
/**
62+
* Checks if an a key is present and not null.
63+
*
64+
* @param string|array $key
65+
* @return bool
66+
*/
67+
public function has($key)
68+
{
69+
return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) {
70+
return is_null($this->get($key));
71+
});
72+
}
73+
74+
/**
75+
* Get the CSRF token value.
76+
*
77+
* @return string
78+
*/
79+
public function token()
80+
{
81+
return $this->get('_token');
82+
}
83+
84+
/**
85+
* Remove one or many items from the session.
86+
*
87+
* @param string|array $keys
88+
* @return void
89+
*/
90+
public function forget($keys)
91+
{
92+
foreach(array_wrap($keys) as $key) {
93+
$this->remove($key);
94+
}
95+
}
96+
97+
/**
98+
* Remove all of the items from the session.
99+
*
100+
* @return void
101+
*/
102+
public function flush()
103+
{
104+
$this->removeAll();
105+
}
106+
107+
/**
108+
* Generate a new session ID for the session.
109+
*
110+
* @param bool $destroy
111+
* @return bool
112+
*/
113+
public function migrate($destroy = false)
114+
{
115+
$this->regenerateID($destroy);
116+
117+
return true;
118+
}
119+
120+
/**
121+
* Determine if the session has been started.
122+
*
123+
* @return bool
124+
*/
125+
public function isStarted()
126+
{
127+
return $this->getIsActive();
128+
}
129+
130+
/**
131+
* Get the previous URL from the session.
132+
*
133+
* @return string|null
134+
*/
135+
public function previousUrl()
136+
{
137+
return $this->get('_previous.url');
138+
}
139+
140+
/**
141+
* Set the "previous" URL in the session.
142+
*
143+
* @param string $url
144+
* @return void
145+
*/
146+
public function setPreviousUrl($url)
147+
{
148+
$this->put('_previous.url', $url);
149+
}
150+
151+
/**
152+
* Get the session handler instance.
153+
*
154+
* @return \SessionHandlerInterface
155+
*/
156+
public function getHandler()
157+
{
158+
return $this->handler;
159+
}
160+
161+
/**
162+
* Determine if the session handler needs a request.
163+
*
164+
* @return bool
165+
*/
166+
public function handlerNeedsRequest()
167+
{
168+
return false;
169+
// return $this->handler instanceof CookieSessionHandler;
170+
}
171+
172+
/**
173+
* Set the request on the handler instance.
174+
*
175+
* @param \Illuminate\Http\Request $request
176+
* @return void
177+
*/
178+
public function setRequestOnHandler($request)
179+
{
180+
if ($this->handlerNeedsRequest()) {
181+
$this->handler->setRequest($request);
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)