ResourceSorter.php

<?php

namespace Lia\Test\Addon;

class ResourceSorter extends \Tlf\Tester {

    public function testSortJs(){

        $unsorted_files = [
            "/path/autowire.js",
            "/path/SimpleRequest.js",
            "/path/Bind.js",
            "/path/TagGroup/Search.js",
            "/path/TagGroup/Group.js",
            "/path/TagGroup/GroupItem.js",
            "/path/TagGroup/TagButton.js",
            "/path/TagGroup/Mode.js",

        ];

        $target = [
            "/path/Bind.js",
            "/path/SimpleRequest.js",
            "/path/autowire.js",
            "/path/TagGroup/Search.js",
            "/path/TagGroup/Group.js",
            "/path/TagGroup/Mode.js",
            "/path/TagGroup/GroupItem.js",
            "/path/TagGroup/TagButton.js",
        ];

        $sort_lists = [
            ["Bind.js"],
            ["autowire.js"], // autowire will go AFTER SimpleRequest because of the path/autowire listed below it
            ["SimpleRequest.js"],
            [
                "TagGroup/Search.js",
                "TagGroup/Group.js",
                "TagGroup/GroupItem.js",
                "TagGroup/TagButton.js",
                "TagGroup/Mode.js",
            ],
            [ "path/autowire.js", ], // autowire WOULD go after the TagGroups, BUT the TagGroups are overwritten by the following set, thus coming last
            [ // this set overwrites the earlier set because they're same-named and set AFTER
                "TagGroup/Search.js",
                "TagGroup/Group.js",
                "TagGroup/Mode.js",
                "TagGroup/GroupItem.js",
                "TagGroup/TagButton.js",
            ],
        ];

        $lia = new \Lia();
        $package = new \Lia\Package($lia, 'test:sorter');
        $res = new \Lia\Addon\Resources($package);
        $sorter = new \Lia\Addon\ResourceSorter($package);

        foreach ($sort_lists as $list){
            $sorter->setResourceOrder('js', $list, true, false);
        }

        $sorted = $sorter->getSortedFiles('js', $unsorted_files);

        $this->compare($target, array_values($sorted));

    }

    public function testSortCss(){
        $lia = new \Lia();
        $package = new \Lia\Package($lia, 'test:sorter');
        $res = new \Lia\Addon\Resources($package);
        $sorter = new \Lia\Addon\ResourceSorter($package);

        $files = [
            '/path/three.css',
            '/path/five.css',
            '/path/two.css',
            '/path/four.css',
            '/path/one.css',
            '/path/six.css',
        ];

        // @export_start(ResourceSorter.RelativeCssSort)
        \Lia\Addon\ResourceSorter::from($lia)
            ->setResourceOrder(
                $extension='css', // js for javascript files
                $relative_file_paths= // an array of relative file paths, in the order you want them. There MUST NOT be leading `/`. There MAY be as many `/` as you like
                    ['one.css', 
                    'two.css', 
                    'three.css', 
                    'four.css',
                    ],
                    $prepend_all=true, // TRUE puts these names BEFORE unsorted files, false puts them after.
                $prepend_sorted=false // TRUE puts these names BEFORE other SORTED files, false puts them after.
            );
        // @export_end(ResourceSorter.RelativeCssSort)

        $resources = $sorter->getSortedFiles('css', $files);

        $target = [
            '/path/one.css',
            '/path/two.css',
            '/path/three.css',
            '/path/four.css',
            '/path/five.css',
            '/path/six.css',
        ];
        
        $this->test('Sorts files correctly');
        $this->compare_arrays($target, $resources);
        $this->test('Has unsorted files, too');
        $this->compare(6, count($resources));
        
    }

    public function dir(){
        return $this->cli->pwd.'/test/input/ResourceSorter/';
    }

}