-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ResolveController.php
172 lines (135 loc) · 5.07 KB
/
ResolveController.php
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* @license MIT, http://opensource.org/licenses/MIT
* @copyright Aimeos (aimeos.org), 2023
*/
namespace Aimeos\Shop\Controller;
use Aimeos\Shop\Facades\Shop;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
/**
* Aimeos controller for dispatching requests.
*/
class ResolveController extends Controller
{
private static $fcn = [];
/**
* Register a new resolver function.
*
* @param string $name Name of the resolver function
* @param \Closure $fcn Resolver function
*/
public static function register( string $name, \Closure $fcn )
{
self::$fcn[$name] = $fcn;
}
/**
* Initializes the object.
*/
public function __construct()
{
self::$fcn['product'] = function( \Aimeos\MShop\ContextIface $context, string $path ) {
return $this->product( $context, $path );
};
self::$fcn['catalog'] = function( \Aimeos\MShop\ContextIface $context, string $path ) {
return $this->catalog( $context, $path );
};
}
/**
* Returns the html of the resolved URLs.
*
* @param \Illuminate\Http\Request $request Laravel request object
* @return \Illuminate\Http\Response Laravel response object containing the generated output
*/
public function indexAction( \Illuminate\Http\Request $request )
{
if( ( $path = $request->route( 'path', $request->input( 'path' ) ) ) === null ) {
abort( 404 );
}
$context = app( 'aimeos.context' )->get( true );
foreach( array_reverse( self::$fcn ) as $name => $fcn )
{
try {
return call_user_func_array( $fcn->bindTo( $this, static::class ), [$context, $path] );
} catch( \Exception $e ) {
if( $e->getCode() !== 404 ) throw $e;
}
}
abort( 404 );
}
/**
* Returns the category page if the give path can be resolved to a category.
*
* @param \Aimeos\MShop\ContextIface $context Context object
* @param string $path URL path to resolve
* @return Response Response object
*/
protected function catalog( \Aimeos\MShop\ContextIface $context, string $path ) : ?\Illuminate\Http\Response
{
$item = \Aimeos\Controller\Frontend::create( $context, 'catalog' )->resolve( $path );
$view = Shop::view();
$params = ( Route::current() ? Route::current()->parameters() : [] ) + Request::all();
$params += ['path' => $path, 'f_name' => $path, 'f_catid' => $item->getId(), 'page' => 'page-catalog-tree'];
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
$view->addHelper( 'param', $helper );
foreach( app( 'config' )->get( 'shop.page.catalog-tree' ) as $name )
{
$client = Shop::get( $name );
$params['aiheader'][$name] = $client->header();
$params['aibody'][$name] = $client->body();
}
return Response::view( Shop::template( 'catalog.tree' ), $params )
->header( 'Cache-Control', 'private, max-age=' . config( 'shop.cache_maxage', 30 ) );
}
/**
* Returns the CMS page if the give path can be resolved to a CMS page.
*
* @param \Aimeos\MShop\ContextIface $context Context object
* @param string $path URL path to resolve
* @return Response Response object
*/
protected function cms( \Aimeos\MShop\ContextIface $context, string $path ) : ?\Illuminate\Http\Response
{
\Aimeos\Controller\Frontend::create( $context, 'cms' )->resolve( $path );
$view = Shop::view();
$params = ( Route::current() ? Route::current()->parameters() : [] ) + Request::all();
$params += ['path' => $path, 'page' => 'page-index'];
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
$view->addHelper( 'param', $helper );
foreach( app( 'config' )->get( 'shop.page.cms' ) as $name )
{
$client = Shop::get( $name );
$params['aiheader'][$name] = $client->header();
$params['aibody'][$name] = $client->body();
}
return Response::view( Shop::template( 'page.index' ), $params )
->header( 'Cache-Control', 'private, max-age=' . config( 'shop.cache_maxage', 30 ) );
}
/**
* Returns the product page if the give path can be resolved to a product.
*
* @param \Aimeos\MShop\ContextIface $context Context object
* @param string $path URL path to resolve
* @return Response Response object
*/
protected function product( \Aimeos\MShop\ContextIface $context, string $path ) : ?\Illuminate\Http\Response
{
$item = \Aimeos\Controller\Frontend::create( $context, 'product' )->resolve( $path );
$view = Shop::view();
$params = ( Route::current() ? Route::current()->parameters() : [] ) + Request::all();
$params += ['path' => $path, 'd_name' => $path, 'd_prodid' => $item->getId(), 'page' => 'page-catalog-detail'];
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
$view->addHelper( 'param', $helper );
foreach( app( 'config' )->get( 'shop.page.catalog-detail' ) as $name )
{
$client = Shop::get( $name );
$params['aiheader'][$name] = $client->header();
$params['aibody'][$name] = $client->body();
}
return Response::view( Shop::template( 'catalog.detail' ), $params )
->header( 'Cache-Control', 'private, max-age=' . config( 'shop.cache_maxage', 30 ) );
}
}