/** * Recurses through the plugins directory creating Plugin objects for each plugin it finds. * * @return array|Plugin[] array of Plugin objects * @throws \RuntimeException */ public function init() { /** @var Config $config */ $config = self::getGrav()['config']; $plugins = (array) $config->get('plugins'); /** @var EventDispatcher $events */ $events = self::getGrav()['events']; foreach ($plugins as $plugin => $data) { if (empty($data['enabled'])) { // Only load enabled plugins. continue; } $locator = self::getGrav()['locator']; $filePath = $locator->findResource('plugins://' . $plugin . DS . $plugin . PLUGIN_EXT); if (!is_file($filePath)) { self::getGrav()['log']->addWarning(sprintf("Plugin '%s' enabled but not found! Try clearing cache with `bin/grav clear-cache`", $plugin)); continue; } require_once $filePath; $pluginClassFormat = ['Grav\\Plugin\\' . ucfirst($plugin) . 'Plugin', 'Grav\\Plugin\\' . Inflector::camelize($plugin) . 'Plugin']; $pluginClassName = false; foreach ($pluginClassFormat as $pluginClass) { if (class_exists($pluginClass)) { $pluginClassName = $pluginClass; break; } } if (false === $pluginClassName) { throw new \RuntimeException(sprintf("Plugin '%s' class not found! Try reinstalling this plugin.", $plugin)); } $instance = new $pluginClassName($plugin, self::getGrav(), $config); if ($instance instanceof EventSubscriberInterface) { $events->addSubscriber($instance); } } return $this->items; }
/** * Searches for a list of Packages in the repository * @param array $searches An array of either slugs or names * @return array Array of found Packages * Format: ['total' => int, 'not_found' => array, <found-slugs>] */ public function findPackages($searches = []) { $packages = ['total' => 0, 'not_found' => []]; foreach ($searches as $search) { $repository = ''; // if this is an object, get the search data from the key if (is_object($search)) { $search = (array) $search; $key = key($search); $repository = $search[$key]; $search = $key; } if ($found = $this->findPackage($search)) { // set override respository if provided if ($repository) { $found->override_repository = $repository; } if (!isset($packages[$found->package_type])) { $packages[$found->package_type] = []; } $packages[$found->package_type][$found->slug] = $found; $packages['total']++; } else { // make a best guess at the type based on the repo URL if (Utils::contains($repository, '-theme')) { $type = 'themes'; } else { $type = 'plugins'; } $not_found = new \stdClass(); $not_found->name = Inflector::camelize($search); $not_found->slug = $search; $not_found->package_type = $type; $not_found->install_path = str_replace('%name%', $search, $this->install_paths[$type]); $not_found->override_repository = $repository; $packages['not_found'][$search] = $not_found; } } return $packages; }
/** * Load current theme. * * @return Theme|object */ public function load() { // NOTE: ALL THE LOCAL VARIABLES ARE USED INSIDE INCLUDED FILE, DO NOT REMOVE THEM! $grav = $this->grav; $config = $this->config; $name = $this->current(); /** @var UniformResourceLocator $locator */ $locator = $grav['locator']; $file = $locator('theme://theme.php') ?: $locator("theme://{$name}.php"); if ($file) { // Local variables available in the file: $grav, $config, $name, $file $class = (include $file); if (!is_object($class)) { $themeClassFormat = ['Grav\\Theme\\' . ucfirst($name), 'Grav\\Theme\\' . Inflector::camelize($name)]; $themeClassName = false; foreach ($themeClassFormat as $themeClass) { if (class_exists($themeClass)) { $themeClassName = $themeClass; $class = new $themeClassName($grav, $config, $name); break; } } } } elseif (!$locator('theme://') && !defined('GRAV_CLI')) { exit("Theme '{$name}' does not exist, unable to display page."); } if (empty($class)) { $class = new Theme($grav, $config, $name); } return $class; }