/** * Exports all PHP files for this extension * * @param string $directory * @param bool $create_sub_directories * * @throws \Exception * @throws \InvalidArgumentException */ public function exportFiles($directory, $create_sub_directories = true) { $dir = realpath($directory); if (empty($dir) || !file_exists($dir)) { throw new \InvalidArgumentException("Directory does not exist: {$directory}"); } foreach ($this->_extension->getClasses() as $class) { $reflection_class = new ReflectionClass($class); $current_dir = $dir; if ($create_sub_directories) { $namespaces = explode('\\', $class->getNamespaceName()); array_shift($namespaces); $sub_dirs = join(DIRECTORY_SEPARATOR, $namespaces); if (!empty($sub_dirs)) { $current_dir = $dir . DIRECTORY_SEPARATOR . $sub_dirs; if (!file_exists($current_dir) && !@mkdir($current_dir, 0755, true)) { throw new \Exception('Could not create sub directories: ' . $sub_dirs); } } } $filename = $reflection_class->getClassName() . '.php'; $file_path = $current_dir . DIRECTORY_SEPARATOR . $filename; $result = file_put_contents($file_path, $reflection_class->exportCode()); if ($result === false) { throw new \Exception('Could not create file: ' . $file_path); } } }