/src/examples_utils.inc.php

https://github.com/limb-php-framework/limb-example-macro · PHP · 189 lines · 127 code · 32 blank · 30 comment · 16 complexity · e6795a6609659830181338bf417e098c MD5 · raw file

  1. <?php
  2. /**
  3. * require framework
  4. */
  5. require_once './setup.php';
  6. /**
  7. * path
  8. */
  9. $tmp_arr = explode('/', $_SERVER['PHP_SELF']);
  10. $examples_root_file = array_pop($tmp_arr);
  11. $examples_root_dir = implode('/', $tmp_arr);
  12. $examples_root_dir = 'http://'.$_SERVER['HTTP_HOST'].$examples_root_dir.'/';
  13. unset($tmp_arr);
  14. // -------------------------------------------------------------
  15. /**
  16. * @return array file & subdir listing, without "." and ".."
  17. */
  18. function getDirContents($dir)
  19. {
  20. $dir = rtrim($dir, '/\\');
  21. $files = scandir($dir);
  22. if (!is_array($files)) {
  23. return false;
  24. }
  25. //strip . and ..
  26. array_shift($files);
  27. array_shift($files);
  28. return $files;
  29. }
  30. // -------------------------------------------------------------
  31. /**
  32. * @param array file list
  33. * @param string path
  34. */
  35. function stripFiles($dirContents, $path)
  36. {
  37. $dirs = array();
  38. foreach ($dirContents as $file)
  39. {
  40. if (is_dir($path . DIRECTORY_SEPARATOR . $file))
  41. $dirs[] = $file;
  42. }
  43. $ignored = array('.svn', 'templates');
  44. if (file_exists($path. DIRECTORY_SEPARATOR . 'ignore.ini'))
  45. $ignored = array_merge($ignored, parse_ini_file($path . DIRECTORY_SEPARATOR . 'ignore.ini'));
  46. foreach ($dirs as $k => $dir)
  47. {
  48. if (in_array($dir, $ignored))
  49. unset($dirs[$k]);
  50. }
  51. return $dirs;
  52. }
  53. // -------------------------------------------------------------
  54. /**
  55. * @param reference to array
  56. * @param integer number of levels to scan
  57. */
  58. function createTree(& $node, $levels = 1)
  59. {
  60. if ($levels < 1)
  61. return;
  62. $dirContents = getDirContents($node['path']);
  63. if ($dirContents == false)
  64. return;
  65. if (!in_array('desc.ini', $dirContents))
  66. {
  67. $subdirs = stripFiles($dirContents, $node['path']);
  68. foreach ($subdirs as $subdir)
  69. {
  70. $child = & addChild($node, $subdir);
  71. createTree($child, $levels - 1);
  72. }
  73. }
  74. }
  75. // -------------------------------------------------------------
  76. /**
  77. * @param reference to array (tree node)
  78. * @param string name of the node
  79. */
  80. function & addChild(&$node, $name)
  81. {
  82. $node['children'][$name] = array('name' => $name,
  83. 'path' => $node['path'] . $name . DIRECTORY_SEPARATOR,
  84. 'group' => $node['group'] . $name . '/',
  85. 'children' => array());
  86. return $node['children'][$name];
  87. }
  88. // -------------------------------------------------------------
  89. function treeToHtml(&$node)
  90. {
  91. $html = '<li>';
  92. if (!file_exists($node['path'] . 'desc.ini'))
  93. {
  94. $html .= '<a href="index.php?group='.$node['group'].'">'.$node['name'].'</a>';
  95. $html .= '<ul>';
  96. if (array_key_exists('children', $node) && is_array($node['children']))
  97. {
  98. foreach ($node['children'] as $child)
  99. $html .= treeToHtml($child);
  100. }
  101. $html .= '</ul>';
  102. }
  103. else
  104. {
  105. $html .= '<a href="show.php?group='.$node['group'].'">'.$node['name'].'</a>';
  106. }
  107. $html .= '</li>';
  108. return $html;
  109. }
  110. // -------------------------------------------------------------
  111. function get_breadcrumbs($path)
  112. {
  113. $nodes = array();
  114. $dirs = explode('/', $path);
  115. $dirs = array_filter($dirs, 'purge_empty');
  116. while (count($dirs))
  117. {
  118. $nodes[] = array('path' => implode('/', $dirs).'/',
  119. 'name' => array_pop($dirs));
  120. }
  121. //add examples root
  122. $nodes[] = array('path' => '',
  123. 'name' => 'examples');
  124. return array_reverse($nodes);
  125. }
  126. // -------------------------------------------------------------
  127. function purge_empty($var) {
  128. return !empty($var);
  129. }
  130. function getDescriptionFileData($desc_file, $root)
  131. {
  132. $data = parse_ini_file($desc_file, true);
  133. $newData = array();
  134. foreach ($data as $name => $row)
  135. {
  136. $srcfiles = array();
  137. if(!is_array($row) && $name == 'title')
  138. {
  139. $newData['title'] = $row;
  140. continue;
  141. }
  142. foreach ($row as $key => $file)
  143. {
  144. if (is_numeric($key))
  145. $srcfiles[] = array('path' => "./showtemplate.php?file=$root{$file}",
  146. 'file' => $file);
  147. }
  148. $newData['examples'][] = array('description' => $row['description'],
  149. 'exec' => "./run.php?file=$root{$row['php']}",
  150. 'compiled' => "./compiled.php?file=$root{$row['template']}",
  151. 'php_file' => array('path' => "./showsource.php?file=$root{$row['php']}",
  152. 'file' => $row['php']),
  153. 'templates' => $srcfiles);
  154. }
  155. return $newData;
  156. }
  157. ?>