Skip to content

Commit

Permalink
support env
Browse files Browse the repository at this point in the history
  • Loading branch information
garming committed Aug 17, 2018
1 parent e632a1d commit 70072ac
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/Tangram/AutoGenerator/AutoLoaderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function generate($absolutePathPerfix)
private function autoload_real_fileContent($str){
return <<<"EOF"
<?php
include __DIR__.DIRECTORY_SEPARATOR."autobuild_env.php";
include __DIR__.DIRECTORY_SEPARATOR."autoload_router_map.php";
include __DIR__.DIRECTORY_SEPARATOR."autoload_permission_map.php";
include __DIR__.DIRECTORY_SEPARATOR."autoload_classmap.php";
Expand Down
63 changes: 63 additions & 0 deletions src/Tangram/AutoGenerator/EnvMapGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Author: garming
* Date: 26/03/2018
* Time: 11:25
*/

namespace Tangram\AutoGenerator;


class EnvMapGenerator extends BaseGenerator
{
public function __construct()
{
$this->fileName = "autobuild_env.php";
$this->config = [];
}

/**
* @var array
*/
private $config;

/**
* @return array
*/
public function getConfig(): array
{
return $this->config;
}

/**
* @param array $config
*
* @return \Tangram\AutoGenerator\EnvMapGenerator
*/
public function setConfig(array $config):EnvMapGenerator
{
$this->config = array_merge($this->config,$config);
return $this;
}

public function generate($absolutePathPerfix)
{
$content = preg_replace('#,(\s+|)\)#', '$1)', var_export($this->config, true));
$this->write($absolutePathPerfix,$this->fileContent($content));
}
private function fileContent($str){
return <<<"EOF"
<?php
function env(\$key = null){
static \$config = {$str};
if(empty(\$key)){
return \$config;
}
if(isset(\$config[\$key])){
return \$config[\$key];
}
return null;
}
EOF;
}
}
5 changes: 4 additions & 1 deletion src/Tangram/Command/Build/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
//todo console auto add `application` argument ??
$env = $input->getArgument("application");

if(array_sum($input->getOptions()) === 0){
$input->setOption('router',1);
$input->setOption('permission',1);
Expand Down Expand Up @@ -54,11 +57,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
}
(new EnvBuild($this))->exec($env);
//command
$this->writeHeader("");
(new ClassMapBuild($this))->exec();
(new AutoLoaderBuild($this))->exec();

}
private function setDefinitions():array
{
Expand Down
48 changes: 48 additions & 0 deletions src/Tangram/Command/Build/EnvBuild.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Created by PhpStorm.
* User: garming
* Date: 11/01/2018
* Time: 01:07
*/

namespace Tangram\Command\Build;


use Symfony\Component\Yaml\Yaml;
use Tangram\AutoGenerator\EnvMapGenerator;
use Tangram\Command\BaseCommandRun;
use Tangram\Resourse\Applications;

class EnvBuild extends BaseCommandRun
{
public function exec($env = null){
if(empty($env)){
return;
}
/** @var \Tangram\Tangram $tangram */
$tangram = $this->getTangram();

$projectConfig = $tangram->getPorjectConfig();

//check env config exist
$file = empty($projectConfig->getEnvPath()) ? $projectConfig->getAbsoluteEnvPath().$env : $projectConfig->getAbsoluteEnvPath().DIRECTORY_SEPARATOR.$env;
if(!file_exists($file)){
$this->getIO()->writeError('<error>'.$projectConfig->getEnvPath().DIRECTORY_SEPARATOR.$env.' missing!!!</error>');
exit;
}
$envConfig = Yaml::parse(file_get_contents($file));
$applications = Applications::all();

$this->writeHeader('🌋️ ENV Build: '.$env);

foreach ($applications as $application) {
$generator = new EnvMapGenerator();
$this->writeHeader(" 💡".$generator->getFileName()." >> $application");

$generator->setConfig($envConfig)
->generate($projectConfig->getAbsoluteApplicationPath() . DIRECTORY_SEPARATOR . $application);

}
}
}
2 changes: 1 addition & 1 deletion src/Tangram/Command/Build/PermissionBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class PermissionBuild extends BaseCommandRun
{
public function exec($targetApplication = null){
$this->writeHeader('👮Build Permission');
$this->writeHeader('👮 Build Permission');
//permission可以放在controller或者action上,这里的实现只解析permission注解,业务逻辑不实现
/** @var \Tangram\Tangram $tangram */
$tangram = $this->getTangram();
Expand Down
2 changes: 1 addition & 1 deletion src/Tangram/Command/Build/RouterBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class RouterBuild extends BaseCommandRun
{
public function exec($targetApplication = null){
$this->writeHeader('🎯Build Router ');
$this->writeHeader('🎯 Build Router ');

/** @var \Tangram\Tangram $tangram */
$tangram = $this->getTangram();
Expand Down
57 changes: 41 additions & 16 deletions src/Tangram/Config/ProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,92 @@ class ProjectConfig {
private $applicationPath;
private $absoluteApplicationPath;
private $absoluteModulePath;
private $envPath;
private $absoluteEnvPath;

public function __construct() {
$this->projectRoot = getcwd();
$configFile = $this->projectRoot.DIRECTORY_SEPARATOR."tangram.json";
$data = json_decode(file_get_contents($configFile),1);
$this->require = $data['require'];
$this->namespace = $data['namespace'];
$this->modulePath = $data['path']['modules'] ?? 'modules';
$this->applicationPath = $data['path']['applications'] ?? 'applications';
$this->absoluteApplicationPath = $this->projectRoot.DIRECTORY_SEPARATOR.$this->applicationPath;
$this->absoluteModulePath = $this->projectRoot.DIRECTORY_SEPARATOR.$this->modulePath;
$this->projectRoot = getcwd();
$configFile = $this->projectRoot.DIRECTORY_SEPARATOR."tangram.json";
$data = json_decode(file_get_contents($configFile),1);
$this->require = $data['require'];
$this->namespace = $data['namespace'];
$this->modulePath = $data['path']['modules'] ?? 'modules';
$this->applicationPath = $data['path']['applications'] ?? 'applications';
$this->envPath = $data['path']['env'] ?? 'env';
$this->absoluteApplicationPath = $this->projectRoot.DIRECTORY_SEPARATOR.$this->applicationPath;
$this->absoluteModulePath = $this->projectRoot.DIRECTORY_SEPARATOR.$this->modulePath;
$this->absoluteEnvPath = $this->projectRoot.DIRECTORY_SEPARATOR.$this->envPath;
}

/**
* @return mixed
*/
public function getRequire() {
public function getRequire()
{
return $this->require;
}

/**
* @return mixed
*/
public function getNamespace() {
public function getNamespace()
{
return $this->namespace;
}

/**
* @return mixed
*/
public function getModulePath() {
public function getModulePath(): string
{
return $this->modulePath;
}

/**
* @return mixed
*/
public function getApplicationPath() {
public function getApplicationPath(): string
{
return $this->applicationPath;
}

/**
* @return mixed
*/
public function getProjectRoot() {
public function getProjectRoot(): string
{
return $this->projectRoot;
}

/**
* @return mixed
*/
public function getAbsoluteApplicationPath()
public function getAbsoluteApplicationPath(): string
{
return $this->absoluteApplicationPath;
}

/**
* @return mixed
*/
public function getAbsoluteModulePath()
public function getAbsoluteModulePath(): string
{
return $this->absoluteModulePath;
}

/**
* @return mixed
*/
public function getEnvPath(): string
{
return $this->envPath;
}

/**
* @return string
*/
public function getAbsoluteEnvPath(): string
{
return $this->absoluteEnvPath;
}
}

0 comments on commit 70072ac

Please sign in to comment.