From 8d972caaf5013024f328c8eebc7a527705616ed2 Mon Sep 17 00:00:00 2001 From: Mosen Date: Fri, 5 Jan 2024 19:41:24 +1100 Subject: [PATCH] Fix App/Packages to work with v6 style modules, not tested. More optimisation to the docker build process. --- Dockerfile | 12 +++--- app/Packages.php | 12 ++++-- app/lib/munkireport/Modules.php | 66 ++++++++++++++++++++++++++------- build/docker-php-entrypoint | 2 +- build/php.d/upload.ini | 1 + docker-compose.wip.yml | 6 +-- 6 files changed, 72 insertions(+), 27 deletions(-) diff --git a/Dockerfile b/Dockerfile index 41dc54914..4f96a89d5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM node:lts as frontend COPY . /usr/src/app WORKDIR /usr/src/app -RUN npm install && NODE_ENV=production npm run build +RUN npm install && npm run build FROM php:8.3-apache LABEL maintainer="MunkiReport PHP Team " @@ -35,10 +35,9 @@ RUN apt-get update && \ RUN docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && \ docker-php-ext-install -j$(nproc) curl pdo_mysql soap ldap zip opcache -COPY . $APP_DIR -COPY --from=frontend /usr/src/app/public/ /var/munkireport/public/ +COPY --chown=www-data:www-data . $APP_DIR +COPY --chown=www-data:www-data --from=frontend /usr/src/app/public/ /var/munkireport/public/ WORKDIR $APP_DIR -RUN chown -R www-data:www-data $APP_DIR RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/000-default.conf RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf @@ -51,13 +50,14 @@ COPY build/docker-php-entrypoint /usr/local/bin/docker-php-entrypoint RUN cp "build/php.d/upload.ini" "$PHP_INI_DIR/conf.d/" COPY --from=composer /usr/bin/composer /usr/bin/composer -#RUN ./build/setup_composer.sh USER www-data RUN composer install --no-dev && \ - composer dumpautoload -o + composer dumpautoload -o && \ + composer clear-cache +# You should not use this directory for SQLite as Laravel defines one. However, it is provided for backwards compatibility. RUN mkdir -p app/db && \ touch app/db/db.sqlite diff --git a/app/Packages.php b/app/Packages.php index c7f5a088e..8d68cf1e7 100644 --- a/app/Packages.php +++ b/app/Packages.php @@ -33,7 +33,7 @@ public function __construct() // When invoked from the web application, Composer will assume public/ contains a composer.json // So, we need to hint the actual location of composer.json - $composer = Factory::create($io); + $composer = Factory::create($io, base_path('/composer.local.json')); $this->composer = $composer; $this->io = $io; @@ -79,7 +79,7 @@ public function index() * * @return array|null */ - public function modules() + public function modules(bool $includeLegacy = false) { $mr = $this->composer->getPackage(); @@ -99,8 +99,12 @@ public function modules() if (Arr::has($p->getExtra(), 'munkireport')) { $packages[] = $p; } else { - if (!Str::startsWith($p->getName(), 'munkireport/')) continue; - $packages[] = $p; + if ($includeLegacy) { + if (!Str::startsWith($p->getName(), 'munkireport/')) { + continue; + } + $packages[] = $p; + } } } } diff --git a/app/lib/munkireport/Modules.php b/app/lib/munkireport/Modules.php index 5db2f56bb..e34834e0b 100644 --- a/app/lib/munkireport/Modules.php +++ b/app/lib/munkireport/Modules.php @@ -2,6 +2,8 @@ namespace munkireport\lib; +use App\Packages; +use Illuminate\Foundation\PackageManifest; use Illuminate\Support\Arr; use Symfony\Component\Yaml\Yaml; @@ -16,22 +18,22 @@ class Modules /** * @var array The complete list of available modules with their listings, widgets, etc. */ - private $moduleList = []; + private array $moduleList = []; /** * @var array The complete list of available widgets (probably superseded by Widgets class) */ - private $widgetList = []; + private array $widgetList = []; /** * @var array A list of paths that may be scanned for v5 modules. */ - private $moduleSearchPaths = []; + private array $moduleSearchPaths = []; /** * @var string[] A list of modules that are always enabled effectively, without being declared. */ - private $allowedModules = [ + private array $allowedModules = [ 'machine', 'reportdata', 'tag', @@ -42,7 +44,7 @@ class Modules /** * @var bool When generating a list of modules, whether to skip modules not explicitly enabled. */ - private $skipInactiveModules = False; + private bool $skipInactiveModules = False; public function __construct() { @@ -79,7 +81,7 @@ public function __construct() * Widgets have been omitted because addModuleWidgets() always assumes views are in the same path as the module. * There is a function called addCoreWidgets() which adds them separately. */ - protected function addCoreModules() + protected function addCoreModules(): void { $this->moduleList['reportdata'] = [ 'detail_widgets' => [ @@ -181,7 +183,7 @@ protected function addCoreModules() * * @return array */ - public function getModuleSearchPaths() + public function getModuleSearchPaths(): array { return $this->moduleSearchPaths; } @@ -457,13 +459,18 @@ public function addWidgets(array &$widgetArray, ?array $detailWidgetList = []) /** * Get data to create dropdown nav * - * Change in v6.0 beta to support item data key `url` which replaces the auto generated path with a specific, absolute - * URL. + * As of v6, the URL to the target is not automatically always $base/$module/$item. + * Each module may provide an extra key as the link of the dropdown item: + * + * * `url` - an absolute URL to use for the link + * * `route` - a route alias to use for the link + * + * If both of these are missing, the behaviour falls back to v5 (the link is constructed using the module name) * * @param string $kind 'reports' or 'listings' * @param string $baseUrl 'show/report' or 'show/listing' * @param string $page current page url path - * @return array + * @return array An array of (object) that contain a `url`, `name`, `class` and `i18n` property, sorted alphabetically. */ public function getDropdownData(string $kind, string $baseUrl, string $page): array { @@ -476,11 +483,20 @@ public function getDropdownData(string $kind, string $baseUrl, string $page): ar if(isset($itemData['hide_from_menu']) && $itemData['hide_from_menu']){ continue; } - $i18n = isset($itemData['i18n']) ? $itemData['i18n'] : 'nav.' . $kind . '.' . $itemName; + $i18n = $itemData['i18n'] ?? 'nav.' . $kind . '.' . $itemName; + + if (isset($itemData['url'])) { + $url = $itemData['url']; + } else if (isset($itemData['route'])) { + $url = route($itemData['route']); + } else { + $url = mr_url($baseUrl.'/'.$module.'/'.$itemName); + } + $out[] = (object) [ - 'url' => isset($itemData['url']) ? $itemData['url'] : mr_url($baseUrl.'/'.$module.'/'.$itemName), + 'url' => $url, 'name' => $itemName, - 'class' => $page == $baseUrl.'/'.$module.'/'.$itemName ? 'active' : '', + 'class' => $page == $url ? 'active' : '', 'i18n' => $i18n, ]; } @@ -593,6 +609,30 @@ private function collectModuleInfo(array $modulePaths, bool $skipInactiveModules } } } + + $this->collectPackageInformation(); + } + + /** + * Collect information from all v6 modules which use the Laravel Package Auto-Discovery mechanism + * (composer.json extra.munkireport section) + * + * @return void + */ + private function collectPackageInformation() + { + $packages = new Packages(); + $modulePackages = $packages->modules(); + + foreach ($modulePackages as $mp) { + if (Arr::has($mp->getExtra(), 'munkireport')) { + $meta = $mp->getExtra()['munkireport']; + $this->moduleList[$mp->getName()] = [ + 'listings' => Arr::get($meta, 'navigation.listings', []), + 'path' => '' + ]; + } + } } /** diff --git a/build/docker-php-entrypoint b/build/docker-php-entrypoint index 726b74121..c74e2b1a3 100755 --- a/build/docker-php-entrypoint +++ b/build/docker-php-entrypoint @@ -61,7 +61,7 @@ function run_migrations() { function start_web() { echo "Caching configuration..." - (cd /var/munkireport && php please config:cache && php please route:cache) # && php please view:cache + (cd /var/munkireport && php please config:cache && php please route:cache && php please lighthouse:cache) # && php please view:cache if [[ -z "${APP_URL}" || "${APP_URL}" == "http://localhost:8080" ]]; then echo "APP_URL not set, or APP_URL set to default (http://localhost:8080). Please fix this otherwise you may be redirected to a nonexistent host" diff --git a/build/php.d/upload.ini b/build/php.d/upload.ini index da05b4352..05f6d27c1 100644 --- a/build/php.d/upload.ini +++ b/build/php.d/upload.ini @@ -1,2 +1,3 @@ +; Required so that clients can submit POST body with their entire inventory upload_max_filesize = 64M post_max_size = 64M diff --git a/docker-compose.wip.yml b/docker-compose.wip.yml index 6f78f42ff..3d7012b67 100644 --- a/docker-compose.wip.yml +++ b/docker-compose.wip.yml @@ -34,9 +34,9 @@ services: ports: - 8080:8080 # You should avoid privileged ports, here and in kubernetes volumes: - - ./munkireport-db/:/var/munkireport/app/db - - ./user//:/var/munkireport/local/users - + - ./.docker/db:/var/munkireport/app/db + - ./.docker/local:/var/munkireport/local + - ./.docker/storage:/var/munkireport/storage # Queue Worker (If you are using Queues) queue: image: munkireport-php:wip