sf.phpView Source

Show: PublicProtectedPrivateinherited
Table of Contents
This is the main server-side file of Sf, an isomorphic, recursive and minimalistic client/server side framerwork to easily develop modular and light-fast web applications.
The file also contains some utilities to easily performs operations on strings or arrays using method chaining.
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

>VConstantstring  __BASE_DIR__ = str_replace('\\', '/', realpath(dirname(__FILE__)))
Package
SF (Simple Framework)  
__BASE_DIR__
string

__BASE_DIR__ The absolute path of the website root folder

>VConstantstring  __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

>VConstantstring  __RIGHT__ = false
Package
SF (Simple Framework)  
__RIGHT__
string

__RIGHT__ Keyword used by the class Str to indicate the right part of a string by some reference token

Functions

function__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.

In order to render the entire document (which is represented as views encapsulated into each other) we have to render the most external view first.
Parameters
NameTypeDescription
$scopestring

The page name (a directory within the folder "pages")

$namespacestring

The "node" name (a directory within the folder "nodes")

$labelstring

The label inside the switch of the view enclosing the functions actually rendering it

$optionsarray

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.

Returns
TypeDescription
arrayReturns 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
Details
Package
SF (Simple Framework)  
Todo
Before $options we should have an associative array $values, with values optionally passed to the view  
functionstr(string $str) : void

This is a short for Str() without using the 'new' operator

Parameters
NameTypeDescription
$strstring

The string to be passed as class argument of Str

Details
Package
SF (Simple Framework)  

\Controller

Package: SF (Simple Framework)
A simple interface to query the controller related to a specific resource through Ajax

Methods

methodpublicget(string $scope, string $namespace, string $label,  $values,  $post = array()) : array

Returns the data interpolated by the given controller

Parameters
NameTypeDescription
$scopestring

The scope: by now expected to be "pages" or "nodes": a "page" might contain multiple nodes)

$namespacestring

The specific "page" or "node" (a subdirectory within the directory specified by "scope")

$labelstring

The label of the portion of code to be executed

$values
$post

array An associative array with the values eventually taken from a FORM

Returns
TypeDescription
arrayReturns an associative array with the controller's output data (usually an interpolation of data taken from a database)

\Loader

Package: SF (Simple Framework)
A class providing an interface with the server side javascript interpreter, and collecting all the required resources, to be made available both server and client side.

Properties

>VPropertyprivatestring $content = ''

The output as text/html to be used in the BODY section of the document

Default value''Details
Type
string
>VPropertyprivatearray $data = array()

the values provided by the controller to be made available also to the client once that the page is loaded

Default valuearray()Details
Type
array
>VPropertyprivatearray $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

Default valuearray()Details
Type
array
>VPropertyprivatearray $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.

Default valuearray()Details
Type
array
>VPropertypublicobject $v8 = null

The v8 instance taken from the outside

Default valuenullDetails
Type
object

Methods

methodpublic__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)

Parameters
NameTypeDescription
$filesarray

A list of path of required files

methodpublicadd_script( $scope,  $namespace) : void

Other files to be added dynamically as soon as they are required by the javascript views interpreted server-side

Parameters
NameTypeDescription
$scope

string The folder name within the directory "pages"

$namespace

string The folder name within the directory "nodes"

methodpublicget_content() : string

Returns the resulting text/html to be used in the BODY section of the document

Returns
TypeDescription
stringReturns the overall text/html containing all the rendered / nested views of the HTML document
methodpublicget_resources() : array

All the resources, in terms of required scripts and data, required by the HTML document

Returns
TypeDescription
arrayReturns the resources actually required by the document, with the aim to make them available also client side
methodpublicload(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

The method might be called recursively depending on the server-side javascript implementation (see below, method "add_script")
Parameters
NameTypeDescription
$scopestring

The "page" name (a directory within the folder "pages")

$namespacestring

The "node" name (a directory within the folder "nodes")

$labelstring

The label inside the switch of the view enclosing the functions actually rendering it

methodpublicrecord_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.

Parameters
NameTypeDescription
$contextstring

Can be "data" or "html" (since v. 0.1)

$objarray

An associative array with the values to store

Details
Todo
The data's structure might not require a further "interface" or interpolation  

\Str

Package: SF (Simple Framework)
An interface to easily handle strings using method chaining (fluent interface) and to groups string related functions.
The class is called through the function below str() to avoid the use of the 'new' operator.

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

>VPropertyprivateboolean $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

Default valuefalseDetails
Type
boolean
>VPropertyprivatearray $str_array = array()

An array of strings. This is the main argument of the class, assigned by the constructor. Hereinafter called "class argument"

Default valuearray()Details
Type
array

Methods

methodpublic__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)

Parameters
NameTypeDescription
$mixedstring | array

A string or an array of strings

Returns
TypeDescription
\StrReturns the class instance to allow method chaining
Details
Fluent
This method is part of a fluent interface and will return the same instance  
methodpublicget() : mixed

Gets the transformed class argument

Returns
TypeDescription
mixedA string of an array of strings depending on the value of $this->is_array
methodpublicget_contents() : string

Gets the content of the file path(s) corresponding to the class argument

Returns
TypeDescription
stringReturns the file's contents
methodpublicpart(string $tok, boolean $left = true, boolean $last = false) : \Str

Gets the left or right side of a string by a specified token

Parameters
NameTypeDescription
$tokstring

The reference token

$leftboolean

The left or right side ($left is false or the RIGHT constant is used)

$lastboolean

Uses as reference the last occurrence of the token symbol

Returns
TypeDescription
\StrReturns the class instance
Details
Fluent
This method is part of a fluent interface and will return the same instance  
methodpublicpath_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

Removes from every string of the class argument n items separated by the token '/' starting from the right. The method is used to get a path which is n levels up by a given path
Parameters
NameTypeDescription
$ninteger

The number of levels to go up

Returns
TypeDescription
\StrReturns the class instance
Details
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  
methodpublicprefix(string $prefix) : \Str

Prepends a string (prefix) to the class argument

Parameters
NameTypeDescription
$prefixstring

A string to prepend (add before) to every string of the class argument

Returns
TypeDescription
\StrReturns the class instance
Details
Fluent
This method is part of a fluent interface and will return the same instance  
methodpublicreplace(string $a, string $b) : \Str

Replaces one string with another (the subject is the class argument)

Parameters
NameTypeDescription
$astring

The string to search for

$bstring

The replacement string

Returns
TypeDescription
\StrReturns the class instance
Details
Fluent
This method is part of a fluent interface and will return the same instance  
methodpublicrpart(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)

Parameters
NameTypeDescription
$tokstring

The reference token

$left
Returns
TypeDescription
\StrReturns the class instance
Details
Fluent
This method is part of a fluent interface and will return the same instance  
methodpublicsuffix(string $suffix) : \Str

Appends a string (suffix) to the class argument

Parameters
NameTypeDescription
$suffixstring

A string to append (add after) to every string of the class argument

Returns
TypeDescription
\StrReturns the class instance
Details
Fluent
This method is part of a fluent interface and will return the same instance  
Documentation was generated by phpDocumentor 2.8.5.