Guest User

Untitled

a guest
Jun 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # MT registry cookbook
  3.  
  4. use strict;
  5. use warnings;
  6. use lib qw( lib extlib ../lib ../extlib);
  7. use utf8;
  8. use MT;
  9. use MT::Component;
  10. use Test::More;
  11.  
  12. # create and register components to MT.
  13. # ( normally this is automatically done by MT at initialize phase )
  14.  
  15. my $c1 = MT::Component->new;
  16. my $c2 = MT::Component->new;
  17. push @MT::Components, ( $c1, $c2 );
  18. $MT::Component{c1} = $c1;
  19. $MT::Component{c2} = $c2;
  20.  
  21. =head1 registry method is Setter/Getter
  22.  
  23. You can use $component->registry() method to Set/Get registry values.
  24.  
  25. =head2 Setter
  26.  
  27. If hashref is in args, it is Setter.
  28. $component->registry( 'path' => 'to' => 'set' => {
  29. things => 'you',
  30. wanna => 'set',
  31. });
  32.  
  33. =cut
  34.  
  35. $c1->registry( this => is => { a => 'pen' } );
  36. is_deeply(
  37. $c1->{registry},
  38. {
  39. this => {
  40. is => {
  41. a => 'pen',
  42. plugin => $c1,
  43. }
  44. }
  45. },
  46. 'Set value.',
  47. );
  48.  
  49. =head2 Getter
  50.  
  51. And, if all args were scalar, it is getter.
  52. $component->registry( 'path' => 'to' => 'get' );
  53.  
  54. =cut
  55.  
  56. is(
  57. $c1->registry( this => is => 'a' ),
  58. 'pen',
  59. 'Get value.',
  60. );
  61.  
  62. =head1 Two more getters
  63.  
  64. =head2 MT::registry()
  65.  
  66. MT::registry method can marge hashes of components.
  67.  
  68. =cut
  69.  
  70. $c1->registry( foo => { bar => 1 } );
  71. $c2->registry( foo => { buz => 1 } );
  72. is_deeply(
  73. MT->registry('foo'),
  74. {
  75. bar => 1,
  76. buz => 1,
  77. },
  78. 'Get merged value.',
  79. );
  80.  
  81. =head2 MT::Component::registry()
  82.  
  83. MT::Component::registry was invoked as class method, returns
  84. arrayref of each components' value of the path.
  85.  
  86. =cut
  87.  
  88. is_deeply(
  89. MT::Component->registry('foo'),
  90. [
  91. { 'bar' => 1 },
  92. { 'buz' => 1 },
  93. ],
  94. 'Get multiple values from components as arrayref.',
  95. );
  96.  
  97. =head1 Localization
  98.  
  99. In registry, the strings registered with the key name ends with 'label' are
  100. automatically translated.
  101.  
  102. This means, If you get the hash tree, all '*label' values in the hash can get
  103. as closure. if you want to get the translated string, just run it.
  104.  
  105. =cut
  106.  
  107. $c1->registry({
  108. entry_label => 'Entry',
  109. foo => {
  110. page_label => 'Page',
  111. },
  112. });
  113.  
  114. MT->new; # required for load Core translate lexicons.
  115. MT->set_language('ja');
  116. is(
  117. $c1->registry('entry_label'),
  118. 'ブログ記事',
  119. 'Get translated value',
  120. );
  121.  
  122. my $page_label = $c1->registry('foo')->{page_label};
  123. is(
  124. ref $page_label,
  125. 'CODE',
  126. 'In deep hash, it is closure.',
  127. );
  128. is(
  129. $page_label->(),
  130. 'ウェブページ',
  131. 'And we can get translated value from it.',
  132. );
  133.  
  134. =head1 Grafts
  135.  
  136. May use some tricks grafting yaml file or subroutins on to other registry for
  137. delay loading or using dynamic values.
  138.  
  139. =head2 sub routine reference
  140.  
  141. If you set subroutin reference to the registry, you can explore the return
  142. value of subroutin as it's a part of registry.
  143.  
  144. =cut
  145.  
  146. $c1->registry({
  147. secret => sub {
  148. return {
  149. 'of_life' => 2 * 3 * 7,
  150. };
  151. },
  152. });
  153.  
  154. is(
  155. $c1->registry( 'secret' => 'of_life' ),
  156. 42,
  157. 'Fetch from coderef grafts.',
  158. );
  159.  
  160. =head2 MT specified routine name
  161.  
  162. If the string witch starts with doller sign($) and includes double colon(::),
  163. MT understands this is name of perl subroutine.
  164. Note that this value is *NOT* the name of subroutin name in perl symbol table.
  165. the first part is the MT::Component's ID value.
  166.  
  167. $ + COMPONENT_ID + :: + FULL::NAME::OF::SUB
  168.  
  169. =cut
  170.  
  171. sub my_sub {
  172. my $component = shift;
  173. return {
  174. nelson => "histoire"
  175. };
  176. }
  177.  
  178. $c1->registry({ melody => '$c1::main::my_sub' });
  179. is(
  180. $c1->registry( melody => 'nelson' ),
  181. 'histoire',
  182. 'Fetch from subroutine grafts',
  183. );
  184.  
  185. =pod
  186.  
  187. or you wanna invoke the routine as class method, you can use another syntax
  188.  
  189. $ + COMPONENT_ID + :: + PACKAGE::NAME->routine_name
  190.  
  191. this means, set I<COMPONENT_ID> to the current component and run I<routine_name> method
  192. of I<PACKAGE::NAME> package, then grafts retun value on to the registry.
  193.  
  194. (this needs real file lib/PACKAGE/NAME.pm at somewhere in search path)
  195.  
  196. =cut
  197.  
  198. package MT;
  199.  
  200. sub this_is_me {
  201. my $pkg = shift;
  202. my $component = shift;
  203. return {
  204. my_name => "my name is $pkg",
  205. }
  206. }
  207.  
  208. package main;
  209.  
  210. $c1->registry({ this_is_me => '$c1::MT->this_is_me' });
  211. is(
  212. $c1->registry( this_is_me => 'my_name' ),
  213. 'my name is MT',
  214. 'Fetch from class method grafts.'
  215. );
  216.  
  217. =head2 YAML file
  218.  
  219. Registry getters can read inside of yaml file, if the yaml file name
  220. ( string ends with '.yaml' ) was found in the value of registry.
  221.  
  222. MT will search the specified yaml file from the component's base path.
  223.  
  224. =cut
  225.  
  226. # sorry this test will be skipped until you put registry-test.yaml
  227. # in the same place of this script and that file includes this line.
  228. #---
  229. #greeting: Hello from registry-test.yaml.
  230. #---
  231.  
  232. SKIP: {
  233. skip "No yaml file for test", 1 unless -f 'registry-test.yaml';
  234. $c1->path('.');
  235. $c1->registry({ yaml => 'registry-test.yaml' });
  236. is(
  237. $c1->registry('yaml' => 'greeting'),
  238. 'Hello from registry-test.yaml.',
  239. 'Fetch from yaml file grafts.',
  240. );
  241. }
  242.  
  243. done_testing();
Add Comment
Please, Sign In to add comment