Source of file AllLinksProviderBase.php

Size: 2,041 Bytes - Last Modified: 2021-03-22T11:05:29+00:00

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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
<?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;

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 getListOfAllSiteTreeClasses()
    {
        if (empty($this->listOfAllSiteTreeClassesCache)) {
            $siteTreeDetails = Injector::inst()->get(SiteTreeDetails::class);
            $list = $siteTreeDetails->ListOfAllSiteTreeClasses();
            foreach ($list as $page) {
                $this->listOfAllSiteTreeClassesCache[] = $page->ClassName;
            }
        }
        return $this->listOfAllSiteTreeClassesCache;
    }

    protected function isValidClass($class)
    {
        $obj = new ReflectionClass($class);
        if ($obj->isAbstract()) {
            return false;
        }
        return true;
    }

    /**
     * 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;
    }
}