📜 ⬆️ ⬇️

Phalcon 1.2.0 release



We are pleased to announce the release of the new version of Phalcon .

A little more than two months have passed since the release of the previous version, and the development team presents one of the largest and most serious releases of the fastest PHP framework written as a C-extension.
The new version contains many new features, bug fixes and optimizations. We also updated the site and are preparing for major documentation updates and API descriptions.
')
Innovations Phalcon 1.2.0:
  1. Dynamic paths for compiled Volt templates
  2. Empowering Volt
  3. Links for static and dynamic paths in Phalcon \ Mvc \ Url
  4. Phalcon \ Mvc \ View \ Simple
  5. Improved work with JSON
  6. Many-To-Many support in ORM
  7. Work with virtual foreign keys
  8. Javascript and CSS minification
  9. Barring variables (literals) in PHQL
  10. Extended Partials
  11. Using Phalcon \ Tag as a service
  12. Macros in Volt
  13. BadMethodCallException instead of warnings
  14. Debug component

Download
New motto
Acknowledgments


So, what was implemented in Phalcon 1.2.0



1. Dynamic Paths for Volt Compiled Templates

The “compiledPath” option in the Volt template engine now accepts an anonymous function that allows you to perform the necessary actions before writing the template cache file.
//   .php    $volt->setOptions([ 'compiledPath' => function($templatePath) { return $templatePath . '.php'; } ]); // ​ ​     $volt->setOptions([ 'compiledPath' => function($templatePath) { $dirName = dirname($templatePath); if (!is_dir(CACHE_DIR . $dirName)) { mkdir(CACHE_DIR . $dirName,0755,true); } return CACHE_DIR . $dirName . '/'. $templatePath . '.php'; } ]); 


2. Empowering the Volt

For our template engine, you can now add any required extensions, change behavior, operators, and add functions and filters. The class below, for example, allows using any php-functions in templates:
 class PhpFunctionExtension { public function compileFunction($name, $arguments) { if (function_exists($name)) { return $name . '('. $arguments . ')'; } } } 

Add this extension:
 $volt->getCompiler()->addExtension(new PHPFunctionExtension()); 


3. Links for static and dynamic paths in Phalcon \ Mvc \ Url

Now you can specify different paths for links leading to static files: to pictures or, for example, js files and for links to pages. This feature will be especially relevant when using a CDN.
 $di['url'] = function() { $url = new Phalcon\Mvc\Url(); // ​  URI  mod-rewrite $url->setBaseUri('/index.php?_url='); // ​ ​ URI  CSS/Javascript/Images $url->setStaticUri('/static/'); return $url; }; 


4. Phalcon \ Mvc \ View \ Simple

The component is a lighter alternative to Phalcon \ Mvc \ View, without support for a hierarchy of patterns. It will be relevant for use in micro-applications and get views as a string.
 // ​ ​View service $di['view'] = function() { $view = new Phalcon\Mvc\View\Simple(); $view->setViewsDir(APP_PATH . '/views/'); return $view; }; 

Use in micro applications:
 $app->map('/login', function() use ($app) { echo $app->view->render('security/login', array( 'form' => new LoginForm(), )); }); 

The component supports different template engines and template caching.

5. Improved work with JSON

Receive and send data in JSON-format has become much easier. Transferring data to Phalcon \ Http \ Response will automatically be sent in JSON format:
 $app->post('/api/robots', function() use ($app) { $data = $app->request->getJsonRawBody(); $robot = new Robots(); $robot->name = $data->name; $robot->type = $data->type; $response = new Phalcon\Http\Response(); //    if ($robot->success() == true) { $response->setJsonContent([ 'status' => 'OK', 'id' => $robot->id ]); } else { //   HTTP  $response->setStatusCode(500, "Internal Error"); $response->setJsonContent([ 'status' => 'ERROR', 'messages' => $status->getMessages() ]); } return $response; }); 


6. Many-To-Many support in ORM

Our ORM has begun to maintain a many-to-many relationship. This allows you to establish direct relationships between the two models using a third intermediate model:
 class Artists extends Phalcon\Mvc\Model { public $id; public $name; public function initialize() { $this->hasManyToMany( 'id', 'ArtistsSongs', 'artists_id', 'songs_id', 'Songs', 'id' ); } } 

Songs of performers can be obtained through the alias of the relationship:
 $artist = Artists::findFirst(); //     foreach ($artist->songs as $song) { echo $song->name; } 

With many-to-many relationships you can work in PHQL (this is an add-on for SQL, which simplifies the use of models and the construction of sql queries):
 $phql = 'SELECT Artists.name, Songs.name FROM Artists JOIN Songs WHERE Artists.genre = "Trip-Hop"'; $result = $this->modelsManager->query($phql); 

Linked data can be added immediately when creating a model, all necessary intermediate binding records will also be created automatically:
 $songs = array() $song = new Song(); $song->name = 'Get Lucky'; $songs[] = $song; $song = new Song(); $song->name = 'Instant Crush'; $songs[] = $song; $artist = new Artists(); $artist->name = 'Daft Punk'; $artist->songs = $songs; //    $artist->save(); 


7. Work with virtual foreign keys

When specifying a Virtual foreign keys, all dependent elements associated with the main entry will also be deleted:
 use Phalcon\Mvc\Model Phalcon\Mvc\Model\Relation; class Artists extends Model { public $id; public $name; public function initialize() { $this->hasMany('id', 'Songs', 'artists_id', [ 'foreignKey' => [ 'action' => Relation::ACTION_CASCADE ] ]); } } 

When deleting an artist, all his songs will also be deleted:
 $artist = Artists::findFirst(); $artist->delete(); //     


8. Minification of Javascript and CSS

We taught Phalcon \ Assets component not only to flexibly manage static resources, group and filter them, but also compress JS / CSS (minify). For such demanding work, ready-made Jsmin solutions from Douglas Crockford and CSSMin from Ryan Day were used.
 $manager = new Phalcon\Assets\Manager(array( 'sourceBasePath' => './js/', 'targetBasePath' => './js/production/' )); $manager //  Javascripts     ->collection('jsFooter') //     ->setTargetPath('final.js') //    ->setTargetUri('production/final.js') //    ,    ->addJs('code.jquery.com/jquery-1.10.0.min.js', true, false) //       ->addJs('common-functions.js') ->addJs('page-functions.js') //       ( final.js ) ->join(true) //    Jsmin ->addFilter(new Phalcon\Assets\Filters\Jsmin()) //   ,    ->addFilter(new MyApp\Assets\Filters\LicenseStamper()); $manager->outputJs(); 


9. Prohibiting variables (literals) in PHQL

Parameters inside PHQL now cannot be used directly, the following code will throw exceptions:
 $artist = Artists::findFirst("name = '$name'"); 


10. Expanded features of Partials

The developer can now pass an array of parameters to partials:
 <?php $this->partial('footer', ['links' => $myLinks]); 

Volt also supports this feature:
 {{ partial('footer', ['links': myLinks]) }} {% include 'footer' with ['links': myLinks] %} 


11. Using Phalcon \ Tag as a service

We diligently avoid using any static methods, they still work, but using them is not advisable. The Phalcon \ Tag component is now available as a service in DI \ FactoryDefault. Instead:
 Phalcon\Tag is now a service in DI\FactoryDefault​. So instead of doing this: 

Worth using:
 $this->tag->setDefault('name', $robot->name); 


12. Macros in Volt

Work with macros has just begun, more will continue:
 {%- macro input_text(name, attributes=null) -%} {{- '<input type="' ~ name ~ '" ' -}} {%- for key, value in attributes -%} {{- key ~ '="' ~ value|e '"' -}} {%- endfor -%} {{- '/>' -}} {%- endmacro -%} {{ input_text("telephone", ['placeholder': 'Type telephone']) }} 


13. BadMethodCallException instead of warnings

Prior to version 1.1.0, a warning occurred when passing an incorrect number of parameters to methods. Starting from version 1.2.0, in such situations, a BadMethodCallException will be created with the ability to track exactly where the error occurred:
 <?php $e = new Phalcon\Escaper(); $e->escapeCss('a {}', 1, 2, 3); 

Will be displayed:
 Fatal error: Uncaught exception 'BadMethodCallException' with message 'Wrong number of parameters' in test.php:4 Stack trace: #0 test.php(4): Phalcon\Escaper->escapeCss('a {}', 1, 2, 3) #1 {main} 


14. Debug Component

The Phalcon \ Debug component forms a debugging stack, allowing the developer to trace where something went wrong. The data are decorated in a structured form. For the component to work, you must remove the try / catch blocks in the start file and specify it at the beginning:
 (new Phalcon\Debug)->listen(); 

We have prepared a small screencast with a demonstration of the component:


There are some more important innovations:

A complete list of changes is available at CHANGELOG .

Download


You can build a new version on GitHub , or download ready-made DLL libraries for Windows. Ready-made builds are also available for some linux systems . Documentation is also updated.

New motto


For a long time we worked under the motto “The fastest PHP Framework”, fully complying with it and maintaining the speed of Phalcon at the maximum. We optimized everything we could and looked for any opportunities to save memory and reduce overhead. But speed is not our only priority. From version to version, we tried to make Phalcon even more convenient and include the most useful features. This allowed the project to grow into one of the fastest and most functional products in the open source world.
Moving forward, we decided to change our motto for more accurate positioning of Phalcon. From version 1.2.0 we work under the slogan “The best balance of speed and functionality”. Our primary goal is to maintain the optimal balance between the speed of the framework and the functionality it provides. We really want to be not only the fastest framework for PHP, but also the most optimal in terms of speed and functionality.

Acknowledgments


We want to thank all the community members, developers, voting for new functions and helping with their implementation and testing. Anyone who helps with testing and improving documentation.
Thanks to everyone who helps develop Phalcon and participates in community life.
Thanks you!

Source: https://habr.com/ru/post/186150/


All Articles