$this
. Also, we usually provide a set of methods for setting the internal properties of an object, which allows you to customize the parameters of an object inside an expression and also makes the code more concise.
- <? php
- class DSL_Builder {
- protected $ parent ;
- protected $ object ;
- public function __construct ( $ parent , $ object ) {
- $ this -> parent = $ parent ;
- $ this -> object = $ object ;
- }
- public function __get ( $ property ) {
- switch ( $ property ) {
- case 'end' :
- return $ this -> parent? $ this -> parent: $ this -> object;
- case 'object' :
- return $ this -> $ property ;
- default :
- throw new Core_MissingPropertyException ( $ property );
- }
- }
- public function __set ( $ property , $ value ) { throw new Core_ReadOnlyObjectException ( $ this ); }
- public function __isset ( $ property ) {
- switch ( $ property ) {
- case 'object' :
- return isset ( $ this -> $ property );
- default :
- return false ;
- }
- }
- public function __unset ( $ property ) { throw new Core_ReadOnlyObjectException ( $ this ); }
- public function __call ( $ method , $ args ) {
- method_exists ( $ this -> object, $ method )?
- call_user_func_array ( array ( $ this -> object, $ method ), $ args ):
- $ this -> object-> $ method = $ args [ 0 ];
- return $ this ;
- }
- }
- ?>
$object
field, delegating to it a method call and setting properties. Of course, the builder can define its own set of methods for more complex configuration of the target object. In this case, the end
pseudo-property allows you to return to the parent object builder, and so on.
- <? php
- class Config_DSL_Builder extends DSL_Builder {
- public function __construct (Config_DSL_Builder $ parent = null , stdClass $ object = null ) {
- parent :: __ construct ( $ parent , Core :: if_null ( $ object , new stdClass ()));
- }
- public function load ( $ file ) {
- ob_start ();
- include ( $ file );
- ob_end_clean ();
- return $ this ;
- }
- public function begin ( $ name ) {
- Return new Config_DSL_Builder ( $ this , $ this -> object-> $ name = new stdClass ());
- }
- public function __get ( $ property ) {
- return (strpos ( $ property , 'begin_' ) === 0 )?
- $ this -> begin (substr ( $ property , 6 )):
- parent :: __ get ( $ property );
- }
- public function __call ( $ method , $ args ) {
- $ this -> object-> $ method = $ args [ 0 ];
- return $ this ;
- }
- }
- ?>
config.php
in which we describe the configuration of our application in the following form:
- <? php
- $ this ->
- begin_db->
- dsn ( 'mysql: // user: password @ localhost / db' ) ->
- end->
- begin_cache->
- dsn ( 'dummy: //' ) ->
- default_timeout ( 300 ) ->
- timeouts ( array (
- 'front / index' => 300 ,
- 'news / most_popular' => 300 ,
- 'news / category' => 300 )) ->
- end->
- begin_site->
- begin_from->
- top_limit ( 7 ) ->
- end->
- begin_news->
- most_popular_limit ( 5 ) ->
- end->
- end;
- ?>
- <? php
- $ config = Config_DSL :: Builder () -> load ( 'config.php' );
- ?>
- <? php
- WS_REST_DSL :: Application () ->
- media_type ( 'html' , 'text / html' , true ) ->
- media_type ( 'rss' , 'application / xhtml + xml' ) ->
- begin_resource ( 'gallery' , 'App.Photo.Gallery' , 'galleries / {id: \ d +}' ) ->
- for_format ( 'html' ) ->
- get_for ( '{page_no: \ d +}' , 'index' ) ->
- post_for ( 'vote' , 'vote' ) ->
- index () ->
- end->
- end->
- begin_resource ( 'index' , 'App.Photo.Index' ) ->
- for_format ( 'rss' ) ->
- get ( 'index_rss' ) ->
- get_for ( 'top' , 'top_rss' ) ->
- end->
- for_format ( 'html' ) ->
- get_for ( '{page_no: \ d +}' , 'index' ) ->
- index () ->
- end->
- end->
- end;
- ?>
- <? php
- public function index ( $ page_no = 1 ) {
- $ pager = Data_Pagination :: pager ( $ this -> db-> photo-> galleries-> count (), $ page_no , self :: PAGE_LIMIT);
- return $ this -> html ( 'index' ) ->
- with ( array (
- 'top' => $ this -> db-> photo-> galleries-> most_important () -> select (),
- 'pager' => $ pager ,
- 'galleries' => $ this -> db-> photo-> galleries->
- published () ->
- paginate_with ( $ pager ) ->
- select ()));
- }
- ?>
DSL_Builder
slightly expanding the DSL_Builder
class, it is possible to describe not only a static structure, but also a set of actions, that is, some script. For example, with the Google AdWords API you can work like this:
- <? php
- Service_Google_AdWords_DSL :: Script () ->
- for_campaign ( $ campaign_id ) ->
- for_ad_group ( $ group_id ) ->
- for_each ( 'text' , 'keyword1' , 'keyword2' , 'keyword3' ) ->
- add_keyword_criteria () ->
- bind ( 'text' ) ->
- end->
- end->
- add_ad () ->
- with ( 'headline' , 'headline' ,
- 'displayUrl' , 'www.techart.ru' ,
- 'destinationUrl' , 'http://www.techart.ru/' ,
- 'description1' , 'desc1' ,
- 'description2' , 'desc2' ) ->
- format ( "Ad Created" ) ->
- end->
- end->
- end->
- for_each_campaign () ->
- format ( "Campaign:% d,% s \ n" , 'campaign.id' , 'campaign.name' ) ->
- dump ( 'campaign' ) ->
- for_each_ad_group () ->
- format ( "Ad group:% d,% s \ n" , 'ad_group.id' , 'ad_group.name' ) ->
- for_each_criteria () ->
- format ( "Criteria:% d,% s \ n" , 'criteria.id' , 'criteria.text' ) ->
- end->
- end->
- end->
- end->
- run_for (Service_Google_AdWords :: Client () ->
- useragent ( 'user agent' ) ->
- email ( 'email@domain.com' ));
- ?>
Source: https://habr.com/ru/post/82715/
All Articles