Resource
class and imported
through the Res
class. The Resource
internal static files
url resolver class validates and saves urls within itself which can be imported later. The early validation of urls and resolving
of scripts pulls unecessary resources especially for scripts that may not be required within a web page. Spoova 2.6 introduces a new
url resolver and compiler class Ress
which is faster because it only validates specific local resources needed on
a webpage. Except the deprecated resources, most of the Resource
class methods relating to static resource
url storage and processing are also available on the Ress
class. These methods are listed below:
url()
method is used to set the static resource url similarly to the
Res::url()
method. An example is shown below
<?php Ress::url('res/file1.css') ->url('res/file2.js') ->url('http/site.com/some-link/file3.css');
new()
method is used to set a parent path for the url supplied.
This parent path will only be adopted by relative urls. This is shown below.
<?php Ress::new('res/') // set a parent path ->url('file1.css') # res/file1.css ->url('file2.js') # res/file2.js ->new('') // change parent path ->url('file3.css') # file3.css ->url('http/site.com/some-link/file3.css'); // not affected
new()
and the url()
method. The parent path will be added to only the relative urls.
urx()
method is used to escape the predefined parent path.
This is shown below:
<?php Ress::new('res/') // set a parent path ->url('file1.css') # res/file1.css ->url('file2.js') # res/file2.js ->urx('src/file3.css') # src/file3.css ->url('http/site.com/some-link/file3.css'); // not affected
"file1.css"
and file2.js
will inherit the parent path "res/"
,
the file3.css
will not be affected by the parent path. The urx()
method is similar to the Res::xurl()
method.
named()
method is used for naming urls similarly to the Res::named()
method. When declaring resources, it is important to define unique names for urls. This makes it easier to perform complex operations
like merging of resources.
<?php Ress::new('res/') ->url('file1.css') -> named('file1') ->url('file2.js') -> named('file2') ->urx('src/file3.css') -> named('file3') ->url('http/site.com/some-link/file3.css') -> named('linkfile3');
bind()
method binds the unique names of either urls or group of urls together into a
new unique name. An example of this is shown below:
<?php Ress::new('res/') ->url('file1.css') -> named('file1') ->url('file2.js') -> named('file2') ->urx('src/file3.css') -> named('file3') ->url('http/site.com/some-link/file3.css') -> named('linkfile3'); // bind "file1" and "file2" into "files" -> bind('files', ['file1', 'file2']) // bind "files" and "file3" into "all-files" -> bind('all-files', ['files', 'file3']) # bind "file1" and "file2" into "files"
"files"
contains both
"file1"
and
"file2"
while
"all-files"
will contain
"file1"
,
"file2"
and
"file3"
. The relative parent path of each url will be remembered during importation.
bindTo()
method is used for binding a list of currently declared urls together under a new group name. Once
this method is declared, the previous lists will be removed for a new url group listing. An example is shown below:
<?php Ress::new('res/') ->url('file1.css') -> named('file1') ->url('file2.js') -> named('file2') -> bindTo('files1') // bind file1, file2 ->urx('src/file3.css') -> named('file3') ->url('http/site.com/some-link/file3.css') -> named('link'); -> bindTo('files2') // bind file3, link
bindTo()
method is called
which automatically defines a new unique name for the list of previously declared urls while reseting the urls list.
The major advantage of bindTo()
over the bind()
method
is that it can combine new named urls into existing names and can also perform the role of bind()
method. An example is shown below
<?php Ress::new('res/') ->url('file1.css') -> named('file1') ->url('file2.js') -> named('file2') -> bindTo('files', ['file1', 'file2']) // bind file1, file2 ->urx('src/file3.css') -> named('file3') ->url('http/site.com/some-link/file3.css') -> named('link'); -> bindTo('files2') // bind file3, link
bindTo()
method is used to perform the
role of bind()
method. In this situation, the url listing is never being reset unless a single
argument is supplied as in the case of bindTo('files2')
. Also, unlike the
bindTo()
that can only be called once for a unique name, the
bindTo()
method can be used more than once. This is shown below:
<?php Ress::new('res/') ->url('file1.css') -> named('file1') ->url('file2.js') -> named('file2') -> bindTo('files', ['file1', 'file2']) // bind file1, file2 ->urx('src/file3.css') -> named('file3') ->url('http/site.com/some-link/file3.css') -> named('link'); -> bindTo('files', ['file3']) // add 'file3' to existing 'files'
Resource
and Ress
. It makes it easier to pull
a resource configuration file from any directory in the project application. Since static configuration files
are PHP files, they file extension names are not defined. The following is a sample configuration file expected to
be pulled.
<?php Res::new('res/') ->url('file1.css') -> named('file1') ->url('file2.js') -> named('file2') -> bindTo('files1') // bind file1, file2 ->urx('src/file3.css') -> named('file3') ->url('http/site.com/some-link/file3.css') -> named('link'); -> bindTo('files2') // bind file3, link
design.php
within an
src/
directory. The sample below shows how to pull these design rules.
<?php
namespace \spoova\mi\windows\Routes;
use Routes;
use Ress;
class Home extends Routes {
function __construct() {
Ress::pull('src.design'); // pull resource config file
}
}
src/design.php
.
Now, we can easily import these resources using the Ress::import()
method.
<?php namespace \spoova\mi\windows\Routes; use Routes; use Ress; class Home extends Routes { function __construct() { Ress::pull('src.design'); // pull sample resource config file $import = Ress::import(); // compile and import scripts } }
Ress::import()
is then used to compile and return the compiled scripts. Aside
from pulling from Ress
class declarations, we can also pull from a file returning
an array. This is shown below.
<?php return [ 'designCss' => 'res/css/design.css', 'designJs' => 'res/css/design.css', 'designs' => ['designCss','designJs'] ];
theme.php
was added into a windows/designs
directory. The array key in the
file above specifies the defined unique name of the resource file expected to be compiled while the value supplied is the relative file path
or external static resource link itself. The array key "designs"
is a unique
name that is used to bind "designCss"
and "designJs"
unique names together into a new group.
Now, we can import the resources by specifying the unique group to be imported. This is shown below:
<?php namespace \spoova\mi\windows\Routes; use Routes; use Ress; class Home extends Routes { function __construct() { Ress::pull('windows.design.theme'); // pull sample resource config file $importJs = Ress::import('designJs'); // compile 'designJs' and return script $importCss = Ress::import('designCss'); // compile 'designCss' and return script $importDesigns = Ress::import('designs'); // compile 'designs' and return scripts $importMulti = Ress::import(['designJs', 'designCss']); // compile multiple names and return scripts } }
$importDesigns
and $importMulti
above will return an empty value given the fact that they contain unique names that have already been imported earlier.
Also note that unlike Res::import()
class which uses the colon to specify groups to be imported,
the Ress::import()
uses the unique name explicity without any prefix colon.
res/res.php
as a top level static resource urls configuration file.
This means any resource pulled within that file will be available automatically in routes. This configuration was being handled
previously by the Res
class. However, starting from spoova version 2.6
,
the configuration is now being handled by the Ress
class. This new improvement is responsible for the addition of a
new bridge function import()
which is used import static resources based on the preferred resource
handler between the old Res
and the new Ress
classes. Unlike
Ress
which is specifically used for static resources, the old Res
class
is used to perform other functions which is why it is not entirely replaced. By default, the newly introduced import()
function uses the new Ress
class to import its resources and this is also reflected in the res/res.php
file. However, spoova still give options for developers to specify which resource handler class is expected to be used as default
by manually configuring the icore/init
file or by easily by running the command below from the command line which does the same thing.
> php mi initConfig RESOURCE_HANDLER Res
Res
class. Once, this is done, the res/res.php
should be manually updated to reflect these changes.
import()
or recall()
that is used to import static resource files and the introduction
of the import()
function makes it easier to import static resources easily regardless of
the resource handler classes preferred yet, in most cases when working within template files, the template directives
such as @import()
and @load()
will be used to import from
the resource handler chosen. The directive @import()
is discussed
here while the
@load()
directive is discussed here
here while the