Skip to content

Commit

Permalink
[#72] Adds new parts kit directory parsing & config json action
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuapease committed Dec 22, 2023
1 parent 2210074 commit 4ed7c46
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
use craft\web\View;
use craft\web\twig\variables\CraftVariable;
use craft\helpers\ArrayHelper;
use craft\services\Plugins;
use craft\web\Application as CraftWebApplication;
use viget\base\Bundle;
use viget\base\controllers\PartsKitController;
use viget\base\twigextensions\Extension;
use viget\base\services\CpNav;
Expand All @@ -27,6 +25,7 @@

/**
* Yii Module for setting up custom Twig functionality to keep templates streamlined
* @property-read PartsKit $partsKit
*/
class Module extends \yii\base\Module implements BootstrapInterface
{
Expand Down
8 changes: 8 additions & 0 deletions src/controllers/PartsKitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ public function actionRedirectIndex(): Response

return $this->redirect($redirectUrl, 301);
}

public function actionConfig(): Response
{
return $this->asJson([
'schemaVersion' => '0.0.1',
'nav' => Module::getInstance()->partsKit->getNav(),
]);
}
}
24 changes: 24 additions & 0 deletions src/models/NavNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace viget\base\models;

class NavNode implements \JsonSerializable
{
public function __construct(
public string $title,
public string $url,
public string $path,
public array $children = [],
)
{}

public function jsonSerialize(): array
{
// Exclude path from JSON
return [
'title' => $this->title,
'url' => $this->url,
'children' => $this->children,
];
}
}
74 changes: 42 additions & 32 deletions src/services/PartsKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
use Craft;
use craft\helpers\StringHelper;
use craft\helpers\FileHelper;
use craft\helpers\UrlHelper;
use craft\helpers\Template as TemplateHelper;
use craft\helpers\ArrayHelper;
use craft\elements\Asset;
use Twig\Markup;

use viget\base\models\NavNode;
use viget\base\Module;

/**
Expand Down Expand Up @@ -46,48 +45,59 @@ public static function getConfig(string $key)
*/
public static function getNav(): array
{
$partsKitDir = self::getConfig('directory');

$templates = self::_getTemplates($partsKitDir);

if (empty($templates)) return [];
$templatesPath = Craft::$app->getPath()->getSiteTemplatesPath();
$partsPath = $templatesPath . '/' . 'parts-kit' . '/';

$items = [];
// Combine and sort all files & directories in the parts kit
$directories = \yii\helpers\FileHelper::findDirectories($partsPath);
$files = FileHelper::findFiles($partsPath);

foreach ($templates as $dir => $files) {
natcasesort($files);
$templates = [
...$directories,
...$files
];

$humanizedDir = self::_formatName($dir);
sort($templates);

$items[$humanizedDir] = [
'isActive' => false,
'items' => [],
];

foreach ($files as $file) {
$url = UrlHelper::siteUrl(implode('/', [
$partsKitDir,
$dir,
$file,
]));
/** @var NavNode[] $result */
$result = [];
// Loop through all the paths in the folder.
// Creating a NavNode object and putting the path to each node in the map.
foreach ($templates as $templatePath) {
$path = str_replace($partsPath, '', $templatePath);
$pathParts = explode('/', $path);
// TODO format name
$title = end($pathParts);

$requestUrl = UrlHelper::siteUrl(Craft::$app->request->url);
$result[$path] = new NavNode(
title: $title,
url: $path, // TODO only use URL if there's a file
path: $path,
);
}

$isActive = $url === $requestUrl;
// Reverse the array, so we can build up child nav nodes into their parents.
// We remove child nodes as they're added.
$result = array_reverse($result);

$items[$humanizedDir]['items'][] = [
'title' => self::_formatName($file),
'url' => $url,
'isActive' => $isActive,
];
foreach ($result as $node) {
$pathParts = explode('/', $node->path);
$parentPath = implode('/', array_slice($pathParts, 0, -1));
$parentNode = $result[$parentPath] ?? null;

if ($isActive) {
$items[$humanizedDir]['isActive'] = true;
}
if (!$parentNode) {
continue;
}

$parentNode->children[] = $node;
unset($result[$node->path]);
}

return $items;
// Sort by keys so they're in alpha order
ksort($result);

return array_values($result);
}

/**
Expand Down

0 comments on commit 4ed7c46

Please sign in to comment.