Skip to content

Commit 40c68f6

Browse files
committed
Initial commit of redirector component.
Signed-off-by: Jason Lewis <jason.lewis1991@gmail.com>
1 parent 19aaa2a commit 40c68f6

2 files changed

Lines changed: 164 additions & 2 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php namespace Feather\Components\Foundation;
2+
3+
use URL;
4+
use URI;
5+
use Input;
6+
use Request;
7+
use Redirect;
8+
use Feather\Auth;
9+
use Feather\Config;
10+
use Laravel\Messages;
11+
12+
class Redirector extends Redirect {
13+
14+
/**
15+
* Add an alert to the session flash data.
16+
*
17+
* @param string $type
18+
* @param string $key
19+
* @param array $replacements
20+
* @return object
21+
*/
22+
public function with_alert($type, $key, $replacements = array())
23+
{
24+
return $this->with('alert', array('type' => $type, 'message' => __($key, $replacements)));
25+
}
26+
27+
/**
28+
* Create a redirect response with the query string appended.
29+
*
30+
* @param string $url
31+
* @return object
32+
*/
33+
public function with_query($url)
34+
{
35+
$url = URL::to($this->pattern($url));
36+
37+
if($query = urldecode(Request::getQueryString()))
38+
{
39+
$url = "{$url}?{$query}";
40+
}
41+
42+
return $this->to($url);
43+
}
44+
45+
/**
46+
* Create a redirect response to the current page.
47+
*
48+
* @return object
49+
*/
50+
public function to_self()
51+
{
52+
return $this->with_query(URI::current());
53+
}
54+
55+
/**
56+
* Create a redirect response to the previous page.
57+
*
58+
* @param string $default
59+
* @return object
60+
*/
61+
public function to_previous($default = null)
62+
{
63+
return $this->to(Input::has('return') ? Input::get('return') : $this->pattern($default));
64+
}
65+
66+
/**
67+
* Create a redirect response after a logout.
68+
*
69+
* @return object
70+
*/
71+
public static function after_logout()
72+
{
73+
return $this->for_auth(Config::get('feather: auth.logout_url'));
74+
}
75+
76+
/**
77+
* Create a redirect response before a registration.
78+
*
79+
* @return object
80+
*/
81+
public static function before_register()
82+
{
83+
return $this->for_auth(Config::get('feather: auth.register_url'));
84+
}
85+
86+
/**
87+
* Create a redirect response before a login.
88+
*
89+
* @return object
90+
*/
91+
public static function before_login()
92+
{
93+
return $this->for_auth(Config::get('feather: auth.login_url'));
94+
}
95+
96+
/**
97+
* Create a redirect response to an authentication page.
98+
*
99+
* @param string $url
100+
* @return object
101+
*/
102+
protected function for_auth($url)
103+
{
104+
$replace = array(
105+
'{feather}' => Bundle::option('feather', 'handles'),
106+
'{current}' => URL::to(Input::has('return') ? Input::get('return') : URI::current()),
107+
'{token}' => Auth::online() ? Auth::user()->authenticator_token : null
108+
);
109+
110+
if(Config::get('feather: auth.driver') == 'internal')
111+
{
112+
$url = URL::to_route('feather');
113+
}
114+
else
115+
{
116+
$url = str_replace(array_keys($replace), array_values($replace), $url);
117+
}
118+
119+
return $this->to($url);
120+
}
121+
122+
/**
123+
* Builds a URL based on defined patterns.
124+
*
125+
* @param string $url
126+
* @return string
127+
*/
128+
protected function pattern($url)
129+
{
130+
if(starts_with($url, 'route: '))
131+
{
132+
return URL::to_route(substr($url, 7));
133+
}
134+
elseif(starts_with($url, 'action: '))
135+
{
136+
return URL::to_action(substr($url, 8));
137+
}
138+
139+
return $url;
140+
}
141+
142+
/**
143+
* Flash a Validator's errors to the session data.
144+
*
145+
* @param object|string $container
146+
* @return object
147+
*/
148+
public function with_errors($container)
149+
{
150+
$errors = !($container instanceof Messages) ? new Messages(array($container)) : $container;
151+
152+
return $this->with('errors', $errors);
153+
}
154+
155+
}

config/feather.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
{
5555
$feather['sso'] = $feather->share(function($feather)
5656
{
57-
return new Feather\Components\Auth\Dispactcher($feather);
57+
return new Feather\Components\Auth\SSO($feather);
5858
});
5959
},
6060
'gear' => function($feather)
@@ -63,7 +63,14 @@
6363
{
6464
return new Feather\Components\Gear\Manager($feather);
6565
});
66-
}
66+
},
67+
'redirect' => function($feather)
68+
{
69+
$feather['redirect'] = $feather->share(function($feather)
70+
{
71+
return new Feather\Components\Foundation\Redirector(null);
72+
});
73+
},
6774
),
6875

6976
/*

0 commit comments

Comments
 (0)