/** * Get replacement array that will be used for replace in string * * @param Module $module * @param array $definedReplacements * * @return Collection */ private function getReplacements(Module $module, array $definedReplacements) { $replacements = collect(); collect(['module' => $module->name(), 'class' => $module->name(), 'moduleNamespace' => $module->name(), 'namespace' => rtrim($this->configClass()->modulesNamespace(), '\\'), 'plural|lower' => mb_strtolower(str_plural($module->name()))])->merge($definedReplacements)->each(function ($value, $key) use($replacements) { $replacements->put($this->configClass()->startSeparator() . $key . $this->configClass()->endSeparator(), $value); }); return $replacements; }
protected function createModuleMock($options = []) { $this->app = m::mock(Application::class); $this->config = m::mock(Config::class); $this->app->shouldReceive('offsetGet')->once()->with('modular.config')->andReturn($this->config); $this->module = m::mock(Module::class, [$this->name, $this->app, $options])->makePartial()->shouldAllowMockingProtectedMethods(); $this->module->shouldReceive('foo')->andReturn('bar'); }
/** * Add module to configuration file * * @param $module */ protected function addModuleToConfigurationFile(Module $module) { $configFile = $this->laravel['modular.config']->configPath(); if (!$this->laravel['modular.config']->autoAdd()) { $this->info("[Module {$module->name()}] - auto-adding to config file turned off\n" . "Please add this module manually into {$configFile} file if you want to use it"); return; } // getting modified content of config file $result = preg_replace_callback($this->laravel['modular.config']->autoAddPattern(), function ($matches) use($module, $configFile) { return $matches[1] . $matches[2] . $this->replace($this->laravel['modular.config']->autoAddTemplate(), $module) . $matches[3]; }, $this->laravel['files']->get($configFile), -1, $count); if ($count) { // found place where new module should be added into config file $this->laravel['files']->put($configFile, $result); $this->comment("[Module {$module->name()}] Added into config file {$configFile}"); } else { // cannot add module to config file automatically $this->warn("[Module {$module->name()}] It was impossible to add module into {$configFile}" . " file.\n Please make sure you haven't changed structure of this file. " . "At the moment add <info>{$module->name()}</info> to this file manually"); } }
/** * Creates single file * * @param Module $module * @param string $sourceFile * @param string $destinationFile * @param array $replacements * * @throws Exception */ protected function createFile(Module $module, $sourceFile, $destinationFile, array $replacements = []) { $result = $this->laravel['files']->put($module->directory() . DIRECTORY_SEPARATOR . $destinationFile, $this->replace($this->laravel['files']->get($sourceFile), $module, $replacements)); if ($result === false) { throw new Exception("[Module {$module->name()}] Cannot create file {$destinationFile}"); } $this->line("[Module {$module->name()}] Created file {$destinationFile}"); }