Completed
Push — 3.3.x ( 37d736...41194d )
by Erwan
02:32
created

categories_loader::load()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 33
rs 8.8571
c 2
b 0
f 0
cc 2
eloc 21
nc 2
nop 2
1
<?php
2
/**
3
*
4
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\routing;
12
13
use Symfony\Component\Config\Loader\Loader;
14
use Symfony\Component\Routing\Route;
15
use Symfony\Component\Routing\RouteCollection;
16
17
/**
18
* Loads routes defined in directory_cats table.
19
*/
20
class categories_loader extends Loader
21
{
22
	/** @var \phpbb\db\driver\driver_interface */
23
	protected $db;
24
25
	/** @var string */
26
	protected $categories_table;
27
28
	/**
29
	* Constructor
30
	*
31
	* @param \phpbb\db\driver\driver_interface    $db          		Database connection
32
	* @param string                               $categories_table Table name
33
	* @access public
34
	*/
35
	public function __construct(\phpbb\db\driver\driver_interface $db, $categories_table)
36
	{
37
		$this->db          		= $db;
38
		$this->categories_table = $categories_table;
39
	}
40
41
	/**
42
	* {@inheritdoc}
43
	*
44
	* @api
45
	*/
46
	public function load($resource, $type = null)
47
	{
48
		$routes = new RouteCollection();
49
50
		$defaults = array(
51
			'_controller'	=> 'ernadoo.phpbbdirectory.controller.categories:view_route',
52
			'page'			=> 1,
53
			'sort_days'		=> 0,
54
			'sort_key'		=> '',
55
			'sort_dir'		=> '',
56
		);
57
58
		$requirements = array(
59
			'page'		=> '\d+',
60
			'sort_days'	=> '\d+',
61
		);
62
63
		$sql = 'SELECT cat_id, cat_route
64
			FROM ' . $this->categories_table;
65
		$result = $this->db->sql_query($sql);
66
67
		while ($row = $this->db->sql_fetchrow($result))
68
		{
69
			$defaults['cat_id'] = $row['cat_id'];
70
			$path = 'directory/' . $row['cat_route'] . '/{page}/{sort_days}/{sort_key}/{sort_dir}';
71
72
			$route = new Route($path, $defaults, $requirements);
73
			$routes->add('ernadoo_phpbbdirectory_dynamic_route_' . $row['cat_id'], $route);
74
		}
75
		$this->db->sql_freeresult();
76
77
		return $routes;
78
	}
79
80
	/**
81
	* {@inheritdoc}
82
	*
83
	* @api
84
	*/
85
	public function supports($resource, $type = null)
86
	{
87
		return $type === 'ernadoo_phpbbdirectory_route';
88
	}
89
}
90