Source of file AllLinksProviderBase.php

Size: 2,174 Bytes - Last Modified: 2021-12-23T10:47:10+00:00

/var/www/docs.ssmods.com/process/src/src/Api/AllLinksProviderBase.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
<?php

namespace Sunnysideup\TemplateOverview\Api;

namespace Sunnysideup\TemplateOverview\Api;

use ReflectionClass;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;

abstract class AllLinksProviderBase
{
    use Extensible;
    use Injectable;
    use Configurable;

    protected $numberOfExamples = 1;

    private $listOfAllSiteTreeClassesCache = [];

    public function setNumberOfExamples($n): self
    {
        $this->numberOfExamples = $n;

        return $this;
    }

    public function getNumberOfExamples(): int
    {
        return $this->numberOfExamples;
    }

    /**
     * returns a list of all SiteTree Classes.
     *
     * @return array
     */
    public function getListOfAllClasses()
    {
        if (empty($this->listOfAllSiteTreeClassesCache)) {
            $siteTreeDetails = Injector::inst()->get(SiteTreeDetails::class);
            $list = $siteTreeDetails->ListOfAllClasses();
            foreach ($list as $page) {
                $this->listOfAllSiteTreeClassesCache[] = $page->ClassName;
            }
        }

        return $this->listOfAllSiteTreeClassesCache;
    }

    protected function isValidClass($class)
    {
        $obj = new ReflectionClass($class);

        return ! $obj->isAbstract();
    }

    /**
     * Takes an array, takes one item out, and returns new array.
     *
     * @param array  $array     array which will have an item taken out of it
     * @param string $exclusion Item to be taken out of $array
     *
     * @return array new array
     */
    protected function arrayExcept($array, $exclusion)
    {
        $newArray = $array;
        $count = count($newArray);
        for ($i = 0; $i < $count; ++$i) {
            if ($newArray[$i] === $exclusion) {
                unset($newArray[$i]);
            }
        }

        return $newArray;
    }

    protected function tableExists(string $table): bool
    {
        $tables = DB::table_list();

        return array_key_exists(strtolower($table), $tables);
    }
}