Recommend this page to a friend! |
Download .zip |
Info | Documentation | View files (65) | Download .zip | Reputation | Support forum (1) | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2014-12-13 (1 year ago) | Not enough user ratings | Total: 265 | All time: 7,269 This week: 579 |
Version | License | PHP version | Categories | |||
ssa 1.0.4 | The PHP License | 5.0 | PHP 5, AJAX |
Description | Author | |
This package can be used to call PHP classes from JavaScript on Web pages. Recommendations Restful Web service |
SSA is a framework to simply perform Ajax call. You can call your service as PHP into you javascript.
The usage of ssa is very simple, you create your PHP service, and you can call this service in javascript code. For example : HelloWorld.php
namespace services\;
/
* simple example service
*
* @author deblock
*/
class HelloWorld {
/
* return Hello <yourName> !!
* @param string $yourName
* @return string
*/
public function helloYou($yourName) {
return 'Hello ' . $yourName.' !!';
}
}
example.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- include ssa core javascript -->
<script type="text/javascript" src="javascript/ssa.js" ></script>
<!-- include the autogenerate javascript service -->
<script type="text/javascript" src="javascript.php?service=HelloWorld"></script>
<script type="text/javascript">
HelloWorld.helloYou('deblockt').done(function(result){
document.getElementById('serviceResult').innerHTML = result;
});
</script>
</head>
<body>
<div> SSA exemple </div>
<div id="serviceResult"></div>
</body>
</html>
This exemple add "Hello deblock !!" into the serviceResult div.
For convert the service into Javascript service ssa use doc comment. It's use the @param annotation for know parameters and type parameters. If a php parameter have no comment, they will not be export into javascrit service.
For run the service, @param annotations are used, the type is use for convert $_POST parameters into PHP type. Type support can be primitive, complete class name (with namespace), \DateTime(inputFormat, file or array. \DateTime and array are specific. \DateTime type have parameter input format. Example \DateTime(m/d/Y) array can have parameter. Example array(int) array(int) array(\Path\To\My\Class) array(file) ...
Javascript service have multiple method for handle ajax event. fail* : If a network error occurs always* : Already run after ajax call phpError* : Run if a php error occurs. (if the ssa mode is debug, the php error are logged) done* : Run when the service call is a success.
ssa.js has two default handler : defaultFailHandler* : default handler used if no specific handler are specified. It can be overrided by fail handler. defaultPhpErrorHandler* : default handler used if no specific handler are specified. It can be overrided by phpError handler.
_serviceTest.js_
myService.myAction('firstParameter', {attr1 : 'value1'})
.done(function(returnValue, xhr) { alert(returnValue);})
.fail(function(xhr) {alter('a network error occurs');})
.phpError(function(errorPhp, xhr) {alter('an error occurs'.errorPhp.message);})
.always(function(xhr){console.log('fin de la requĂȘte')});
each callback have the same object context, you can pass variable between this callbacks.
Ssa support multiple type parameter, and return value. Parameters and return value can be, primitive type, object or DateTime, array, you can simply add an other type support.
Ssa support file uploaded, if you want upload a file you must use the file, or array(file) types.
service.php
class FileService {
/
* @param file $file
*/
public function upload($file) {
// file is like this
// array(
// 'name' => 'string',
// 'size' => int,
// 'error' => int,
// 'tmp_name' => 'string',
// 'type' => 'string'
// )
}
/
* @param array(file) $files
*/
public function uploadMultiple($files) {
foreach ($files as $file) {
$this->upload($file);
}
}
}
FileUpload.js
// upload one file
FileService.upload(document.getElementById('simpleFileUploadInput').files);
// upload multiple file
FileService.uploadMultipleFile(document.getElementById('multipleFileUploadInput').files);
Warning the file uploaded is not support by all navigator, it use FormData class. The method ssa.supportFileUpload return true if the navigator support this function. When you run a service with file upload the callback formDataError is call is this function is not supported.
FileService.upload(document.getElementById('simpleFileUploadInput').files)
.formDataError(function(){
alert('Your navigator is too old for this function');
});
Ssa support multiple javascript framework : - you can use ssa standolone only on include ssa.js file - you can use ssa with angular js you just need to include ssa.js file and your service generated file. After use injection dependencies for get your service.
for exemple :
// add ssa as module dependencies
var controller = angular.module('ssa.test', ['ssa']);
// get simply your service on your controller with the service name. here the service is helloWorldService
controller.controller('controller', function($scope, helloWorldService){
});
- you can use ssa with requirejs you just need to include ssa on your configuration of requirejs.
// configuration must containe a ssa link
require.config({
paths: {
// you must add ssa srcipt on your configuration
"ssa": "path/to/ssa/javascript/file",
// warning if you don't use htaccess service param is like this /serviceName
// if you use htacess you can have url like this /javascript/ssa/service/servicename.js who redirect on javascript.php
// path of your javscript service generator
"ssaService" : "javascript.php?type=requirejs&service="
}
});
// juste require your service
require( ["ssaService/helloWorldService"],
function(helloWorldService) {
// helloWorldService is you php service
}
);
If you want see ssa exemple you can look on test/ssa/toEndTest directory
The ssa configuration is different between standalone version, and the symfony version.
For symfony version you can look this bundle project
The documentation of the Standalone version is under.
The simple way for register your service is to create a configuration.php files. this file call the serviceManager for register your own services.
configuration.php
include 'autoload.php';
use ssa\ServiceManager;
// the first way to do this is with registerAllServices method
ServiceManager::getInstance()->registerAllServices(array(
// the first service is the class ssa\test\Service1 and we expose all this method
'service1' => array(
'class' => 'ssa\test\Service1'
),
// the second service is the class ssa\test\Service2 and we expose only the action1 and action2 method
'service2' => array(
'class' => 'ssa\test\Service2',
'methods' => array('action1','action2')
)
));
// the second way to do this is the registerService function, you can only register one service
ServiceManager::getInstance()->registerService('service3','ssa\test\Service3');
// or
ServiceManager::getInstance()->registerService('service4','ssa\test\Service4', array('action1'));
SSA can be configured, the configuration can be in the configuration.php file.
configuration.php
include 'autoload.php';
use ssa\Configuration;
Configuration::getInstance()->configure(array(
'debug' => true, // if debug is true the generated javascript file are not minimized
'cacheMode' => 'file', // if the cache is configured this cache mode is use, this can be file,apc,memcache,no(default)
'cacheDirectory' => '', // if the cacheMode is file, this parameter is mandatory, this is the directory where the cache is put
'memcacheHost' => '', // if the cacheMode is memcache is set this parameter is mandatory
'memcachePort' => ''// if the cacheMode is memcache is set this parameter is mandatory
));
// the configuration can be do like this, example
Configuration::getInstance()->setDebug(true);
Configuration::getInstance()->setCacheMode('file');
/service register/
If default type support of ssa is not suffisant, you can define your own type support. You have two way to do this, add simple a type support (for object, or for parameter), or create a new ParameterResolver who resolve primitive and object. A default ParameterResolver exists, it can resolve primitiveType, array, object, and \DataTime.
The first method to add a type resolver is add directly into the default type resolver. _configuration.php_
/ register services, and configure ssa */
use ssa\runner\resolver\impl\DefaultParameterResolver;
$defaultParameter = DefaultParameterResolver::createDefaultParameterResolver();
$defaultParameter->addObjectResolver(new MyObjectResolver());
$defaultParameter->addPrimitiveResolver(new MyPrimitiveResolver());
Your own ObjectResolver and your ParameterResolver need impements ssa\runner\resolver\ObjectResolver, ssa\runner\resolver\PrimitiveResolver. see documentation of PrimitiveResolver and ObjectResolver.
Or you can create your own ParameterResolver (not recommended). your ParameterResolver need implement ssa\runner\resolver\ParameterResolver.
run.php
include 'configuration.php';
use ssa\runner\ServiceRunner;
// get the service and the action : HelloWorld.sayHello
list($service, $action) = explode('.', $_GET['service']);
// create the service runner
$serviceRunner = new ServiceRunner($service, new MyParameterResolver());
// run the action with get parameters
echo $serviceRunner->runAction($action, $_GET);
The encoder is use for encode your function return into javascipt value. The default encoder is the JsonEncoder, this encoder can convert primitive type, array, and object. Objects are convert with getter methods, each getter is convert into a JSON property. If you need you can create your own encoder, you can do this, it's simple. On your service action you need to add @Encoder annotation. Service.php
class Service {
/
* @Encoder(\MyEncoder)
*
* @param string $firstParameter
*
* @return string
*/
public function action($firstParameter) {
return $firstParameter;
}
}
The action method use MyEncoder for convert the return on JSON or other format. Your encoder must implements ssa\runner\converter\Encoder, or extends ssa\runner\converter\DefaultJsonEncoder.
Il you want use SSA you have many solution.
An other project allow to simply add this project into your symfony project. see ssa/symfony.
The standalone version is enable.
The first solution is to use a project with composer, you can just add ssa/core dependencies.
If you don't want use composer, you can add ssa into you project.
`
php
function __autoload($className) {
if (strpos($className, 'ssa\') == 0) {
$file = str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php';
include $YOUR_INSTALATION_DIRECTORY.DIRECTORY_SEPARATOR.$file;
}
}
`
For use the standalone version of SSA you need to use two php file, one to create javascript service, one to run the php controller. this two php files, need to use configuration php file for register your services.
configuration.php
include 'autoload.php';
use ssa\ServiceManager;
use ssa\Configuration;
// configure the ssa framework
Configuration::getInstance()->configure(array(
'debug' => true
));
// registrer your services
ServiceManager::getInstance()->registerAllServices(array(
'HelloWorld' => array('class' => 'ssa\toEndTest\HelloWorld')
));
run.php
include 'configuration.php';
use ssa\runner\ServiceRunner;
// get the service and the action : HelloWorld.sayHello
list($service, $action) = explode('.', $_GET['service']);
// create the service runner
$serviceRunner = new ServiceRunner($service);
// run the action with get parameters
echo $serviceRunner->runAction($action, array_merge($_POST, $_FILES));
javascript.php
include 'configuration.php';
use ssa\converter\JavascriptConverter;
use ssa\converter\SimpleUrlFactory;
// url use to call php services
$url = substr($_SERVER['REQUEST_URI'],0, strrpos($_SERVER['REQUEST_URI'], '/'));
// url factory use for create the service run url (your run.php file)
$factory = new SimpleUrlFactory("http://$_SERVER[HTTP_HOST]$url/run.php?service={action}&test=true");
// create the converter convert the service into Javascript service, service use $_GET parameter
$converter = new JavascriptConverter($_GET['service'], $factory);
echo $converter->convert();
for example for get javascript HelloWorld service the url is _http://localhost/javascript.php?service=HelloWorld_
finaly you need include ssa.js into you javascript folder.
Files |
File | Role | Description | ||
---|---|---|---|---|
javascript (1 file) | ||||
src (1 directory) | ||||
test (2 files, 1 directory) | ||||
.gitignore | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
README.md | Doc. | Auxiliary data |
Files | / | src | / | ssa |
File | Role | Description | ||
---|---|---|---|---|
annotation (1 file) | ||||
converter (6 files) | ||||
runner (5 files, 2 directories) | ||||
util (1 file) | ||||
Configuration.php | Class | Class source | ||
ServiceManager.php | Class | Class source | ||
ServiceMetadata.php | Class | Class source | ||
ServiceNotRegistredException.php | Class | Class source |
Files | / | src | / | ssa | / | converter |
File | Role | Description |
---|---|---|
AngularJavascriptConverter.php | Class | Class source |
JavascriptConverter.php | Class | Class source |
RequireJsJavascriptConverter.php | Class | Class source |
ServiceConverter.php | Class | Class source |
SimpleUrlFactory.php | Class | Class source |
UrlFactory.php | Class | Class source |
Files | / | src | / | ssa | / | runner |
File | Role | Description | ||
---|---|---|---|---|
converter (3 files, 1 directory) | ||||
resolver (6 files, 1 directory) | ||||
ActionNotSupportedException.php | Class | Class source | ||
BadTypeException.php | Class | Class source | ||
ClassNotFoundException.php | Class | Class source | ||
MissingParameterException.php | Class | Class source | ||
ServiceRunner.php | Class | Class source |
Files | / | src | / | ssa | / | runner | / | converter |
File | Role | Description | ||
---|---|---|---|---|
annotations (1 file) | ||||
DefaultJsonEncoder.php | Class | Class source | ||
Encoder.php | Class | Class source | ||
FileEncoder.php | Class | Class source |
Files | / | src | / | ssa | / | runner | / | converter | / | annotations |
File | Role | Description |
---|---|---|
Encoder.php | Class | Class source |
Files | / | src | / | ssa | / | runner | / | resolver |
File | Role | Description | ||
---|---|---|---|---|
impl (7 files) | ||||
ObjectResolver.php | Class | Class source | ||
ObjectResolverCOR.php | Class | Class source | ||
ParameterResolver.php | Class | Class source | ||
PrimitiveResolver.php | Class | Class source | ||
PrimitiveResolverCOR.php | Class | Class source | ||
TypeNotSupportedException.php | Class | Class source |
Files | / | src | / | ssa | / | runner | / | resolver | / | impl |
File | Role | Description |
---|---|---|
ArrayFilePrimitiveResolver.php | Class | Class source |
ArrayPrimitiveResolver.php | Class | Class source |
DateTimeObjectResolver.php | Class | Class source |
DefaultObjectResolver.php | Class | Class source |
DefaultParameterResolver.php | Class | Class source |
DefaultPrimitiveResolver.php | Class | Class source |
FilePrimitiveResolver.php | Class | Class source |
Files | / | test |
File | Role | Description | ||
---|---|---|---|---|
ssa (4 directories) | ||||
bootstrap.php | Aux. | Class source | ||
phpunit.xml | Data | Auxiliary data |
Files | / | test | / | ssa |
File | Role | Description | ||
---|---|---|---|---|
annotation (1 file) | ||||
converter (3 files) | ||||
runner (3 files, 2 directories) | ||||
toEndTest (7 files) |
Files | / | test | / | ssa | / | converter |
File | Role | Description |
---|---|---|
JavascriptConverterTest.php | Test | Unit test script |
ServiceTest.php | Test | Unit test script |
SimpleUrlFactoryTest.php | Test | Unit test script |
Files | / | test | / | ssa | / | runner |
File | Role | Description | ||
---|---|---|---|---|
converter (2 files) | ||||
resolver (3 files, 1 directory) | ||||
ServiceManagerTest.php | Test | Unit test script | ||
ServiceMetadataTest.php | Test | Unit test script | ||
ServiceRunnerTest.php | Test | Unit test script |
Files | / | test | / | ssa | / | runner | / | converter |
File | Role | Description |
---|---|---|
DefaultJsonEncoderTest.php | Test | Unit test script |
FileEncoderTest.php | Test | Unit test script |
Files | / | test | / | ssa | / | runner | / | resolver |
File | Role | Description | ||
---|---|---|---|---|
impl (6 files) | ||||
ObjectResolverCORTest.php | Test | Unit test script | ||
Pojo.php | Test | Unit test script | ||
PrimitiveResolverCORTest.php | Test | Unit test script |
Files | / | test | / | ssa | / | runner | / | resolver | / | impl |
File | Role | Description |
---|---|---|
ArrayFilePrimitiveResolver.php | Test | Unit test script |
ArrayPrimitiveResolverTest.php | Test | Unit test script |
DateTimeObjectResolverTest.php | Test | Unit test script |
DefaultObjectResolverTest.php | Test | Unit test script |
DefaultPrimitiveResolverTest.php | Test | Unit test script |
FilePrimitiveResolver.php | Test | Unit test script |
Files | / | test | / | ssa | / | toEndTest |
File | Role | Description |
---|---|---|
HelloWorld.php | Example | Example script |
javascript.php | Example | Example script |
run.php | Example | Example script |
serviceConfig.php | Example | Example script |
test.html | Data | Auxiliary data |
test_angular.html | Data | Auxiliary data |
test_requirejs.html | Doc. | Documentation |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
User Comments (1) | |||||
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Related pages |
github ssa ssa repo on github |
ssa Secure Secure module for ssa. You can logon your users |
ssa Symfony github repo for symfony version |