Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Rewriting the CoreDump command to make use of the new toolset impleme…

…ntations.
  • Loading branch information...
commit 459a43e7c6789e776c72d5fe9a5f1a36cdbeee31 1 parent e9a8dfe
Tomasz Jędrzejewski authored

Showing 1 changed file with 53 additions and 62 deletions. Show diff stats Hide diff stats

  1. 115  src/Opl/Autoloader/Command/CoreDump.php
115  src/Opl/Autoloader/Command/CoreDump.php
@@ -10,6 +10,8 @@
10 10
  * and other contributors. See website for details.
11 11
  */
12 12
 namespace Opl\Autoloader\Command;
  13
+use Opl\Autoloader\Toolset\Configuration;
  14
+use Opl\Autoloader\Toolset\CoreDump as CoreDumpTool;
13 15
 use Symfony\Component\Console\Input\InputArgument;
14 16
 use Symfony\Component\Console\Input\InputOption;
15 17
 use Symfony\Component\Console\Input\InputInterface;
@@ -35,30 +37,24 @@ protected function configure()
35 37
 		$this->ignoreValidationErrors = true;
36 38
 		
37 39
 		$this->setDefinition(array(
38  
-			new InputArgument('definition', InputArgument::REQUIRED, 'The class location definition INI file'),
39  
-			new InputArgument('core', InputArgument::OPTIONAL, 'The core file location, unless specified in the INI file.'),
  40
+			new InputArgument('configuration', InputArgument::REQUIRED, 'The OPA configuration file.'),
40 41
 		))
41  
-			->setName('opl:autoloader:core-dump-load')
  42
+			->addOption('core', 'c', InputOption::VALUE_REQUIRED, 'The path to the core file.')
  43
+			->addOption('export-type', 'e', InputOption::VALUE_REQUIRED, 'Export type: \'require\' or \'concat\'')
  44
+			->addOption('strip-comments', 's', InputOption::VALUE_NONE, 'Strip comment headers in the concatenated files.')
  45
+			->setName('opl:autoloader:core-dump-export')
42 46
 			->setDescription('Generates a list of require statements that load the common application core.')
43 47
 			->setHelp(<<<EOF
44 48
 Use the <info>CoreTracker</info> autoloader decorator to find the common application
45 49
 core by sending some HTTP requests. The more requests you perform, the more precise
46  
-the lookup is. The configuration for the command is given as
47  
-an INI file, where each entry represents a single top-level namespace and a path to its code:
  50
+the lookup is. The command requires the path to the Open Power Autoloader XML configuration
  51
+file to be passed. Please refer to the library manual to read more about it.
48 52
 
49  
-  [config]
50  
-  coreDump = "./output/core.txt"
51  
-  coreLoadOutput = "./application/core.php"
52  
-  namespaceSeparator = "\\"
53  
-  extension = ".php"
54  
-
55  
-  [namespaces]
56  
-  Opl = "../libs/"
57  
-  Foo = "../libs/"
58  
-  Bar = "../other/"
59  
-
60  
-It is recommended for the paths to have the trailing slashes prepended. The coreDump
61  
-value can be also provided as a command argument. The INI setting is ignored then.
  53
+The default export type is \'require\' which generates a list of \'require\' statements
  54
+that load the core files. Another supported type is \'concat\' which concatenates the
  55
+core files into a single PHP file. Use it with -s option to strip the header comments
  56
+from the concatenated files. The concatenation is safe both for namespaces and non-namespaced
  57
+code.
62 58
 EOF
63 59
 			);
64 60
 	} // end configure();
@@ -68,67 +64,62 @@ protected function configure()
68 64
 	 */
69 65
 	protected function execute(InputInterface $input, OutputInterface $output)
70 66
 	{
71  
-		$definition = $input->getArgument('definition');
72  
-		if(!$definition)
  67
+		try
  68
+		{
  69
+			$configuration = new Configuration($input->getArgument('configuration'));
  70
+		}
  71
+		catch(RuntimeException $exception)
73 72
 		{
74  
-			$output->writeln('<error>No definition file specified!</error>');
  73
+			$output->writeln('<error>An error occured: '.$exception->getMessage().'</error>');
75 74
 			return;
76 75
 		}
77  
-		$data = parse_ini_file($definition, true);
78  
-		if(!is_array($data))
  76
+		
  77
+		$core = $input->getOption('core');
  78
+		if(!$configuration->hasFile('core-dump') && empty($core))
79 79
 		{
80  
-			$output->writeln('<error>Invalid INI structure in the definition file!</error>');
  80
+			$output->writeln('<error>The path to the core dump file is missing.</error>');
  81
+			$output->writeln('Hint: add \'core-dump\' file type to export-files section or use the --core option.');
81 82
 			return;
82 83
 		}
83  
-
84  
-		$coreDump = $input->getArgument('core');
85  
-		if($coreDump)
  84
+		elseif(empty($core))
86 85
 		{
87  
-			$data['config']['coreDump'] = $coreDump;
  86
+			$core = $configuration->getFile('core-dump');
88 87
 		}
89  
-		
90  
-		if(!file_exists($data['config']['coreDump']))
  88
+		if(!$configuration->hasFile('core-export'))
91 89
 		{
92  
-			$output->writeln('<error>Cannot open the core dump file!</error>');
  90
+			$output->writeln('<error>The path to the core export file is missing.</error>');
  91
+			$output->writeln('Hint: add \'core-export\' file type to export-files section.');
  92
+			return;
  93
+		}
  94
+		$exportType = $input->getOption('export-type');
  95
+		if(empty($exportType))
  96
+		{
  97
+			$exportType = 'require';
  98
+		}
  99
+		elseif($exportType != 'require' && $exportType != 'concat')
  100
+		{
  101
+			$output->writeln('<error>Invalid value for the --export-type option: \'require\' or \'concat\' expected.</error>');
93 102
 			return;
94 103
 		}
95  
-		$dump = unserialize(file_get_contents($data['config']['coreDump']));
96  
-		$outFile = fopen($data['config']['coreLoadOutput'], 'w');
97  
-		fwrite($outFile, '<'.'?php'.PHP_EOL);
98 104
 		
99  
-		foreach($dump as $className)
  105
+		$dump = new CoreDumpTool();
  106
+		foreach($configuration->getSeparators() as $separator)
100 107
 		{
101  
-			$fileName = $this->toFilename($data['namespaces'], $data['config']['namespaceSeparator'], $className, $output);
102  
-			if(false !== $fileName)
  108
+			foreach($configuration->getSeparatorNamespaces($separator) as $name => $namespace)
103 109
 			{
104  
-				fwrite($outFile, 'require(\''.$fileName.$data['config']['extension'].'\');'.PHP_EOL);
  110
+				$dump->addNamespace($name, $namespace['path'], $namespace['extension']);
105 111
 			}
106 112
 		}
107  
-		fclose($outFile);
108  
-		$output->writeln('<info>Core loading file generated.</info>');
109  
-	} // end execute();
110  
-	
111  
-	/**
112  
-	 * Returns the file name for the given class name.
113  
-	 *
114  
-	 * @param array $namespaces The list of available namespaces.
115  
-	 * @param string $className The class name to translate.
116  
-	 * @param OutputInterface $output The output interface.
117  
-	 */
118  
-	protected function toFilename(array $namespaces, $namespaceSeparator, $className, OutputInterface $output)
119  
-	{
120  
-		$className = ltrim($className, $namespaceSeparator);
121  
-		$match = strstr($className, $namespaceSeparator, true);
122  
-
123  
-		if(false === $match || !isset($namespaces[$match]))
  113
+		$dump->loadCore($core);
  114
+		if($exportType == 'require')
124 115
 		{
125  
-			return false;
  116
+			$dump->exportRequireList($configuration->getFile('core-export'));
  117
+		}
  118
+		else
  119
+		{
  120
+			$dump->exportConcatenated($configuration->getFile('core-export'), $input->getOption('strip-comments') ? true : false);
126 121
 		}
127  
-		$rest = strrchr($className, $namespaceSeparator);
128  
-		$replacement =
129  
-			str_replace($namespaceSeparator, '/', substr($className, 0, strlen($className) - strlen($rest))).
130  
-			str_replace(array('_', $namespaceSeparator), '/', $rest);
131 122
 		
132  
-		return $namespaces[$match].$replacement;
133  
-	} // end toFilename();
  123
+		$output->writeln('<info>The core loader has been successfully exported.</info>');
  124
+	} // end execute();
134 125
 } // end CoreDump;

0 notes on commit 459a43e

Please sign in to comment.
Something went wrong with that request. Please try again.