crazko / statie-web

đź“– [deprecated] Documentation site for Statie - PHP Static Site Generator
2 stars 2 forks source link

Custom Generator Object Incorrect #44

Closed timconner closed 4 years ago

timconner commented 4 years ago

The documentation on statie.org for Generators says that you can create your own object file and the example shows the class extending Symplify\Statie\Renderable\File\AbstractFile.

However, when trying to do that, you will get this build error:

"MyWebsite\Statie" must inherit from "Symplify\Statie\Generator\Renderable\File\AbstractGeneratorFile"

I wasn't sure if this restriction was supposed to be imposed or not. If so, here is a working skeleton for a custom generator. It's basically the PostFile with some of the Post stuff stripped out.

<?php

declare(strict_types=1);

namespace MyWebsite\Statie;

use ArrayAccess;
use DateTimeInterface;
use Nette\Utils\ObjectHelpers;
use Symplify\Statie\Exception\Renderable\File\AccessKeyNotAvailableException;
use Symplify\Statie\Exception\Renderable\File\UnsupportedMethodException;
use Symplify\Statie\Generator\Renderable\File\AbstractGeneratorFile;

final class CustomFile extends AbstractGeneratorFile implements ArrayAccess
{
    /**
     * @return int|string|null
     */
    public function offsetGet($offset) {
        $this->ensureAccessExistingKey($offset);

        return $this->configuration[$offset];
    }

    public function offsetExists($offset): bool {
        return isset($this->configuration[$offset]);
    }

    public function offsetSet($offset, $value): void {
        throw new UnsupportedMethodException(__METHOD__ . ' is not supported');
    }

    public function offsetUnset($offset): void {
        throw new UnsupportedMethodException(__METHOD__ . ' is not supported');
    }

    private function ensureAccessExistingKey($offset): void {
        if (isset($this->configuration[$offset])) {
            return;
        }

        $availableKeys = array_keys($this->configuration);
        $suggestion = ObjectHelpers::getSuggestion($availableKeys, $offset);

        if ($suggestion) {
            $help = sprintf('Did you mean "%s"?', $suggestion);
        } else {
            $help = sprintf('Available keys are: "%s".', implode('", "', $availableKeys));
        }

        throw new AccessKeyNotAvailableException(sprintf(
            'Value "%s" was not found for "%s" object. %s',
            $offset,
            self::class,
            $help
        ));
    }
}
crazko commented 4 years ago

Thanks for pointing out this issue. Unfortunately, this documentation was not updated for quite a time, the site itself is created with old version of Statie, so there may be differences, as you noted. Could you create a PR with updated page about generators?

timconner commented 4 years ago

I might take that up. I am having some other issues with generators at the moment and need to work through that first.

crazko commented 4 years ago

Thanks.

crazko commented 4 years ago

Hi, author of Statie decided to shut down the project and replace it with a Symfony package:

Now I do not see a reason to put more effort into the documentation. I'll add notification on the web soon.