Issues (145)

src/Controller/ResolveController.php (3 issues)

1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2023
6
 */
7
8
9
namespace Aimeos\Shop\Controller;
10
11
use Aimeos\Shop\Facades\Shop;
12
use Illuminate\Routing\Controller;
13
use Illuminate\Support\Facades\Request;
14
use Illuminate\Support\Facades\Response;
15
use Illuminate\Support\Facades\Route;
16
use Illuminate\Support\Facades\View;
17
18
19
/**
20
 * Aimeos controller for dispatching requests.
21
 */
22
class ResolveController extends Controller
23
{
24
	private static $fcn = [];
25
26
27
	/**
28
	 * Register a new resolver function.
29
	 *
30
	 * @param string $name Name of the resolver function
31
	 * @param \Closure $fcn Resolver function
32
	 */
33
	public static function register( string $name, \Closure $fcn )
34
	{
35
		self::$fcn[$name] = $fcn;
36
	}
37
38
39
	/**
40
	 * Initializes the object.
41
	 */
42
	public function __construct()
43
	{
44
		self::$fcn['product'] = function( \Aimeos\MShop\ContextIface $context, string $path ) {
45
			return $this->product( $context, $path );
46
		};
47
48
		self::$fcn['catalog'] = function( \Aimeos\MShop\ContextIface $context, string $path ) {
49
			return $this->catalog( $context, $path );
50
		};
51
	}
52
53
54
	/**
55
	 * Returns the html of the resolved URLs.
56
	 *
57
	 * @param \Illuminate\Http\Request $request Laravel request object
58
	 * @return \Illuminate\Http\Response Laravel response object containing the generated output
59
	 */
60
	public function indexAction( \Illuminate\Http\Request $request )
61
	{
62
		if( ( $path = $request->route( 'path', $request->input( 'path' ) ) ) === null ) {
63
			abort( 404 );
64
		}
65
66
		$context = app( 'aimeos.context' )->get( true );
67
68
		foreach( array_reverse( self::$fcn ) as $name => $fcn )
69
		{
70
			try {
71
				return call_user_func_array( $fcn->bindTo( $this, static::class ), [$context, $path] );
72
			} catch( \Exception $e ) {
73
				if( $e->getCode() !== 404 ) throw $e;
74
			}
75
		}
76
77
		abort( 404 );
78
	}
79
80
81
	/**
82
	 * Returns the category page if the give path can be resolved to a category.
83
	 *
84
	 * @param \Aimeos\MShop\ContextIface $context Context object
85
	 * @param string $path URL path to resolve
86
	 * @return Response Response object
87
	 */
88
	protected function catalog( \Aimeos\MShop\ContextIface $context, string $path ) : ?\Illuminate\Http\Response
89
	{
90
		$item = \Aimeos\Controller\Frontend::create( $context, 'catalog' )->resolve( $path );
91
		$view = Shop::view();
92
93
		$params = ( Route::current() ? Route::current()->parameters() : [] ) + Request::all();
94
		$params += ['path' => $path, 'f_name' => $path, 'f_catid' => $item->getId(), 'page' => 'page-catalog-tree'];
95
96
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
97
		$view->addHelper( 'param', $helper );
98
99
		foreach( app( 'config' )->get( 'shop.page.catalog-tree' ) as $name )
100
		{
101
			$client = Shop::get( $name );
102
103
			$params['aiheader'][$name] = $client->header();
104
			$params['aibody'][$name] = $client->body();
105
		}
106
107
		return Response::view( Shop::template( 'catalog.tree' ), $params )
108
			->header( 'Cache-Control', 'private, max-age=' . config( 'shop.cache_maxage', 30 ) );
0 ignored issues
show
'shop.cache_maxage' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
			->header( 'Cache-Control', 'private, max-age=' . config( /** @scrutinizer ignore-type */ 'shop.cache_maxage', 30 ) );
Loading history...
109
	}
110
111
112
	/**
113
	 * Returns the CMS page if the give path can be resolved to a CMS page.
114
	 *
115
	 * @param \Aimeos\MShop\ContextIface $context Context object
116
	 * @param string $path URL path to resolve
117
	 * @return Response Response object
118
	 */
119
	protected function cms( \Aimeos\MShop\ContextIface $context, string $path ) : ?\Illuminate\Http\Response
120
	{
121
		\Aimeos\Controller\Frontend::create( $context, 'cms' )->resolve( $path );
122
		$view = Shop::view();
123
124
		$params = ( Route::current() ? Route::current()->parameters() : [] ) + Request::all();
125
		$params += ['path' => $path, 'page' => 'page-index'];
126
127
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
128
		$view->addHelper( 'param', $helper );
129
130
		foreach( app( 'config' )->get( 'shop.page.cms' ) as $name )
131
		{
132
			$client = Shop::get( $name );
133
134
			$params['aiheader'][$name] = $client->header();
135
			$params['aibody'][$name] = $client->body();
136
		}
137
138
		return Response::view( Shop::template( 'page.index' ), $params )
139
			->header( 'Cache-Control', 'private, max-age=' . config( 'shop.cache_maxage', 30 ) );
0 ignored issues
show
'shop.cache_maxage' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
			->header( 'Cache-Control', 'private, max-age=' . config( /** @scrutinizer ignore-type */ 'shop.cache_maxage', 30 ) );
Loading history...
140
	}
141
142
143
	/**
144
	 * Returns the product page if the give path can be resolved to a product.
145
	 *
146
	 * @param \Aimeos\MShop\ContextIface $context Context object
147
	 * @param string $path URL path to resolve
148
	 * @return Response Response object
149
	 */
150
	protected function product( \Aimeos\MShop\ContextIface $context, string $path ) : ?\Illuminate\Http\Response
151
	{
152
		$item = \Aimeos\Controller\Frontend::create( $context, 'product' )->resolve( $path );
153
		$view = Shop::view();
154
155
		$params = ( Route::current() ? Route::current()->parameters() : [] ) + Request::all();
156
		$params += ['path' => $path, 'd_name' => $path, 'd_prodid' => $item->getId(), 'page' => 'page-catalog-detail'];
157
158
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
159
		$view->addHelper( 'param', $helper );
160
161
		foreach( app( 'config' )->get( 'shop.page.catalog-detail' ) as $name )
162
		{
163
			$client = Shop::get( $name );
164
165
			$params['aiheader'][$name] = $client->header();
166
			$params['aibody'][$name] = $client->body();
167
		}
168
169
		return Response::view( Shop::template( 'catalog.detail' ), $params )
170
			->header( 'Cache-Control', 'private, max-age=' . config( 'shop.cache_maxage', 30 ) );
0 ignored issues
show
'shop.cache_maxage' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

170
			->header( 'Cache-Control', 'private, max-age=' . config( /** @scrutinizer ignore-type */ 'shop.cache_maxage', 30 ) );
Loading history...
171
	}
172
}