xref: /template/strap/ComboStrap/IFetcher.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1*04fd306cSNickeau<?php
2*04fd306cSNickeau
3*04fd306cSNickeaunamespace ComboStrap;
4*04fd306cSNickeau
5*04fd306cSNickeauuse ComboStrap\Web\Url;
6*04fd306cSNickeau
7*04fd306cSNickeau/**
8*04fd306cSNickeau *
9*04fd306cSNickeau *
10*04fd306cSNickeau * A class that returns
11*04fd306cSNickeau *
12*04fd306cSNickeau * It represents a fetch of:
13*04fd306cSNickeau *   * a {@link IFetcherPath::getFetchPath() file}
14*04fd306cSNickeau *   * or {@link IFetcherString::getFetchString() string}
15*04fd306cSNickeau *
16*04fd306cSNickeau * Example
17*04fd306cSNickeau *   * serving a svg file with different request properties (if the requested image width is set, it will generate a new svg from the source)
18*04fd306cSNickeau *
19*04fd306cSNickeau * The request may come from:
20*04fd306cSNickeau *   * a {@link IFetcher::buildFromUrl() URL}
21*04fd306cSNickeau *   * a {@link IFetcher::buildFromTagAttributes() attributes}
22*04fd306cSNickeau *
23*04fd306cSNickeau * TODO: The handler/fetcher should also be able to be call in a process function
24*04fd306cSNickeau *   passing the the {@link ExecutionContext} and pass it the result (a string or a file or whatever)
25*04fd306cSNickeau *   you get then a functional interface (Then we don't need to know the returned type
26*04fd306cSNickeau *   and the interface {@link IFetcherPath} and {@link IFetcherString} are not needed
27*04fd306cSNickeau *   we could also add a function that could cast the result
28*04fd306cSNickeau *
29*04fd306cSNickeau * The hierarchy is {@link Mime} based.
30*04fd306cSNickeau *
31*04fd306cSNickeau * Example:
32*04fd306cSNickeau *   * {@link FetcherTraitImage} such as:
33*04fd306cSNickeau *     * {@link FetcherSvg} that can process and return svg
34*04fd306cSNickeau *     * {@link FetcherRaster} that can process and return raster image
35*04fd306cSNickeau *     * {@link FetcherVignette} that returns a raster image from page metadata
36*04fd306cSNickeau *     * {@link FetcherScreenshot} that returns a snapshot image from a page
37*04fd306cSNickeau *   * {@link FetcherRawLocalPath} that returns all type of local file without processing
38*04fd306cSNickeau *
39*04fd306cSNickeau */
40*04fd306cSNickeauinterface IFetcher
41*04fd306cSNickeau{
42*04fd306cSNickeau    /**
43*04fd306cSNickeau     * buster got the same value
44*04fd306cSNickeau     * that the `rev` attribute (ie mtime)
45*04fd306cSNickeau     * We don't use rev as cache buster because Dokuwiki still thinks
46*04fd306cSNickeau     * that this is an old file and search in the attic
47*04fd306cSNickeau     * as seen in the function {@link mediaFN()}
48*04fd306cSNickeau     *
49*04fd306cSNickeau     * The value used by Dokuwiki for the buster is tseed.
50*04fd306cSNickeau     */
51*04fd306cSNickeau    public const CACHE_BUSTER_KEY = "tseed";
52*04fd306cSNickeau
53*04fd306cSNickeau    /**
54*04fd306cSNickeau     * The property in the URL that identify the fetcher
55*04fd306cSNickeau     */
56*04fd306cSNickeau    public const FETCHER_KEY = "fetcher";
57*04fd306cSNickeau
58*04fd306cSNickeau
59*04fd306cSNickeau    /**
60*04fd306cSNickeau     * Return the URL where the resource can be fetched
61*04fd306cSNickeau     * @param Url|null $url - the url to be able to pass along in the hierarchy
62*04fd306cSNickeau     * @return Url
63*04fd306cSNickeau     */
64*04fd306cSNickeau    function getFetchUrl(Url $url = null): Url;
65*04fd306cSNickeau
66*04fd306cSNickeau
67*04fd306cSNickeau    /**
68*04fd306cSNickeau     * The buster that should be added to the url.
69*04fd306cSNickeau     * @return string
70*04fd306cSNickeau     * @throws ExceptionNotFound - if the buster cannot be calculated (file does not exist for instance)
71*04fd306cSNickeau     */
72*04fd306cSNickeau    function getBuster(): string;
73*04fd306cSNickeau
74*04fd306cSNickeau
75*04fd306cSNickeau    /**
76*04fd306cSNickeau     * @return Mime - the mime of the output
77*04fd306cSNickeau     *
78*04fd306cSNickeau     * You can also ask it via {@link IFetcher::getFetchPath()} but it will
79*04fd306cSNickeau     * perform the processing. If you want to create a cache file path with the good extension
80*04fd306cSNickeau     * this is the way to go.
81*04fd306cSNickeau     */
82*04fd306cSNickeau    public function getMime(): Mime;
83*04fd306cSNickeau
84*04fd306cSNickeau    /**
85*04fd306cSNickeau     * A convenient way to build a fetcher from a URL
86*04fd306cSNickeau     * This method calls the function {@link IFetcher::buildFromTagAttributes()}
87*04fd306cSNickeau     * @param Url $url
88*04fd306cSNickeau     * @return IFetcher
89*04fd306cSNickeau     */
90*04fd306cSNickeau    public function buildFromUrl(Url $url): IFetcher;
91*04fd306cSNickeau
92*04fd306cSNickeau    /**
93*04fd306cSNickeau     * @param TagAttributes $tagAttributes - the attributes
94*04fd306cSNickeau     * @return IFetcher
95*04fd306cSNickeau     */
96*04fd306cSNickeau    public function buildFromTagAttributes(TagAttributes $tagAttributes): IFetcher;
97*04fd306cSNickeau
98*04fd306cSNickeau    /**
99*04fd306cSNickeau     * Get the cache value requested
100*04fd306cSNickeau     * @throws ExceptionNotFound
101*04fd306cSNickeau     * @return string
102*04fd306cSNickeau     */
103*04fd306cSNickeau    public function getRequestedCache(): string;
104*04fd306cSNickeau
105*04fd306cSNickeau    /**
106*04fd306cSNickeau     * @return LocalPath - the cache if any
107*04fd306cSNickeau     * @throws ExceptionNotSupported
108*04fd306cSNickeau     */
109*04fd306cSNickeau    public function getContentCachePath(): LocalPath;
110*04fd306cSNickeau
111*04fd306cSNickeau    /**
112*04fd306cSNickeau     * @return string - an unique name that is added in the fetcher key of the URL
113*04fd306cSNickeau     *
114*04fd306cSNickeau     * Note that because dokuwiki does a sanitizing on the do custom action.
115*04fd306cSNickeau     * The name should not have any space or separator (What fuck up is fucked up)
116*04fd306cSNickeau     */
117*04fd306cSNickeau    public function getFetcherName(): string;
118*04fd306cSNickeau
119*04fd306cSNickeau
120*04fd306cSNickeau    /**
121*04fd306cSNickeau     * @return IFetcher - process and feed the cache
122*04fd306cSNickeau     * @throws ExceptionNotSupported - if the cache is not supported
123*04fd306cSNickeau     */
124*04fd306cSNickeau    public function process(): IFetcher;
125*04fd306cSNickeau
126*04fd306cSNickeau    /**
127*04fd306cSNickeau     * @return string - a label to the resource returned (used in img tag, ...)
128*04fd306cSNickeau     *
129*04fd306cSNickeau     * If the resource is:
130*04fd306cSNickeau     * - based on a local path, it may be the path name
131*04fd306cSNickeau     * - generated from a page, it may be the page title
132*04fd306cSNickeau     * ...
133*04fd306cSNickeau     */
134*04fd306cSNickeau    public function getLabel(): string;
135*04fd306cSNickeau
136*04fd306cSNickeau
137*04fd306cSNickeau}
138