inhere 2 years ago
parent
commit
1bc2121419
4 changed files with 23 additions and 244 deletions
  1. 0 1
      composer.json
  2. 1 32
      src/BaseSlim.php
  3. 0 211
      src/Slim.php
  4. 22 0
      src/console/App.php

+ 0 - 1
composer.json

@@ -34,7 +34,6 @@
       "src/defined.php",
       "src/functions.php"
     ],
-    "classmap": ["src/Slim.php"],
     "psr-4": {
       "slimExt\\" : "src/"
     }

+ 1 - 32
src/BaseSlim.php

@@ -12,7 +12,7 @@ use inhere\library\traits\PathAliasTrait;
  * ```
  * // before, please extend it in your application.
  *
- * class Slim extend \slimExt\BaseSlim {
+ * class Slim extends \slimExt\BaseSlim {
  *      // ...
  * }
  *
@@ -130,37 +130,6 @@ abstract class BaseSlim
     }
 
     /**
-     * @param mixed $key
-     * @param mixed $default
-     * @return \slimExt\Collection|mixed
-     */
-    public static function config($key = null, $default = null)
-    {
-        /** @var \slimExt\Collection $config */
-        $config = static::$app->getContainer()['config'];
-
-        if ($key && is_string($key)) {
-            return $config->get($key, $default);
-        }
-
-        // set, when $key is array
-        if ($key && is_array($key)) {
-            return $config->loadArray($key);
-        }
-
-        return $config;
-    }
-
-    /**
-     * @param string $name
-     * @return \Monolog\Logger
-     */
-    public static function logger($name = 'logger')
-    {
-        return static::$app->getContainer()[$name];
-    }
-
-    /**
      * @param string $name
      * @param array $args
      */

+ 0 - 211
src/Slim.php

@@ -1,211 +0,0 @@
-<?php
-
-/**
- * Class Slim
- * @date  2016.2.17
- *
- * ---------------
- * How to quickly get a service instance?
- * e.g:
- * get request service instance.
- *
- * ```
- *     Slim::get('request')
- * equal
- *     Slim::$app->request // by the magic method { @see \slimExt\base\App::__get() }
- * equal
- *     Slim::$app->request() // by the magic method { @see \Slim\App::__call() }
- * ```
- * @method static \slimExt\Collection cache() Return a driver config instance
- */
-abstract class Slim
-{
-    /**
-     * @var $app \slimExt\web\App
-     */
-    public static $app;
-
-    /**
-     * path alias
-     * @var array
-     */
-    protected static $aliases = [
-        '@project' => PROJECT_PATH,
-        '@public' => PROJECT_PATH . DIR_SEP . 'public',
-        '@config' => PROJECT_PATH . DIR_SEP . 'config',
-
-        '@src' => PROJECT_PATH . DIR_SEP . 'src',
-        '@res' => PROJECT_PATH . DIR_SEP . 'resources',
-        '@resources' => PROJECT_PATH . DIR_SEP . 'resources',
-        '@temp' => PROJECT_PATH . DIR_SEP . 'temp',
-
-        // '@assets'  => PROJECT_PATH . DIR_SEP . 'public' . DIR_SEP . 'assets',
-        '@modules' => PROJECT_PATH . DIR_SEP . 'src' . DIR_SEP . 'modules',
-
-        '@vendor' => PROJECT_PATH . DIR_SEP . 'vendor',
-    ];
-
-    /**
-     * set/get path alias
-     * @param array|string $path
-     * @param string|null $value
-     * @return bool|string
-     */
-    public static function alias($path, $value = null)
-    {
-        // get path by alias
-        if (is_string($path) && !$value) {
-            // don't use alias
-            if ($path[0] !== '@') {
-                return $path;
-            }
-
-            $path = str_replace(['/', '\\'], DIR_SEP, $path);
-
-            // only a alias. e.g. @project
-            if (!strpos($path, DIR_SEP)) {
-                return static::$aliases[$path] ?? $path;
-            }
-
-            // have other partial. e.g: @project/temp/logs
-            $realPath = $path;
-            list($alias, $other) = explode(DIR_SEP, $path, 2);
-
-            if (isset(static::$aliases[$alias])) {
-                $realPath = static::$aliases[$alias] . DIR_SEP . $other;
-            }
-
-            return $realPath;
-        }
-
-        if ($path && $value && is_string($path) && is_string($value)) {
-            $path = [$path => $value];
-        }
-
-        // custom set path's alias. e.g: Slim::alias([ 'alias' => 'path' ]);
-        if (is_array($path)) {
-            foreach ($path as $alias => $realPath) {
-                // 1th char must is '@'
-                if ($alias[0] !== '@') {
-                    continue;
-                }
-
-                static::$aliases[$alias] = $realPath;
-            }
-        }
-
-        return true;
-    }
-
-    /**
-     * @return array
-     */
-    public static function getAliases()
-    {
-        return static::$aliases;
-    }
-
-    /**
-     * @param $id
-     * @return mixed
-     */
-    public static function has($id)
-    {
-        if (!static::$app) {
-            return null;
-        }
-
-        return static::$app->getContainer()->has($id);
-    }
-
-    /**
-     * @param $id
-     * @return mixed
-     */
-    public static function get($id)
-    {
-        if (!static::$app) {
-            return null;
-        }
-
-        return static::$app->getContainer()[$id];
-    }
-
-    /**
-     * @param $id
-     * @param array $params
-     * @return mixed
-     */
-    public static function call($id, array $params = [])
-    {
-        if (!static::$app) {
-            return null;
-        }
-
-        return static::$app->container->call($id, $params);
-    }
-
-    /**
-     * @param $id
-     * @param string $class
-     * @param array $params
-     * @return mixed
-     * @throws \inhere\exceptions\LogicException
-     */
-    public static function make($id, $class = null, $params = null)
-    {
-        if (!static::$app) {
-            return null;
-        }
-
-        $callable = static::$app->container->factory(function () use ($class, $params) {
-            return new $class($params);
-        });
-
-        static::$app->container[$id] = $callable;
-
-        return true;
-    }
-
-    /**
-     * @param $id
-     * @param $value
-     */
-    public static function set($id, $value)
-    {
-        if (static::$app) {
-            static::$app->$id = $value;
-        }
-    }
-
-    /**
-     * @param mixed $key
-     * @param mixed $default
-     * @return \slimExt\Collection|mixed
-     */
-    public static function config($key = null, $default = null)
-    {
-        /** @var \slimExt\Collection $config */
-        $config = static::$app->getContainer()['config'];
-
-        if ($key && is_string($key)) {
-            return $config->get($key, $default);
-        }
-
-        // set, when $key is array
-        if ($key && is_array($key)) {
-            return $config->loadArray($key);
-        }
-
-        return $config;
-    }
-
-    /**
-     * @param string $name
-     * @return \Monolog\Logger
-     */
-    public static function logger($name = 'logger')
-    {
-        return static::$app->getContainer()[$name];
-    }
-}

+ 22 - 0
src/console/App.php

@@ -67,6 +67,28 @@ class App extends \inhere\console\App
     }
 
     /**
+     * get/set config
+     * @param  array|string $name
+     * @param  mixed $default
+     * @return mixed
+     */
+    public function config($name, $default = null)
+    {
+        $config = $this->getContainer()['config'];
+
+        // `$name` is array, set config.
+        if (is_array($name)) {
+            foreach ((array) $name as $key => $value) {
+                $config[$key] = $value;
+            }
+
+            return true;
+        }
+
+        return $config->get($name, $default);
+    }
+
+    /**
      * @param string $name
      * @param mixed $handler
      * @return $this