Skip to content

Commit

Permalink
refactor: Code cleanup for Craft CMS 4
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Mar 14, 2022
1 parent 82aa57e commit 3b354f7
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 254 deletions.
48 changes: 35 additions & 13 deletions src/RouteMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@

namespace nystudio107\routemap;

use nystudio107\routemap\services\Routes as RoutesService;
use nystudio107\routemap\variables\RouteMapVariable;

use Craft;
use craft\base\Plugin;
use craft\base\Element;
use craft\base\Plugin;
use craft\elements\Entry;
use craft\events\ElementEvent;
use craft\events\RegisterCacheOptionsEvent;
use craft\services\Elements;
use craft\utilities\ClearCaches;
use craft\web\twig\variables\CraftVariable;

use nystudio107\routemap\services\Routes as RoutesService;
use nystudio107\routemap\variables\RouteMapVariable;
use yii\base\Event;

/**
Expand All @@ -36,35 +34,59 @@
*/
class RouteMap extends Plugin
{
// Static Properties
// Public Static Properties
// =========================================================================

/**
* @var ?RouteMap
*/
public static ?RouteMap $plugin = null;

// Public Properties
// =========================================================================

/**
* @var RouteMap
* @var string
*/
public string $schemaVersion = '1.0.0';

/**
* @var bool
*/
public static $plugin;
public bool $hasCpSection = false;

/**
* @var bool
*/
public static $craft31 = false;
public bool $hasCpSettings = false;

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function __construct($id, $parent = null, array $config = [])
{
$config['components'] = [
'routes' => RoutesService::class,
];

parent::__construct($id, $parent, $config);
}

/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
self::$plugin = $this;
self::$craft31 = version_compare(Craft::$app->getVersion(), '3.1', '>=');

Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $event): void {
static function (Event $event): void {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('routeMap', RouteMapVariable::class);
Expand All @@ -75,7 +97,7 @@ function (Event $event): void {
Event::on(
Elements::class,
Elements::EVENT_AFTER_SAVE_ELEMENT,
function (ElementEvent $event): void {
static function (ElementEvent $event): void {
Craft::debug(
'Elements::EVENT_AFTER_SAVE_ELEMENT',
__METHOD__
Expand Down Expand Up @@ -104,7 +126,7 @@ function (ElementEvent $event): void {
Event::on(
ClearCaches::class,
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
function (RegisterCacheOptionsEvent $event): void {
static function (RegisterCacheOptionsEvent $event): void {
$event->options[] = [
'key' => 'route-map',
'label' => Craft::t('route-map', 'Route Map Cache'),
Expand Down
68 changes: 32 additions & 36 deletions src/controllers/RoutesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

namespace nystudio107\routemap\controllers;

use nystudio107\routemap\RouteMap;

use craft\base\ElementInterface;
use craft\web\Controller;

use nystudio107\routemap\RouteMap;
use yii\web\Response;

/**
Expand Down Expand Up @@ -45,91 +43,88 @@ class RoutesController extends Controller
// =========================================================================
/**
* Return the public URLs for all elements that have URLs
*
* @param int|null $siteId
*
*/
public function actionGetAllUrls(array $criteria = [], $siteId = null): Response
public function actionGetAllUrls(array $criteria = [], ?int $siteId = null): Response
{
return $this->asJson(RouteMap::$plugin->routes->getAllUrls($criteria, $siteId));
}

/**
* Return the public URLs for a section
*
* @param int|null $siteId
*
*/
public function actionGetSectionUrls(string $section, array $criteria = [], $siteId = null): Response
public function actionGetSectionUrls(string $section, array $criteria = [], ?int $siteId = null): Response
{
return $this->asJson(RouteMap::$plugin->routes->getSectionUrls($section, $criteria, $siteId));
}

/**
* Return the public URLs for a category
*
* @param int|null $siteId
*
*/
public function actionGetCategoryUrls(string $category, array $criteria = [], $siteId = null): Response
public function actionGetCategoryUrls(string $category, array $criteria = [], ?int $siteId = null): Response
{
return $this->asJson(RouteMap::$plugin->routes->getCategoryUrls($category, $criteria, $siteId));
}

/**
* Return all of the section route rules
* Return all the section route rules
*
* @param string $format 'Craft'|'React'|'Vue'
* @param int|null $siteId
* @param ?int $siteId
* @return Response
*/
public function actionGetAllRouteRules(string $format = 'Craft', $siteId = null): Response
public function actionGetAllRouteRules(string $format = 'Craft', ?int $siteId = null): Response
{
return $this->asJson(RouteMap::$plugin->routes->getAllRouteRules($format, $siteId));
}

/**
* Return the route rules for a specific section
*
* @param string $section
* @param string $format 'Craft'|'React'|'Vue'
* @param int|null $siteId
*
* @param ?int $siteId
* @return Response
*/
public function actionGetSectionRouteRules(string $section, string $format = 'Craft', $siteId = null): Response
public function actionGetSectionRouteRules(string $section, string $format = 'Craft', ?int $siteId = null): Response
{
return $this->asJson(RouteMap::$plugin->routes->getSectionRouteRules($section, $format, $siteId));
}

/**
* Return the route rules for a specific category
*
* @param string $format 'Craft'|'React'|'Vue'
* @param int|null $siteId
*
* @param string $category
* @param string $format 'Craft'|'React'|'Vue'
* @param ?int $siteId
* @return Response
*/
public function actionGetCategoryRouteRules(string $category, string $format = 'Craft', $siteId = null): Response
public function actionGetCategoryRouteRules(string $category, string $format = 'Craft', ?int $siteId = null): Response
{
return $this->asJson(RouteMap::$plugin->routes->getCategoryRouteRules($category, $format, $siteId));
}

/**
* Return the Craft Control Panel and `routes.php` rules
*
* @param int|null $siteId
*
* @param ?int $siteId
* @param bool $includeGlobal
* @return Response
*/
public function actionGetRouteRules($siteId = null, bool $includeGlobal = true): Response
public function actionGetRouteRules(?int $siteId = null, bool $includeGlobal = true): Response
{
return $this->asJson(RouteMap::$plugin->routes->getRouteRules($siteId, $includeGlobal));
}

/**
* Get all of the assets of the type $assetTypes that are used in the Entry
* Get all the assets of the type $assetTypes that are used in the Entry
* that matches the $url
*
* @param int|null $siteId
*
* @param string $url
* @param array $assetTypes
* @param ?int $siteId
* @return Response
*/
public function actionGetUrlAssetUrls(string $url, array $assetTypes = ['image'], $siteId = null): Response
public function actionGetUrlAssetUrls(string $url, array $assetTypes = ['image'], ?int $siteId = null): Response
{
return $this->asJson(RouteMap::$plugin->routes->getUrlAssetUrls($url, $assetTypes, $siteId));
}
Expand All @@ -138,11 +133,12 @@ public function actionGetUrlAssetUrls(string $url, array $assetTypes = ['image']
* Returns all of the URLs for the given $elementType based on the passed in
* $criteria and $siteId
*
* @var string|ElementInterface $elementType
* @var array $criteria
* @var int|null $siteId
* @param string|ElementInterface $elementType
* @param array $criteria
* @param ?int $siteId
* @return Response
*/
public function actionGetElementUrls($elementType, array $criteria = [], $siteId = null): Response
public function actionGetElementUrls(string|ElementInterface $elementType, array $criteria = [], ?int $siteId = null): Response
{
return $this->asJson(RouteMap::$plugin->routes->getElementUrls($elementType, $criteria, $siteId));
}
Expand Down
21 changes: 12 additions & 9 deletions src/helpers/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use craft\base\ElementInterface;
use craft\base\Field as BaseField;
use craft\elements\MatrixBlock;
use craft\models\FieldLayout;
use yii\base\InvalidConfigException;

/**
Expand All @@ -26,17 +27,18 @@ class Field extends Component
// Static Methods
// =========================================================================
/**
* Return all of the fields in the $element of the type $fieldType class
* Return all the fields in the $element of the type $fieldType class
*
*
* @return mixed[]
* @param ElementInterface $element
* @param string $fieldType
* @return array
*/
public static function fieldsOfType(ElementInterface $element, string $fieldType): array
{
$foundFields = [];

$layout = $element->getFieldLayout();
if (!$layout instanceof \craft\models\FieldLayout) {
if (!$layout instanceof FieldLayout) {
return [];
}

Expand All @@ -52,12 +54,13 @@ public static function fieldsOfType(ElementInterface $element, string $fieldType
}

/**
* Return all of the fields in the $matrixBlock of the type $fieldType class
*
* Return all the fields in the $matrixBlock of the type $fieldType class
*
* @return string[]|null[]
* @param MatrixBlock $matrixBlock
* @param string $fieldType
* @return ?array
*/
public static function matrixFieldsOfType(MatrixBlock $matrixBlock, string $fieldType): array
public static function matrixFieldsOfType(MatrixBlock $matrixBlock, string $fieldType): ?array
{
$foundFields = [];

Expand All @@ -68,7 +71,7 @@ public static function matrixFieldsOfType(MatrixBlock $matrixBlock, string $fiel
}

if ($matrixBlockTypeModel !== null) {
$fields = $matrixBlockTypeModel->getFields();
$fields = $matrixBlockTypeModel->getCustomFields();
/** @var $field BaseField */
foreach ($fields as $field) {
if ($field instanceof $fieldType) {
Expand Down
Loading

0 comments on commit 3b354f7

Please sign in to comment.