sf.php
- Access
- public. This is an open source software
conceived within the frame of the Institute of
Italian culture (NGO), and originally
implemented by Thomas de Vivo (tdvit@mail.com)
It is published under the following license:
everyone can copy, use, modify and redistribute this
software provided that every copy of it (including the
partial use of its code) will retain the current disclaimer
or in this form, or using the following short notice:
"(C) 2016 Institute of Italian culture (NGO)
-- www.istitutoculturaitaliana.org/sf"
(even compacted in one line)
(the provided link will contain a description of the software and the complete license.)
The software is provided "as is": without any warranty (including its eligibility for whatever kind of purpose), and both the copyright owners and the contributors are excluded from any liability related to the use of it, even if instructions and recommendations of use will be provided with such software or any derivative or related work. - Author
- Thomas de Vivo
- Copyright
- (C) 2016 Institute of Italian culture (NGO)
- Package
- SF (Simple Framework)
- See
- Version
- 0.1
Constants

string
__BASE_DIR__
= str_replace('\\', '/', realpath(dirname(__FILE__)))
- Package
- SF (Simple Framework)
- __BASE_DIR__
- string
__BASE_DIR__ The absolute path of the website root folder

string
__LEFT__
= true
- Package
- SF (Simple Framework)
- __LEFT__
- string
__LEFT__ Keyword used by the class Str to indicate the left part of a string by some reference token
Functions

__load__(string $scope, string $namespace, string $label, array $options = array()) : array
This is the function actually called from the outside in order to start all the rendering process.
Name | Type | Description |
---|---|---|
$scope | string | The page name (a directory within the folder "pages") |
$namespace | string | The "node" name (a directory within the folder "nodes") |
$label | string | The label inside the switch of the view enclosing the functions actually rendering it |
$options | array | An associative array containing more settings. For instance the version of the website, used to force the client to reload all the website's resources at every new version of it. |
Type | Description |
---|---|
array | Returns an associative array with the following keys: 'head' : the output portion to be inserted in the head section of the document 'body': the output portion to be inserted in the body section of the document 'script': the output portion to be inserted at the end of the body section of the document The head portion contains the resources to be made available to the client (mainly scripts and stylesheets, already codified in the proper html tags) The body section contains the content of the views as text/html The script portion contains inline scripts with the data to be made available to the client once the page is loaded |
- Package
- SF (Simple Framework)
- Todo
- Before $options we should have an associative array $values, with values optionally passed to the view
\Controller
Methods


get(string $scope, string $namespace, string $label, $values, $post = array()) : array
Returns the data interpolated by the given controller
Name | Type | Description |
---|---|---|
$scope | string | The scope: by now expected to be "pages" or "nodes": a "page" might contain multiple nodes) |
$namespace | string | The specific "page" or "node" (a subdirectory within the directory specified by "scope") |
$label | string | The label of the portion of code to be executed |
$values | ||
$post | array An associative array with the values eventually taken from a FORM |
Type | Description |
---|---|
array | Returns an associative array with the controller's output data (usually an interpolation of data taken from a database) |
\Loader
Properties


string $content = ''
The output as text/html to be used in the BODY section of the document
''
Details- Type
- string


array $data = array()
the values provided by the controller to be made available also to the client once that the page is loaded
array()
Details- Type
- array


array $files = array()
The scripts required by the application, dynamically updated while the views are loaded, and to be made available both server (as contents to be executed by the javascript interpreter) and client side
array()
Details- Type
- array


array $html_data = array()
The objects (associative arrays) associated to some HTML element's attributes: we allow to pass objects as values for HTML attributes, storing them in the global scope and then referencing to them through an ID stored as value of the HTML element itself. When the code is executed server side, we have to codify such values inside the page itself (for instance as JSON) and then restore them once the page is loaded.
array()
Details- Type
- array
Methods


__construct(array $files) : void
The class constructor. The class argument is a list of files required at first by the javascript interpreter (the list is dynamically updated at run-time)
Name | Type | Description |
---|---|---|
$files | array | A list of path of required files |


add_script( $scope, $namespace) : void
Other files to be added dynamically as soon as they are required by the javascript views interpreted server-side
Name | Type | Description |
---|---|---|
$scope | string The folder name within the directory "pages" | |
$namespace | string The folder name within the directory "nodes" |


get_content() : string
Returns the resulting text/html to be used in the BODY section of the document
Type | Description |
---|---|
string | Returns the overall text/html containing all the rendered / nested views of the HTML document |


get_resources() : array
All the resources, in terms of required scripts and data, required by the HTML document
Type | Description |
---|---|
array | Returns the resources actually required by the document, with the aim to make them available also client side |


load(string $scope, string $namespace, string $label) : void
This method is called by the __load__ function below in order to initialize the javascript interpreter with the required scripts, and to execute the javascript functions server-side
Name | Type | Description |
---|---|---|
$scope | string | The "page" name (a directory within the folder "pages") |
$namespace | string | The "node" name (a directory within the folder "nodes") |
$label | string | The label inside the switch of the view enclosing the functions actually rendering it |


record_data(string $context, array $obj) : void
This method is called by sf.js (when executed server-side) and records the results provided by the view's controllers, as well as the objects (associative arrays) passed as values of some HTML element's attribute.
Name | Type | Description |
---|---|---|
$context | string | Can be "data" or "html" (since v. 0.1) |
$obj | array | An associative array with the values to store |
- Todo
- The data's structure might not require a further "interface" or interpolation
\Str
Here are some examples:
example 1
$path = '/var/www/my_site/my_file.txt';
$contents = str($path)->get_contents();
gets the contents of the file located in $path
example 2
$path = '/var/www/my_site/my_file.txt';
$level = 2;
$path = str($path)->path_level_up($level)->get();
//returns '/var/www';
returns a string n levels up of $path
example 3
$path = '/var/www/my_site/my_file.js.txt';
$str = str($str)->rpart('.',__RIGHT__)->get();
//returns 'txt'
gets the right side of $path by the last occurrence of the token "."
example 4
$path = '/var/www/my_site/my_file.js.txt';
$str = str($str)->rpart('/',__RIGHT__)->part('.',__LEFT__)->get();
//returns 'my_file'
Properties


boolean $is_array = false
Reminds if the parameter passed to the constructor is an array, in order to provide the expected output when "get" method is called
false
Details- Type
- boolean
Methods


__construct(string | array $mixed) : \Str
The class constructor. We allow both single strings and array of strings as class argument (for instance because we want to perform similar operations on multiple strings)
Name | Type | Description |
---|---|---|
$mixed | string | array | A string or an array of strings |
Type | Description |
---|---|
\Str | Returns the class instance to allow method chaining |
- Fluent
- This method is part of a fluent interface and will return the same instance


get() : mixed
Gets the transformed class argument
Type | Description |
---|---|
mixed | A string of an array of strings depending on the value of $this->is_array |


get_contents() : string
Gets the content of the file path(s) corresponding to the class argument
Type | Description |
---|---|
string | Returns the file's contents |


part(string $tok, boolean $left = true, boolean $last = false) : \Str
Gets the left or right side of a string by a specified token
Name | Type | Description |
---|---|---|
$tok | string | The reference token |
$left | boolean | The left or right side ($left is false or the RIGHT constant is used) |
$last | boolean | Uses as reference the last occurrence of the token symbol |
Type | Description |
---|---|
\Str | Returns the class instance |
- Fluent
- This method is part of a fluent interface and will return the same instance


path_level_up(integer $n = 1) : \Str
Given a path string provided in this form /var/www/my_website/my_file.txt removes n levels from it
Name | Type | Description |
---|---|---|
$n | integer | The number of levels to go up |
Type | Description |
---|---|
\Str | Returns the class instance |
- Fluent
- This method is part of a fluent interface and will return the same instance
- Todo
- Add more controls where $n is greater than the array $array size


prefix(string $prefix) : \Str
Prepends a string (prefix) to the class argument
Name | Type | Description |
---|---|---|
$prefix | string | A string to prepend (add before) to every string of the class argument |
Type | Description |
---|---|
\Str | Returns the class instance |
- Fluent
- This method is part of a fluent interface and will return the same instance


replace(string $a, string $b) : \Str
Replaces one string with another (the subject is the class argument)
Name | Type | Description |
---|---|---|
$a | string | The string to search for |
$b | string | The replacement string |
Type | Description |
---|---|
\Str | Returns the class instance |
- Fluent
- This method is part of a fluent interface and will return the same instance


rpart(string $tok, $left = true) : \Str
Gets the left or right side of a string by the last occurrence of a specific token (this is a short call for the method above)
Name | Type | Description |
---|---|---|
$tok | string | The reference token |
$left |
Type | Description |
---|---|
\Str | Returns the class instance |
- Fluent
- This method is part of a fluent interface and will return the same instance


suffix(string $suffix) : \Str
Appends a string (suffix) to the class argument
Name | Type | Description |
---|---|---|
$suffix | string | A string to append (add after) to every string of the class argument |
Type | Description |
---|---|
\Str | Returns the class instance |
- Fluent
- This method is part of a fluent interface and will return the same instance