Example #1
0
 /**
  * @return array
  */
 public static function getStyles()
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     $files = Folder::all('gantry-themes://', ['recursive' => false]);
     $list = array();
     foreach ($files as $theme) {
         if (file_exists($locator('gantry-themes://' . $theme . '/default/gantry/theme.yaml'))) {
             $details = new ThemeDetails($theme);
             if (!$locator->schemeExists('gantry-theme-' . $theme)) {
                 $locator->addPath('gantry-themes-' . $theme, '', $details->getPaths());
             }
             $details['name'] = $theme;
             $details['title'] = ucfirst($theme);
             $details['preview_url'] = '/' . $theme;
             $details['admin_url'] = '/' . $theme . '/admin';
             $details['params'] = [];
             $list[$details->name] = $details;
         }
     }
     // Add Thumbnails links.
     foreach ($list as $details) {
         $details['thumbnail'] = self::getImage($locator, $details, 'thumbnail');
     }
     return $list;
 }
Example #2
0
 /**
  * @return array
  */
 public static function getThemes()
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     $files = Folder::all('gantry-themes://', ['recursive' => false]);
     /** @var array|ThemeDetails[] $list */
     $list = [];
     ksort($files);
     foreach ($files as $theme) {
         if (!is_dir($theme)) {
             continue;
         }
         if ($locator('gantry-themes://' . $theme . '/gantry/theme.yaml')) {
             $details = new ThemeDetails($theme);
             // Stream needs to be valid URL.
             $streamName = 'gantry-themes-' . preg_replace('|[^a-z\\d+.-]|ui', '-', $theme);
             if (!$locator->schemeExists($streamName)) {
                 $locator->addPath($streamName, '', $details->getPaths());
             }
             $details['name'] = $theme;
             $details['title'] = $details['details.name'];
             $details['preview_url'] = $locator('gantry-themes://' . $theme);
             // FIXME:
             $details['admin_url'] = 'FIXME';
             $details['params'] = [];
             $list[$details->name] = $details;
         }
     }
     // Add Thumbnails links after adding all the paths to the locator.
     foreach ($list as $details) {
         $details['thumbnail'] = $details->getUrl("details.images.thumbnail");
     }
     return $list;
 }
Example #3
0
 /**
  * @return array
  */
 public static function getThemes()
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     $files = Folder::all('gantry-themes://', ['recursive' => false]);
     /** @var array|ThemeDetails[] $list */
     $list = [];
     ksort($files);
     foreach ($files as $theme) {
         if (file_exists(PRIME_ROOT . '/themes/' . $theme . '/gantry/theme.yaml')) {
             $details = new ThemeDetails($theme);
             if (!$locator->schemeExists('gantry-theme-' . $theme)) {
                 $locator->addPath('gantry-themes-' . $theme, '', $details->getPaths());
             }
             $details['name'] = $theme;
             $details['title'] = $details['details.name'];
             $details['preview_url'] = rtrim(PRIME_URI, '/') . '/' . $theme;
             $details['admin_url'] = rtrim(PRIME_URI, '/') . '/' . $theme . '/admin/configurations/styles';
             $details['params'] = [];
             $list[$details->name] = $details;
         }
     }
     // Add Thumbnails links after adding all the paths to the locator.
     foreach ($list as $details) {
         $details['thumbnail'] = $details->getUrl("details.images.thumbnail");
     }
     return $list;
 }
Example #4
0
 /**
  * @param string $in    Filename without path or extension.
  * @param string $out   Full path to the file to be written.
  * @return bool         True if the output file was saved.
  */
 public function compileFile($in, $out = null)
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     if (!$out) {
         $out = $locator->findResource($this->getCssUrl($in), true, true);
     }
     $paths = $locator->mergeResources($this->paths);
     // Set the lookup paths.
     $this->compiler->setBasePath($out);
     $this->compiler->setImportPaths($paths);
     $this->compiler->setFormatter('scss_formatter_nested');
     // Run the compiler.
     $this->compiler->setVariables($this->getVariables());
     $scss = '@import "' . $in . '.scss"';
     $css = $this->compiler->compile($scss);
     if (strpos($css, $scss) === 0) {
         $css = '/* ' . $scss . ' */';
     }
     $file = File::instance($out);
     // Attempt to lock the file for writing.
     $file->lock(false);
     //TODO: Better way to handle double writing files at same time.
     if ($file->locked() === false) {
         // File was already locked by another process.
         return false;
     }
     $file->save($css);
     $file->unlock();
     return true;
 }
Example #5
0
 /**
  * @param string $in    Filename without path or extension.
  * @return bool         True if the output file was saved.
  */
 public function compileFile($in)
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     $out = $this->getCssUrl($in);
     $path = $locator->findResource($out, true, true);
     $paths = $locator->mergeResources($this->paths);
     // Set the lookup paths.
     $this->compiler->setBasePath($path);
     $this->compiler->setImportDir($paths);
     $this->compiler->setFormatter('lessjs');
     // Run the compiler.
     $this->compiler->setVariables($this->getVariables());
     $css = $this->compiler->compileFile($in . '.less"');
     $file = File::instance($path);
     // Attempt to lock the file for writing.
     $file->lock(false);
     //TODO: Better way to handle double writing files at same time.
     if ($file->locked() === false) {
         // File was already locked by another process.
         return false;
     }
     $file->save($css);
     $file->unlock();
     $this->createMeta($out, md5($css));
     return true;
 }
Example #6
0
 /**
  * @throws \LogicException
  */
 protected static function load()
 {
     $container = parent::load();
     // Use locator from Grav.
     $container['locator'] = function ($c) {
         return Grav::instance()['locator'];
     };
     return $container;
 }
Example #7
0
 /**
  * @return Gantry
  * @throws \LogicException
  */
 protected static function load()
 {
     // Make sure Timber plugin has been loaded.
     if (!class_exists('Timber')) {
         $action = 'install-plugin';
         $slug = 'timber-library';
         throw new \LogicException('<strong>Timber not activated</strong>. Click <a href="' . wp_nonce_url(add_query_arg(['action' => $action, 'plugin' => $slug], admin_url('update.php')), $action . '_' . $slug) . '"><strong>here</strong></a> to install it or go to the <a href=" ' . admin_url('plugins.php#timber') . '"><strong>Installed Plugins</strong></a> page to activate it, if already installed.');
     }
     return parent::load();
 }
Example #8
0
 public function __construct($theme)
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     $this->items = CompiledYamlFile::instance($locator("gantry-themes://{$theme}/gantry/theme.yaml"))->content();
     $this->offsetSet('name', $theme);
     $parent = $this->offsetGet('configuration.theme.parent') ?: $theme;
     $this->offsetSet('parent', $theme !== $parent ? $parent : null);
 }
Example #9
0
    /**
     * @param string $in    Filename without path or extension.
     * @return bool         True if the output file was saved.
     */
    public function compileFile($in)
    {
        // Buy some extra time as compilation may take a lot of time in shared environments.
        @set_time_limit(30);
        ob_start();
        $gantry = Gantry::instance();
        /** @var UniformResourceLocator $locator */
        $locator = $gantry['locator'];
        $out = $this->getCssUrl($in);
        $path = $locator->findResource($out, true, true);
        $file = File::instance($path);
        // Attempt to lock the file for writing.
        try {
            $file->lock(false);
        } catch (\Exception $e) {
            // Another process has locked the file; we will check this in a bit.
        }
        if ($file->locked() === false) {
            // File was already locked by another process, lets avoid compiling the same file twice.
            return false;
        }
        $paths = $locator->mergeResources($this->paths);
        // Set the lookup paths.
        $this->compiler->setBasePath($path);
        $this->compiler->setImportDir($paths);
        // Run the compiler.
        $this->compiler->setVariables($this->getVariables());
        $css = $this->compiler->compileFile($in . '.less"');
        $warnings = trim(ob_get_clean());
        if ($warnings) {
            $this->warnings[$in] = explode("\n", $warnings);
        }
        if (!$this->production) {
            $warning = <<<WARN
/* GANTRY5 DEVELOPMENT MODE ENABLED.

   WARNING: This file is automatically generated by Gantry5. Any modifications to this file will be lost!

   For more information on modifying CSS, please read:

   http://docs.gantry.org/gantry5/configure/styles
   http://docs.gantry.org/gantry5/tutorials/adding-a-custom-style-sheet
 */
WARN;
            $css = $warning . "\n\n" . $css;
        }
        $file->save($css);
        $file->unlock();
        $file->free();
        $this->createMeta($out, md5($css));
        return true;
    }
Example #10
0
 public function onLayoutSave(Event $event)
 {
     $gantry = Gantry::instance();
     /** @var Configurations $configurations */
     $configurations = $gantry['configurations'];
     $list = [];
     foreach ($configurations as $name => $title) {
         $list += Layout::instance($name)->positions();
     }
     $manifest = new Manifest($gantry['theme.name']);
     $manifest->setPositions(array_keys($list));
     $manifest->save();
 }
Example #11
0
 /**
  * @throws \LogicException
  */
 protected static function load()
 {
     $container = parent::load();
     $container['site'] = function ($c) {
         return new Site();
     };
     $container['menu'] = function ($c) {
         return new Menu();
     };
     $container['global'] = function ($c) {
         return new Config([]);
     };
     return $container;
 }
Example #12
0
 public function __construct($theme)
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     $filename = $locator->findResource("gantry-themes://{$theme}/gantry/theme.yaml");
     $cache = $locator->findResource("gantry-cache://{$theme}/compiled/yaml", true, true);
     $file = CompiledYamlFile::instance($filename);
     $this->items = $file->setCachePath($cache)->content();
     $this->offsetSet('name', $theme);
     $file->free();
     $parent = $this->offsetGet('configuration.theme.parent') ?: $theme;
     $this->offsetSet('parent', $theme !== $parent ? $parent : null);
 }
Example #13
0
 public function display($group, $id = null)
 {
     $configuration = $this->params['configuration'];
     $particle = $this->container['content']->get("{$group}/{$id}");
     $blueprints = new BlueprintsForm($particle);
     $prefix = "content.{$group}.{$id}";
     if ($configuration == 'default') {
         $this->params['overrideable'] = false;
     } else {
         $this->params['defaults'] = $this->container['defaults']->get($prefix);
         $this->params['overrideable'] = true;
     }
     $this->params += ['particle' => $blueprints, 'data' => Gantry::instance()['config']->get($prefix), 'id' => "{$group}.{$id}", 'parent' => "configurations/{$this->params['configuration']}/content", 'route' => "configurations.{$this->params['configuration']}.content.{$prefix}", 'skip' => ['enabled']];
     return $this->container['admin.theme']->render('@gantry-admin/pages/configurations/content/item.html.twig', $this->params);
 }
Example #14
0
 /**
  * Create new theme details.
  *
  * @param string $theme
  */
 public function __construct($theme)
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     $filename = $locator->findResource("gantry-themes://{$theme}/gantry/theme.yaml");
     if (!$filename) {
         throw new \RuntimeException(sprintf('Theme %s not found', $theme), 404);
     }
     $cache = $locator->findResource("gantry-cache://{$theme}/compiled/yaml", true, true);
     $file = CompiledYamlFile::instance($filename);
     $this->items = $file->setCachePath($cache)->content();
     $file->free();
     $this->offsetSet('name', $theme);
     $parent = (string) $this->offsetGet('configuration.theme.parent', $theme);
     $parent = $parent != $theme ? $parent : null;
     $this->offsetSet('parent', $parent);
 }
Example #15
0
 /**
  * @throws \LogicException
  */
 protected static function load()
 {
     // Make sure Timber plugin has been loaded.
     if (!class_exists('Timber')) {
         throw new \LogicException('Timber not activated. Make sure you activate the plugin in <a href="' . admin_url('plugins.php#timber') . '">' . admin_url('plugins.php') . '</a>');
     }
     $container = parent::load();
     $container['site'] = function ($c) {
         return new Site();
     };
     $container['page'] = function ($c) {
         return new Page($c);
     };
     $container['global'] = function ($c) {
         return new Config([]);
     };
     return $container;
 }
Example #16
0
 /**
  * @throws \LogicException
  */
 protected static function load()
 {
     $container = parent::load();
     // Use locator from Grav.
     $container['locator'] = function ($c) {
         return Grav::instance()['locator'];
     };
     $container['site'] = function ($c) {
         return new Site();
     };
     $container['menu'] = function ($c) {
         return new Menu();
     };
     $container['page'] = function ($c) {
         return new Page($c);
     };
     $container['global'] = function ($c) {
         return new Config([]);
     };
     return $container;
 }
Example #17
0
 /**
  * @throws \LogicException
  */
 protected static function load()
 {
     // Make sure Timber plugin has been loaded.
     if (!class_exists('Timber')) {
         $action = 'install-plugin';
         $slug = 'timber-library';
         throw new \LogicException('<strong>Timber not activated</strong>. Click <a href="' . wp_nonce_url(add_query_arg(['action' => $action, 'plugin' => $slug], admin_url('update.php')), $action . '_' . $slug) . '"><strong>here</strong></a> to install it or go to the <a href=" ' . admin_url('plugins.php#timber') . '"><strong>Installed Plugins</strong></a> page to activate it, if already installed.');
     }
     $container = parent::load();
     $container['site'] = function ($c) {
         return new Site();
     };
     $container['page'] = function ($c) {
         return new Page($c);
     };
     $container['menu'] = function ($c) {
         return new Menu();
     };
     $container['global'] = function ($c) {
         return new Config((array) \get_option('gantry5_plugin'));
     };
     return $container;
 }
Example #18
0
 /**
  * Save compiled file.
  *
  * @param  string  $filename
  * @throws \RuntimeException
  * @internal
  */
 protected function saveCompiledFile($filename)
 {
     $gantry = Gantry::instance();
     /** @var Config $global */
     $global = $gantry['global'];
     if (!$global->get('compile_yaml', 1)) {
         return;
     }
     $file = PhpFile::instance($filename);
     // Attempt to lock the file for writing.
     try {
         $file->lock(false);
     } catch (\Exception $e) {
         // Another process has locked the file; we will check this in a bit.
     }
     if ($file->locked() === false) {
         // File was already locked by another process.
         return;
     }
     $cache = ['@class' => get_class($this), 'timestamp' => time(), 'checksum' => $this->checksum(), 'files' => $this->files, 'data' => $this->object->toArray()];
     $file->save($cache);
     $file->unlock();
     $file->free();
     $this->modified();
 }
Example #19
0
 /**
  * @internal
  */
 public function findImport($url)
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     // Ignore vanilla css and external requests.
     if (preg_match('/\\.css$|^https?:\\/\\//', $url)) {
         return null;
     }
     // Try both normal and the _partial filename.
     $files = array($url, preg_replace('/[^\\/]+$/', '_\\0', $url));
     foreach ($this->paths as $base) {
         foreach ($files as $file) {
             if (!preg_match('|\\.scss$|', $file)) {
                 $file .= '.scss';
             }
             if ($locator->findResource($base . '/' . $file)) {
                 return $base . '/' . $file;
             }
         }
     }
     return null;
 }
Example #20
0
 /**
  * @param string $name
  * @param array $paths
  * @return string
  */
 protected function addStream($name, $paths)
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     /** @var Streams $streams */
     $streams = $gantry['streams'];
     // Add theme stream.
     $streamName = 'gantry-themes-' . preg_replace('|[^a-z\\d+.-]|ui', '-', $name);
     if (!$locator->schemeExists($streamName)) {
         $streams->add([$streamName => ['paths' => $paths]]);
     }
     return $streamName;
 }