Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tiagofrancafernandes/1cc361d2cd89d644d4f60718ea0d57d6 to your computer and use it in GitHub Desktop.
Save tiagofrancafernandes/1cc361d2cd89d644d4f60718ea0d57d6 to your computer and use it in GitHub Desktop.
dev-composer snippets
{
"require": {
"ext-yaml": "^2.2",
"ext-xml": "*"
}
}
{
    "repositories": [
        {
            "type":"package",
            "package": {
              "name": "tiagof2/materializecss-laravel-pagination",
              "version":"v1.0.3",
              "source": {
                  "url": "https://github.com/tiagofrancafernandes/materializecss-laravel-pagination.git",
                  "type": "git",
                  "reference":"v1.0.3"
                }
            }
        }
    ],
    "require": {
        "tiagof2/materializecss-laravel-pagination": "v1.0.3"
    },
}
composer update
composer upgrade --ignore-platform-req=ext-ldap
composer install --ignore-platform-req=ext-ldap
if (!function_exists('safe')) {
    /**
     * safe function  -- A easy way to try/catch run
     *
     * ```php
     * // Usage:
     * safe(fn () => 'may be error action', fn (\Throwable $th) => 'catch action here')
     * ```
     *
     * @param \Closure $runner
     * @param \Closure|null $throwableCatcher
     *
     * @return mixed
     */
    function safe(Closure $runner, ?Closure $throwableCatcher = null): mixed
    {
        try {
            return $runner();
        } catch (\Throwable $th) {
            if (!$throwableCatcher) {
                return false;
            }

            return $throwableCatcher($th);
        }
    }
}

if (!function_exists('safeOr')) {
    /**
     * safeOr function
     *
     * ```php
     * // Usage:
     * safeOr(fn () => 'may be error action', 'fail')
     * safeOr(fn () => 'may be error action', null)
     * safeOr(fn () => 'may be error action')
     * safeOr(fn () => 'may be error action', [])
     * ```
     *
     * @param \Closure $runner
     * @param mixed $valueOnFail
     *
     * @return mixed
     */
    function safeOr(Closure $runner, mixed $valueOnFail = null): mixed
    {
        try {
            return $runner();
        } catch (\Throwable $th) {
            return $valueOnFail;
        }
    }
}

if (!function_exists('array_first')) {
    /**
     * Get the first element of an array.
     *
     * @param  array  $array
     * @param  mixed  $defaultValue
     *
     * @return mixed
     */
    function array_first(array $array, mixed $defaultValue = null)
    {
        return $array[array_key_first($array)] ?? $defaultValue;
    }
}

if (!function_exists('array_last')) {
    /**
     * Get the last element of an array.
     *
     * @param  array  $array
     * @param  mixed  $defaultValue
     *
     * @return mixed
     */
    function array_last(array $array, mixed $defaultValue = null)
    {
        return $array[array_key_last($array)] ?? $defaultValue;
    }
}

if (!function_exists('piped')) {
    /**
     * function piped
     *
     * <code>piped(fn() => [], fn() => [], fn() => [], )->run()</code>
     * <code>piped(fn() => [])->pipe(fn() => [])->->pipe(fn() => [])->run()</code>
     * <code>$a = 0; piped(function() use (&$a) { $a = 12 + 5; }, function() use (&$a) { $a = $a - 7; }); echo $a;</code>
     *
     * @param callable ...$pipedItems
     *
     * @return object
     */
    function piped(callable ...$pipedItems): object
    {
        return new class ($pipedItems) {
            public array $pipedItems;

            public function __construct(
                array $pipedItems = [],
            ) {
                $this->pipedItems = array_filter($pipedItems, fn($item) => is_callable($item));
            }

            public function pipe(callable $item): static
            {
                $this->pipedItems[] = $item;

                return $this;
            }

            public function run(): void
            {
                foreach ($this->pipedItems as &$item) {
                    $copyOfCallable = $item;
                    unset($item);

                    if (!is_callable($copyOfCallable)) {
                        continue;
                    }

                    call_user_func($copyOfCallable);
                }
            }
        };
    }
}

if (!function_exists('is_unserializable')) {
    /**
     * function is_unserializable
     *
     * @param mixed $data
     *
     * @return bool
     */
    function is_unserializable(mixed $data): bool
    {
        $data = is_string($data) ? trim($data) : null;

        if (!$data || !(strlen($data) === 2 && $data === 'N;') || strlen($data) < 4) {
            return false;
        }

        foreach (['N;', ';', ':'] as $partial) {
            if (
                (
                    str_contains($data[1] ?? '', $partial)
                    || str_contains($data, $partial)
                ) && (
                    str_ends_with($data, ';')
                    || str_ends_with($data, '}')
                )
            ) {
                return true;
            }
        }

        return false;
    }
}

if (!function_exists('try_unserialize')) {
    /**
     * function try_unserialize
     *
     * @param mixed $data
     * @param mixed $defaultOnFail
     * @param null|\Closure $catcher
     *
     * @return mixed
     */
    function try_unserialize(
        mixed $data,
        mixed $defaultOnFail = null,
        ?\Closure $catcher = null
    ): mixed {
        try {
            if (!is_unserializable($data)) {
                return $defaultOnFail;
            }

            return unserialize($data);
        } catch (\Throwable $th) {
            if ($catcher) {
                $catcher($th);
            }

            return $defaultOnFail;
        }
    }
}
// ..
    "license": "MIT",
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "rezaamini-ir/laravel-easyblade",
                "version": "dev-tiagof2/v0.6.x",
                "source": {
                    "type": "git",
                    "reference": "tiagof2/v0.6.x", // Nome da branch a usar
                    "url": "https://github.com/tiagofrancafernandes/laravel-easyblade"
                }
            }
        }
    ],
    "require": {
        // ...
        "rezaamini-ir/laravel-easyblade": "dev-tiagof2/v0.6.x", // HERE
        // ...
    },
    // ...
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        // ...
    },
    "minimum-stability": "dev",
    "prefer-stable": true
    // ...
// ..
    "license": "MIT",
    "repositories": [
        {
            "type": "github",
            "branch": "tiagof2/v0.6.x",
            // "url": "https://github.com/tiagofrancafernandes/laravel-json-api-paginate"
            "url": "https://github.com/tiagofrancafernandes/laravel-easyblade"
        },

        // ou

        {
            "type": "package",
            "package": {
                "name": "rezaamini-ir/laravel-easyblade",
                "version": "dev-tiagof2/v0.6.x",
                "source": {
                    "type": "git",
                    "branch": "tiagof2/v0.6.x",
                    "reference": "tiagof2/v0.6.x",
                    "url": "https://github.com/tiagofrancafernandes/laravel-easyblade"
                }
            }
        }
    ],
    "require": {
        // ...
        "spatie/laravel-json-api-paginate": "^1.13|dev-feat/per_page", // HERE
        // ...
    },
    // ...
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        // ...
    },
    "minimum-stability": "dev",
    "prefer-stable": true
    // ...

Leitura:

composer config --list

The probem of symlink On Windows

Note: On Windows, directory symlinks are implemented using NTFS junctions because they can be created by non-admin users. Mirroring will always be used on versions below Windows 7 or if proc_open has been disabled.

Se estiver usando o projeto direto no Windows (sem Docker) pode ter problemas com links simbólicos

Nota!!!

  • É importante que a instatrução de repositório local venha acima da chave require. Veja o exemplo abaixo:
//composer.json

{
    // ...
    "license": "MIT",
    "repositories": [
        {
            "type": "path",
            "url": "./packages/*"
        }
    ],
    "require": {
        "php": "^8.0.2",
        // ...
        "rezaamini-ir/laravel-easyblade": "dev-master", // Esse pacote está no caminho `packages/laravel-easyblade/src/...`
        // ...
    },
    // ...
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        // ...
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

Via GIT repo

//composer.json

{
    // ...
    "license": "MIT",
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/tiagofrancafernandes/laravel-easyblade.git"
        }
    ],
    "require": {
        // ...
        "rezaamini-ir/laravel-easyblade": "dev-laravel-v10",
        // Esse pacote está apontando para a branch `laravel-v10`, o prefixo `dev-` é obrigatório
        // ...
    },
    // ...
}

Arquivo básico

{
    "name": "vendor/package",
    "type": "project",
    "description": "Demo Application",
    "keywords": [
        "composer preset",
        "php script"
    ],
    "license": "MIT",
    "repositories": [
        {
            "type": "path",
            "url": "./packages/*"
        }
    ],
    "require": {
        "php": "^8.0.2"
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        },
    },
    "scripts": {
        "post-autoload-dump": [
            "@php -v"
        ]
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

!!! Lembre-se de executar:

mkdir -p src/ && touch src/.gitkeep; mkdir -p packages/ && touch packages/.gitkeep
{
    "repositories": [
        {
            "type": "vcs",
            "url":  "git@bitbucket.org:vendor/my-private-repo.git"
        }
    ],
    "require": {
        "vendor/my-private-repo": "dev-master"
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment