source

sample

code


target file(s):


controllers/source_controller.php

1 <?php
2
3 class SourceController extends AppController {
4
5 var $name = 'Source';
6 var $uses = array();
7 var $sourceFiles = array(
8 'source' => array(
9 'controllers/source_controller.php',
10 'views/source/index.ctp',
11 'views/source/view.ctp',
12 'views/elements/code.ctp',
13 'views/elements/pageLink.ctp',
14 ),
15 'debug' => array(
16 'controllers/debug_controller.php',
17 'views/debug/index.ctp',
18 'views/elements/pageLink.ctp',
19 ),
20 'validation' => array(
21 'controllers/validation_controller.php',
22 'views/validation/index.ctp',
23 'models/user.php',
24 ),
25 'testing' => array(
26 'models/user.php',
27 'tests/cases/models/user.test.php',
28 'tests/fixtures/user_fixture.php',
29 ),
30 );
31
32 function index() {
33 $sourceKeys = array_keys($this->sourceFiles);
34 $this->set('sourceKeys', $sourceKeys);
35 }
36
37 function view($act = null) {
38 if (empty($act)) {
39 $this->redirect(array('action' => 'index'));
40 }
41 if (!array_key_exists($act, $this->sourceFiles)) {
42 $this->Session->setFlash(__('invalid key.', true));
43 $this->redirect(array('action' => 'index'));
44 }
45
46 $sources = array();
47 foreach($this->sourceFiles[$act] as $file) {
48 $file_path = ROOT . DS . APP_DIR . DS . $file;
49 if (file_exists($file_path)) {
50 $lines = file($file_path);
51 $sources[$file] = $lines;
52 } else {
53 $sources[$file] = null;
54 }
55 }
56 $this->set('sources', $sources);
57 $this->set('sourceKey', $act);
58 }
59 }
60 ?>

views/source/index.ctp

1 <dl>
2 <?php
3 foreach ($sourceKeys as $sourceKey) {
4 $params = array(
5 'sourceKey' => $sourceKey,
6 );
7 echo $this->element('pageLink', $params);
8 }
9 ?>
10 </dl>

views/source/view.ctp

1 <?php
2 $params = array(
3 'sourceKey' => $sourceKey,
4 );
5 echo $this->element('pageLink', $params);
6 ?>
7
8 <hr>
9 <a name="index"></a>
10 target file(s):<br>
11 <ul>
12 <?php
13 foreach($sources as $filePath => $lines) {
14 ?>
15 <li><a href="#<?php echo urlencode($filePath); ?>">
16 <?php echo $filePath; ?></a><br>
17 <?php
18 }
19 ?>
20 </ul><br>
21 <hr>
22 <?php
23 reset($sources);
24 foreach($sources as $filePath => $lines) {
25 $params = array(
26 'filePath' => $filePath,
27 'lines' => $lines
28 );
29 echo $this->element('code', $params);
30 }
31
32 $script = <<< EOD
33 <style type="text/css">
34 <!--
35 .code {
36 margin: 1em 0 1em 0;
37 padding: 1px;
38 }
39 table.code {
40 border: 1px solid gray;
41 margin-left: 20px;
42 border-collapse: collapse;
43 }
44 td.line {
45 text-align: right;
46 padding-right: .3em;
47 width: 3em;
48 white-space: pre;
49 }
50 td.content {
51 padding-left: .5em;
52 border-left:solid 3px #0099cc;
53 white-space: pre;
54 }
55 -->
56 </style>
57 EOD;
58
59 $this->addScript($script);
60 ?>

views/elements/code.ctp

1 <div style="float: left;">
2 <h3><a name="<?php echo urlencode($filePath); ?>"></a><?php echo $filePath; ?></h3>
3 </div>
4 <div style="float: right;">
5 <a href="#index">↑</a>
6 </div>
7
8 <?php
9 if (empty($lines)) {
10 ?>
11 <p class="error">file not found.</p>
12 <?php
13 } else {
14 ?>
15
16 <table class="code">
17 <?php
18 foreach ($lines as $lineNum => $line):
19 ?>
20 <tr class="code">
21 <td class="code line"><?php echo ++$lineNum; ?></td>
22 <td class="code content"><?php echo htmlspecialchars($line); ?></td>
23 </tr>
24 <?php endforeach; ?>
25 </table>
26
27 <?php
28 }
29 ?>

views/elements/pageLink.ctp

1 <dl>
2 <?php
3 echo "<dt>$sourceKey</dt><br>\n";
4 $link = array(
5 'controller' => $sourceKey,
6 'action' => 'index',
7 );
8 echo "<dd>" . $html->link('sample', $link) . "</dd><br>";
9
10 $link = array(
11 'controller' => 'source',
12 'action' => 'view',
13 $sourceKey
14 );
15 echo "<dd>" . $html->link('code', $link) . "</dd><br>";
16 ?>
17 </dl>