1: <?php
2: /**
3: * @author Jefferson González
4: * @license MIT
5: * @link http://github.com/peg-org/peg-custom Source code.
6: */
7:
8: namespace Peg\Custom\Command\Action\Parse;
9:
10: use Peg\Custom\Application;
11: use Peg\Custom\CommandLine\Error;
12: use Peg\Custom\Utilities\FileSystem;
13:
14: /**
15: * Declares the base for a parse action that extract and generates definition
16: * files.
17: */
18: abstract class Base extends \Peg\Custom\CommandLine\Action
19: {
20: /**
21: * Reference to command that called this action.
22: * @var \Peg\Custom\CommandLine\Command
23: */
24: protected $command;
25:
26: /**
27: * Format of the files to parse/lex.
28: * @var string
29: */
30: protected $input_format;
31:
32: /**
33: * Format used to store the parsed definition files.
34: * @see \Peg\Lib\Definitions\Type
35: * @var string
36: */
37: protected $output_format;
38:
39: /**
40: * This is used optionally by a children implementing this class in order
41: * for them to correctly resolve a header file. For example: doxygen xml
42: * files store the full path to the header file where a symbol was found, eg:
43: * /home/user/libs/wx/frame.h, if this variable is set to /home/user/libs/
44: * the final header file will be stored on the symbols object as wx/frame.h
45: * @var string
46: */
47: protected $headers_path;
48:
49: /**
50: * Flag that indicates if the lexer/parser should output messages of its
51: * current status.
52: * @var bool
53: */
54: protected $verbose;
55:
56: /**
57: * You derived class should override this and set the
58: * input_format name of your parser/lexer so it is called apropiately
59: * when this variable is set.
60: * @param string $input_format Format of files to parse, eg: doxygen
61: */
62: public function __construct($input_format)
63: {
64: $this->input_format = $input_format;
65: }
66:
67: /**
68: * You shouldn't override this method, instead write a Start() implementation.
69: * @param \Peg\Custom\CommandLine\Command $command
70: */
71: public function OnCall(\Peg\Custom\CommandLine\Command $command)
72: {
73: if(!Application::ValidExtension())
74: Error::Show(t("The current directory is not a valid peg managed extension."));
75:
76: if(!file_exists(Application::GetCwd() . "/definitions"))
77: FileSystem::MakeDir(Application::GetCwd() . "/definitions");
78:
79: if($command->GetOption("input-format")->GetValue() == $this->input_format)
80: {
81: $this->command = $command;
82:
83: $this->output_format = $command->GetOption("output-format")->GetValue();
84:
85: if(trim($command->GetOption("headers")->GetValue()) != "")
86: {
87: $this->headers_path = rtrim(
88: str_replace(
89: "\\",
90: "/",
91: $command->GetOption("headers")->GetValue()
92: ),
93: "/"
94: ) . "/";
95: }
96:
97: $this->verbose = $command->GetOption("verbose")->active;
98:
99: $this->Start($command->GetOption("source")->GetValue());
100: }
101: }
102:
103: /**
104: * Needs to be implemented by classes extending this one in order to
105: * begin the parsing process when the input format matches that of the
106: * action been called.
107: */
108: abstract public function Start($path);
109: }