File: D:/HostingSpaces/SBogers79/artofeinstein.be/app/Komma/Seo/SitemapService.php
<?php
namespace Komma\Seo;
use Komma\Routes\RouteRepository;
use Roumen\Sitemap\Sitemap;
use Carbon\Carbon;
class SitemapService
{
/**
* @var RouteRepository
*/
private $routeRepository;
/**
* SitemapService constructor.
* @param RouteRepository $routeRepository
*/
public function __construct(RouteRepository $routeRepository)
{
$this->routeRepository = $routeRepository;
}
/***
* This method will generate the sitemap based on the routes table
* It will collect the routes based on the given groups array.
* Then it will build the sitemap grouped by the linked id
*
* @param array $groups , which routes we want in the sitemap
* @param float $weight_from / the minimum weight of the sitemap items
* @return Sitemap object
*/
public function getSitemap(
$groups = ['pages'],
$weight_from = 0.1)
{
//Create the sitemap class
$sitemap = \App::make('sitemap');
//Loop trough the routes based on the given parameters
foreach ($this->routes($groups, $weight_from) as $type => $route)
{
$sitemap->add(
\URL::to($route['route']),
Carbon::now(),
$route['weight'],
'monthly',
[],
$route['name']);
}
return $sitemap;
}
/**
* Return all (cleaned up) routes
*
* @param array $groups
* @return array
*/
public function routes($groups = ['pages'], $minWeight = 0.1)
{
// Get all routes from database
$routes = $this->routeRepository->routesForSitemap($minWeight);
// Clean up route structure
$cleanRoutes = [];
foreach ($routes as $route)
{
// Get pages
if ($route->routable_type == 'Komma\Kms\Pages\Models\PageTranslation' && in_array('pages', $groups))
{
$cleanRoutes['page-' . $route->page_id] = $this->cleanRoute($route, 'page');
}
}
return $cleanRoutes;
}
/**
* Return only necessary properties
*
* @param $route
* @param $prefix
* @return array
*/
private function cleanRoute($route, $prefix)
{
return [
'route' => $route->route,
'weight' => $route->sitemap_weight,
'name' => $route->{$prefix . '_name'}
];
}
}